pestphp / pest

Pest is an elegant PHP testing Framework with a focus on simplicity, meticulously designed to bring back the joy of testing in PHP.
https://pestphp.com
MIT License
9.12k stars 319 forks source link

[Bug]: Strict type checking falsely stating success #1007

Open Tklaversma opened 8 months ago

Tklaversma commented 8 months ago

What Happened

The following test will check if declare(strict_types=1); is in any file within the directory app.

test('Application uses strict typing')
    ->expect('App')
    ->toUseStrictTypes();

However, it is not checking if the line is actually being used. For example, whenever you comment the line, the test will still return true.

I would expect to check if the line is used.

How to Reproduce

  1. Define the following test:

    test('Application uses strict typing')
    ->expect('App')
    ->toUseStrictTypes();
  2. Comment declare(strict_types=1); in a file within the directory app.

  3. Run the test and see it succeed, instead of returning failed.

Sample Repository

No response

Pest Version

2.24.3

PHP Version

8.1

Operation System

macOS, Windows, Linux

Notes

No response

dasundev commented 6 months ago

Yeah, this is confirm. I have also noticed this issue right now.

owenvoke commented 6 months ago

Yeah, this only uses a str_contains() on the file contents.

I think what would make the most sense is to probably have a regex check for something like:

^<\?php\s+declare\(.*?strict_types\s?=\s?1.*?\);

Preview on regex101

That would be slower, but would resolve the issue. 🤷🏻 It would also ensure that it's at the start of a line (so no // <?php declare(strict_types=1);) and also support a declare() usage that includes ticks or encoding.

kusab85 commented 4 months ago

Bumping up the issue. Minor thing but annoying.