{"record":{"id":"814a397017b27aa3","repo":"rectorphp/rector","slug":"s-is-deprecated-as-it-worsens-readability-use","errorCode":null,"errorMessage":"\"%s\" is deprecated as it worsens readability. Use \"match (true)\" for the same logic instead","messagePattern":"\"(.+?)\" is deprecated as it worsens readability\\. Use \"match \\(true\\)\" for the same logic instead","errorType":"exception","errorClass":"ShouldNotHappenException","httpStatus":null,"severity":"error","filePath":"rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php","lineNumber":69,"sourceCode":"        }\n    }\n}\nCODE_SAMPLE\n)]);\n    }\n    /**\n     * @return array<class-string<Node>>\n     */\n    public function getNodeTypes(): array\n    {\n        return [Switch_::class];\n    }\n    /**\n     * @param Switch_ $node\n     */\n    public function refactor(Node $node): ?Node\n    {\n        throw new ShouldNotHappenException(sprintf('\"%s\" is deprecated as it worsens readability. Use \"match (true)\" for the same logic instead', self::class));\n    }\n}\n","sourceCodeStart":51,"sourceCodeEnd":72,"githubUrl":"https://github.com/rectorphp/rector/blob/408fcb0ff1833e3d26cbc9b04c23a77565814a16/rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php#L51-L72","documentation":"SwitchTrueToIfRector used to convert switch (true) { case ...: } constructs into if/elseif chains. It is deprecated because the rewrite worsens readability — PHP 8's match (true) expresses the same dispatch more compactly. refactor() now throws ShouldNotHappenException for every Switch_ node; the rule kept its class and constants only so outdated configurations abort instead of silently producing worse code.","triggerScenarios":"Registering rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php in config and running process/dry-run over any file that contains a switch statement. The throw fires at rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php:69 on the first Switch_ node visited.","commonSituations":"Configs migrated from older Rector versions still list SwitchTrueToIfRector, or import an aged CodeQuality set that included it. Projects modernising to PHP 8 often kept this rule from a pre-match era, then hit the guard after a rector/rector-prefixed upgrade.","solutions":["Remove SwitchTrueToIfRector::class from rector.php / imported sets and re-run dry-run","If you want to modernise switch (true), convert it manually to match (true) on PHP 8+","Consider other still-supported switch rules (e.g. SwitchTrueToMatchRector if available in your version) instead","Audit shared set packages for the deprecated class name"],"exampleFix":"// before (rector.php)\n->withRules([\n    \\Rector\\CodeQuality\\Rector\\Switch_\\SwitchTrueToIfRector::class,\n]);\n// code:\nswitch (true) {\n    case $x < 0: $label = 'neg'; break;\n    default: $label = 'pos';\n}\n\n// after (rector.php)\n// rule removed\n// code converted manually to match:\n$label = match (true) {\n    $x < 0 => 'neg',\n    default => 'pos',\n};","handlingStrategy":"validation","validationCode":"$deprecated = ['SwitchTrueToIfRector'];\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":["On PHP 8+, prefer match (true) over both switch(true) and if-chains","Remove rules flagged deprecated in the version you upgrade to","Test rector dry-run on a branch before merging the composer upgrade","Convert dispatch constructs manually where readability demands it"],"tags":["rector","php","deprecated-rule","switch","match-expression"],"backgroundTag":"rector-deprecated-rule","analyzedSha":"408fcb0ff1833e3d26cbc9b04c23a77565814a16","analyzedAt":"2026-08-21T05:11:02.643Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}