Repository navigation
[CodingStyle] Deprecate CountArrayToEmptyArrayComparisonRector - #8274
Conversation
|
Which coding standard tool would help me rewrite this? I have applied this rule plenty of times already. |
|
Goal of this rule was to gain performance, but there is none. It also removes the intent of count and implies general preference. This rule was very often skipped to avoid pointless cosmetic changes. |
|
I totally understand that people want to skip this rule but wouldn't it then be good enough to just remove it from the set and leave it in the code base for people who still want to use it (like me)? Now I need to copy the rule to my own code base instead of just reference it. |
|
Having it here in basic sets would suggest it's a best practise to do, which is not. This is why it fits better as custom rule, that are now very cheap to make. |
## Description This follows [the discussion on #3717](#3717 (comment)). I don't mind whether we use `count()` or direct comparisons with `[]`, I just want the chosen form enforced automatically so we don't have to think about it during reviews. Rector originally provided this rule, then [deprecated and disabled it](rectorphp/rector-src#8274) because the transformation only enforces coding style. This PR restores a local version: ```diff -count($array) === 0; +$array === []; -count($array) > 0; +$array !== []; ``` ## Changes - Register the rule in the project Rector configuration, with support for reversed comparisons, negated counts, and direct `if`/`elseif` conditions. - Require a native array type and a single, non-unpacked argument. Preserve `Countable` checks, custom `count()` functions, calls with an explicit mode, and unrelated comparisons such as `count($array) >= 0`. - Add fixtures for supported rewrites and checks that must remain unchanged. - Exclude the vendored `UnifiedDiffOutputBuilder` to minimise cosmetic differences from `sebastian/diff`. ## Reviewer Notes The rule was copied from Rector and slightly adjusted for readability. All work in this PR was otherwise done by AI.
Deprecate
CountArrayToEmptyArrayComparisonRectorand remove it from the coding style level.The rule only swaps one equivalent check for another - no real win, it is a coding standard preference. Better left to a coding standard tool.