PHPCSStandards / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
856 stars 53 forks source link

Generic/DisallowYogaConditions: false negatives when handling arrays #467

Open rodrigoprimo opened 4 months ago

rodrigoprimo commented 4 months ago

Describe the bug

The Generic.ControlStructures.DisallowYodaConditions sniff has a method called isArrayStatic() that determines what the sniff considers to be static arrays. When static arrays are found on the side of a condition, the sniff triggers an error. I would expect the examples below to be considered static arrays and to trigger an error, but they are not.

Code sample

[1, 2] === $value;
[null] === $value;

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above.
  2. Run phpcs --standard=Generic --sniffs=Generic.ControlStructures.DisallowYodaConditions tests.php
  3. Confirm that no errors were displayed.

Expected behavior

I would expect the sniff to generate a Generic.ControlStructures.DisallowYodaConditions.Found error for each line in the code sample above. Similar to what happens, for example, with ['some string'] === $value.

Versions (please complete the following information)

Operating System Ubuntu 23.10
PHP version 8.3.4
PHP_CodeSniffer version master
Standard generic
Install type git clone

Please confirm

rodrigoprimo commented 4 months ago

I have removed the Status: triage label as Juliette already confirmed this to be a valid issue in https://github.com/squizlabs/PHP_CodeSniffer/issues/2830#issuecomment-2075880864. I also added the Standard: Generic label.