{"record":{"id":"2aa6c08c2b12bae1","repo":"rectorphp/rector","slug":"s-rule-is-deprecated-as-inverting-nested-ifs-t","errorCode":null,"errorMessage":"\"%s\" rule is deprecated, as inverting nested ifs to continue makes the code harder to read and understand","messagePattern":"\"(.+?)\" rule is deprecated, as inverting nested ifs to continue makes the code harder to read and understand","errorType":"exception","errorClass":"ShouldNotHappenException","httpStatus":null,"severity":"error","filePath":"rules/EarlyReturn/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector.php","lineNumber":71,"sourceCode":"        }\n    }\n}\nCODE_SAMPLE\n)]);\n    }\n    /**\n     * @return array<class-string<Node>>\n     */\n    public function getNodeTypes(): array\n    {\n        return [Foreach_::class];\n    }\n    /**\n     * @param Foreach_ $node\n     */\n    public function refactor(Node $node): ?Node\n    {\n        throw new ShouldNotHappenException(sprintf('\"%s\" rule is deprecated, as inverting nested ifs to continue makes the code harder to read and understand', self::class));\n    }\n}\n","sourceCodeStart":53,"sourceCodeEnd":74,"githubUrl":"https://github.com/rectorphp/rector/blob/408fcb0ff1833e3d26cbc9b04c23a77565814a16/rules/EarlyReturn/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector.php#L53-L74","documentation":"ChangeNestedForeachIfsToEarlyContinueRector used to invert nested if blocks inside foreach loops into guard continue statements (early-continue style). It is deprecated because inverting nested ifs makes the loop body harder to read and understand — the guard-chain style trades nesting for order-dependence. refactor() now throws ShouldNotHappenException on every Foreach_ node; the rule performs no rewrite and exists only as a deprecation marker.","triggerScenarios":"Registering rules/EarlyReturn/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector.php in config and running rector over any file containing a foreach loop. The throw comes from rules/EarlyReturn/Rector/Foreach_/ChangeNestedForeachIfsToEarlyContinueRector.php:71 on the first Foreach_ node visited.","commonSituations":"Configs importing the EarlyReturn set or listing this rule individually, carried forward into a Rector version where it was deprecated. The first dry-run over any loop-heavy file (importers, iterators) crashes, which looks like a regression in Rector rather than a config issue.","solutions":["Remove ChangeNestedForeachIfsToEarlyContinueRector::class from rector.php and every imported set, then re-run vendor/bin/rector dry-run","Convert specific loops to early-continue manually only where the guard style genuinely reads better","Prefer extracting the loop body into a named method that uses early returns — clearer than either inline form","grep -r ChangeNestedForeachIfsToEarlyContinueRector across the project to catch shared set files"],"exampleFix":"// before (rector.php)\n->withRules([\n    \\Rector\\EarlyReturn\\Rector\\Foreach_\\ChangeNestedForeachIfsToEarlyContinueRector::class,\n])\n// code:\nforeach ($items as $item) {\n    if ($item->isValid()) {\n        if ($item->isReady()) {\n            $handler->process($item);\n        }\n    }\n}\n\n// after (rector.php)\n// rule removed; loop left as is, or hand-converted where clearer:\nforeach ($items as $item) {\n    if (! $item->isValid()) {\n        continue;\n    }\n    if (! $item->isReady()) {\n        continue;\n    }\n    $handler->process($item);\n}","handlingStrategy":"validation","validationCode":"$deprecated = ['ChangeNestedForeachIfsToEarlyContinueRector'];\nforeach (glob(__DIR__ . '/rector*.php') as $config) {\n    $src = file_get_contents($config);\n    foreach ($deprecated as $rule) {\n        if (str_contains($src, $rule)) {\n            fwrite(STDERR, \"Remove deprecated rule {$rule} from {$config}\\n\");\n            exit(1);\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Restructure loops manually or by extracting methods with early returns","Import curated sets instead of enumerating individual style rules","Delete rules the upgraded Rector version deprecates","Run vendor/bin/rector dry-run on a fixture path in CI to catch guard throws"],"tags":["rector","php","deprecated-rule","foreach","early-return","readability"],"backgroundTag":"rector-deprecated-rule","analyzedSha":"408fcb0ff1833e3d26cbc9b04c23a77565814a16","analyzedAt":"2026-08-21T05:11:02.643Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}