Skip to content

[CodingStyle] Deprecate CountArrayToEmptyArrayComparisonRector - #8274

Merged
TomasVotruba merged 2 commits into
mainfrom
deprecate-count-array-to-empty-array-comparison
Aug 3, 2026
Merged

TomasVotruba merged 2 commits into
mainfrom
deprecate-count-array-to-empty-array-comparison

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Deprecate CountArrayToEmptyArrayComparisonRector and 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.

-count($array) === 0;
-count($array) > 0;
-! count($array);
+$array === [];
+$array !== [];
+$array === [];

@TomasVotruba
TomasVotruba merged commit 0e8debc into main Aug 3, 2026
64 checks passed
@TomasVotruba
TomasVotruba deleted the deprecate-count-array-to-empty-array-comparison branch August 3, 2026 12:40
@simonschaufi

Copy link
Copy Markdown
Contributor

Which coding standard tool would help me rewrite this? I have applied this rule plenty of times already.

@TomasVotruba

Copy link
Copy Markdown
Member Author

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.
See this post on similar note https://tideways.com/profiler/blog/new-in-php-8.6-narrowing-the-array_map-vs-foreach-performance-gap

@simonschaufi

Copy link
Copy Markdown
Contributor

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.

@TomasVotruba

Copy link
Copy Markdown
Member Author

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.

theofidry added a commit to infection/infection that referenced this pull request Oct 31, 2026
## 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants