pest icon indicating copy to clipboard operation
pest copied to clipboard

[Bug]: Strict type checking falsely stating success

Open Tklaversma opened this issue 1 year ago • 3 comments

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();
  1. Comment declare(strict_types=1); in a file within the directory app.

  2. 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

Tklaversma avatar Nov 14 '23 08:11 Tklaversma

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

dasundev avatar Jan 11 '24 08:01 dasundev

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.

owenvoke avatar Jan 11 '24 13:01 owenvoke

Bumping up the issue. Minor thing but annoying.

kusab85 avatar Feb 21 '24 09:02 kusab85