{"record":{"id":"2056c6107391f4af","repo":"rectorphp/rector","slug":"rule-s-exists-but-is-not-registered-in-your-re","errorCode":null,"errorMessage":"Rule \"%s\" exists, but is not registered in your Rector config.%sRegister it in your rector.php:\n\n    ->withRules([%s::class])","messagePattern":"Rule \"(.+?)\" exists, but is not registered in your Rector config\\.(.+?)Register it in your rector\\.php:\n\n    ->withRules\\(\\[(.+?)::class\\]\\)","errorType":"exception","errorClass":"RectorRuleNotFoundException","httpStatus":null,"severity":"error","filePath":"src/Configuration/OnlyRuleResolver.php","lineNumber":73,"sourceCode":"            throw new RectorRuleNameAmbiguousException($message);\n        }\n        if (strpos($rule, '\\\\') === \\false) {\n            // the shell has eaten unescaped backslashes, e.g. --only=\\Rector\\Some\\Rule\n            $flattenMatching = [];\n            foreach ($this->rectors as $rector) {\n                if (str_replace('\\\\', '', get_class($rector)) === $rule) {\n                    $flattenMatching[] = get_class($rector);\n                }\n            }\n            $flattenMatching = array_unique($flattenMatching);\n            if (count($flattenMatching) === 1) {\n                return $flattenMatching[0];\n            }\n            $message = sprintf('Rule \"%s\" was not found.%sThe rule has no namespace. Make sure to escape the backslashes, and add quotes around the rule name: --only=\"My\\Rector\\Rule\"', $rule, \\PHP_EOL);\n        } else {\n            // the rule class exists, it is just missing in the config\n            if ($this->isRectorRuleClass($rule)) {\n                throw new RectorRuleNotFoundException($this->createUnregisteredMessage($rule));\n            }\n            $message = sprintf('Rule \"%s\" was not found.%sMake sure it is registered in your config or in one of the sets', $rule, \\PHP_EOL);\n        }\n        throw new RectorRuleNotFoundException($message);\n    }\n    /**\n     * Is this an existing rule class, that is just not registered in the config?\n     */\n    private function isRectorRuleClass(string $className): bool\n    {\n        if (!class_exists($className)) {\n            return \\false;\n        }\n        $reflectionClass = new ReflectionClass($className);\n        if ($reflectionClass->isAbstract()) {\n            return \\false;\n        }\n        return $reflectionClass->implementsInterface(RectorInterface::class);","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/rectorphp/rector/blob/408fcb0ff1833e3d26cbc9b04c23a77565814a16/src/Configuration/OnlyRuleResolver.php#L55-L91","documentation":"When a --only value with a namespace matches no registered rule, OnlyRuleResolver checks whether the class exists and implements RectorInterface; if so, the rule is real but simply not registered in your container, and it throws RectorRuleNotFoundException with a ready-to-paste withRules() snippet. The fix is registration, not spelling -- the class string you typed is already correct.","triggerScenarios":"Running --only=\"Rector\\Php81\\Rector\\Property\\ReadOnlyPropertyRector\" while rector.php registers no set that includes it (no withPhpSets/withPhpLevel, or the relevant set was removed during a config cleanup).","commonSituations":"Trying a rule you saw in docs/changelog without loading its set; configs that were trimmed to a tiny withRules() list; rule moved to a level/set you no longer load after an upgrade.","solutions":["Register the rule as the message suggests: add ->withRules([ReadOnlyPropertyRector::class]) to rector.php (copy the snippet from the exception)","Or load the set that ships it, e.g. ->withPhpSets() / ->withPhpLevel(...), instead of naming the rule individually","Verify registration first: search rector.php (and imported configs) for the class short name"],"exampleFix":"// before\nreturn RectorConfig::configure()\n    ->withPaths([__DIR__ . '/src']);\n// vendor/bin/rector process src --only=\"Rector\\Php81\\Rector\\Property\\ReadOnlyPropertyRector\"\n\n// after\nreturn RectorConfig::configure()\n    ->withPaths([__DIR__ . '/src'])\n    ->withRules([\\Rector\\Php81\\Rector\\Property\\ReadOnlyPropertyRector::class]);","handlingStrategy":"validation","validationCode":"// in rector.php, reference rules via ::class constants so typos surface as fatal class loads\nuse Rector\\Php81\\Rector\\Property\\ReadOnlyPropertyRector;\nreturn RectorConfig::configure()\n    ->withRules([ReadOnlyPropertyRector::class]);","typeGuard":null,"tryCatchPattern":"catch \\Rector\\Exception\\Configuration\\RectorRuleNotFoundException; when the message contains 'exists, but is not registered', register the rule via withRules() (or load its set) and re-run.","preventionTips":["Use ::class imports instead of string literals in withRules()","When testing a rule from docs, also load the set that ships it (e.g. withPhpSets())","Keep --only for temporarily narrowing an already-registered rule set"],"tags":["php","rector","cli","rule-selection","registration","config"],"backgroundTag":"rule-not-registered","analyzedSha":"408fcb0ff1833e3d26cbc9b04c23a77565814a16","analyzedAt":"2026-08-21T05:11:02.643Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}