{"record":{"id":"ba136d6155b8bb9d","repo":"rectorphp/rector","slug":"s-is-deprecated-as-null-compare-is-ambiguous-a","errorCode":null,"errorMessage":"\"%s\" is deprecated, as null compare is ambiguous and weak; use an \"instanceof\" check instead","messagePattern":"\"(.+?)\" is deprecated, as null compare is ambiguous and weak; use an \"instanceof\" check instead","errorType":"exception","errorClass":"ShouldNotHappenException","httpStatus":null,"severity":"error","filePath":"rules/CodingStyle/Rector/If_/NullableCompareToNullRector.php","lineNumber":50,"sourceCode":"\nif ($value === null) {\n}\nCODE_SAMPLE\n)]);\n    }\n    /**\n     * @return array<class-string<Node>>\n     */\n    public function getNodeTypes(): array\n    {\n        return [If_::class];\n    }\n    /**\n     * @param If_ $node\n     */\n    public function refactor(Node $node): ?Node\n    {\n        throw new ShouldNotHappenException(sprintf('\"%s\" is deprecated, as null compare is ambiguous and weak; use an \"instanceof\" check instead', self::class));\n    }\n}\n","sourceCodeStart":32,"sourceCodeEnd":53,"githubUrl":"https://github.com/rectorphp/rector/blob/408fcb0ff1833e3d26cbc9b04c23a77565814a16/rules/CodingStyle/Rector/If_/NullableCompareToNullRector.php#L32-L53","documentation":"NullableCompareToNullRector used to rewrite loose object-vs-null comparisons such as if ($object != null) into stricter forms / null checks. It is deprecated because comparing objects against null with weak comparison is ambiguous (a falsy object vs a null object), and the intended check is usually $obj instanceof Foo — something a context-free rule cannot infer. refactor() now throws ShouldNotHappenException on every If_ node; the rule transforms nothing.","triggerScenarios":"Registering rules/CodingStyle/Rector/If_/NullableCompareToNullRector.php and running rector over any file containing an if statement. The throw comes from rules/CodingStyle/Rector/If_/NullableCompareToNullRector.php:50 on the first If_ visited — effectively the first file analysed.","commonSituations":"Legacy rector.php rule lists or set bundles carried into a newer rector/rector-prefixed still name NullableCompareToNullRector. Since every procedural file has an if, the guard exception appears immediately and is frequently misdiagnosed as a vendor corruption issue.","solutions":["Remove NullableCompareToNullRector::class from rector.php and all imported sets, then re-run dry-run","Replace weak null comparisons by hand with the check you actually mean: $obj instanceof Foo, $obj !== null, or !empty($obj) as appropriate","Enable strict_types and let static analysis (PHPStan level 6+) flag remaining weak comparisons","grep -r NullableCompareToNullRector to eliminate every config reference"],"exampleFix":"// before (rector.php)\n->withRules([\n    \\Rector\\CodingStyle\\Rector\\If_\\NullableCompareToNullRector::class,\n]);\n// code:\nif ($handler != null) {\n    $handler->run();\n}\n\n// after (rector.php)\n// rule removed; manual fix expressing real intent:\nif ($handler instanceof Handler) {\n    $handler->run();\n}","handlingStrategy":"validation","validationCode":"$deprecated = ['NullableCompareToNullRector'];\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":["Replace weak null comparisons with instanceof or !== null by hand","Declare strict_types and run PHPStan at high level to surface weak comparisons","Remove deprecated rules from rector.php on every upgrade","Validate configs with a CI dry-run before rolling Rector out"],"tags":["rector","php","deprecated-rule","null-comparison","instanceof","weak-comparison"],"backgroundTag":"rector-deprecated-rule","analyzedSha":"408fcb0ff1833e3d26cbc9b04c23a77565814a16","analyzedAt":"2026-08-21T05:11:02.643Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}