rectorphp/rector · error · ShouldNotHappenException

"%s" rule is deprecated, as it is a coding standard preferen

Error message

"%s" rule is deprecated, as it is a coding standard preference with no real value

What it means

CountArrayToEmptyArrayComparisonRector used to rewrite count($items) === 0 into $items === [] (and count($items) > 0 into $items !== []). It is deprecated because this is a coding-standard preference with no real value, and the rewritten form can be subtler about type strictness. refactor() now throws ShouldNotHappenException on every Identical (===) node; the rule no longer performs any transformation.

Source

Thrown at rules/CodingStyle/Rector/FuncCall/CountArrayToEmptyArrayComparisonRector.php:44

$array === [];
$array !== [];
$array === [];
CODE_SAMPLE
)]);
    }
    /**
     * @return array<class-string<Node>>
     */
    public function getNodeTypes(): array
    {
        return [Identical::class];
    }
    /**
     * @param Identical $node
     */
    public function refactor(Node $node): ?Node
    {
        throw new ShouldNotHappenException(sprintf('"%s" rule is deprecated, as it is a coding standard preference with no real value', self::class));
    }
}

View on GitHub (pinned to 408fcb0ff1)

Solutions

  1. Remove CountArrayToEmptyArrayComparisonRector::class from rector.php / imported sets and re-run vendor/bin/rector dry-run
  2. Keep count($items) === 0 — it is explicit and universally understood
  3. Rewrite individual sites by hand if your team prefers the [] comparison
  4. grep -r CountArrayToEmptyArrayComparisonRector across configs to catch indirect imports

Example fix

// before (rector.php)
->withRules([
    \Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector::class,
])
// code:
if (count($items) === 0) {
    return;
}

// after (rector.php)
// rule removed; manual rewrite only if preferred:
if ($items === []) {
    return;
}
Defensive patterns

Strategy: validation

Validate before calling

$deprecated = ['CountArrayToEmptyArrayComparisonRector'];
foreach (glob(__DIR__ . '/rector*.php') as $config) {
    $src = file_get_contents($config);
    foreach ($deprecated as $rule) {
        if (str_contains($src, $rule)) {
            fwrite(STDERR, "Remove deprecated rule {$rule} from {$config}\n");
            exit(1);
        }
    }
}

Prevention

When it happens

Trigger: Registering rules/CodingStyle/Rector/FuncCall/CountArrayToEmptyArrayComparisonRector.php and running rector over any file containing a === comparison. The throw fires at rules/CodingStyle/Rector/FuncCall/CountArrayToEmptyArrayComparisonRector.php:44 on the first identical comparison — i.e. almost immediately.

Common situations: Style-driven rector.php files or shared set bundles that standardised on the [] === form still list this rule after a Rector upgrade. Because === comparisons are pervasive, the first analysed file crashes, giving the false impression of an installation problem.

Related errors


AI-assisted analysis of rectorphp/rector@408fcb0ff1 (2026-08-21). Data as JSON: /api/errors/28c648215e42110e. Report an issue: GitHub.