{"record":{"id":"2670be02cc7dad41","repo":"rectorphp/rector","slug":"s-rule-is-deprecated-as-the-scalar-value-targe","errorCode":null,"errorMessage":"\"%s\" rule is deprecated, as the scalar value target is too wide and replaces values in unrelated contexts; use a custom rule scoped to the exact context instead","messagePattern":"\"(.+?)\" rule is deprecated, as the scalar value target is too wide and replaces values in unrelated contexts; use a custom rule scoped to the exact context instead","errorType":"exception","errorClass":"ShouldNotHappenException","httpStatus":null,"severity":"error","filePath":"rules/Transform/Rector/Scalar/ScalarValueToConstFetchRector.php","lineNumber":46,"sourceCode":"        return new RuleDefinition('Replaces Scalar values with a ConstFetch or ClassConstFetch', [new ConfiguredCodeSample(<<<'SAMPLE'\n$var = 10;\nSAMPLE\n, <<<'SAMPLE'\n$var = \\SomeClass::FOOBAR_INT;\nSAMPLE\n, [new ScalarValueToConstFetch(new Int_(10), new ClassConstFetch(new FullyQualified('SomeClass'), new Identifier('FOOBAR_INT')))])]);\n    }\n    public function getNodeTypes(): array\n    {\n        return [String_::class, Float_::class, Int_::class];\n    }\n    /**\n     * @param String_|Float_|Int_ $node\n     * @return \\PhpParser\\Node\\Expr\\ConstFetch|\\PhpParser\\Node\\Expr\\ClassConstFetch|null\n     */\n    public function refactor(Node $node)\n    {\n        throw new ShouldNotHappenException(sprintf('\"%s\" rule is deprecated, as the scalar value target is too wide and replaces values in unrelated contexts; use a custom rule scoped to the exact context instead', self::class));\n    }\n    /**\n     * @param mixed[] $configuration\n     */\n    public function configure(array $configuration): void\n    {\n    }\n}\n","sourceCodeStart":28,"sourceCodeEnd":55,"githubUrl":"https://github.com/rectorphp/rector/blob/408fcb0ff1833e3d26cbc9b04c23a77565814a16/rules/Transform/Rector/Scalar/ScalarValueToConstFetchRector.php#L28-L55","documentation":"Rector\\Transform\\Rector\\Scalar\\ScalarValueToConstFetchRector is deprecated: refactor() throws Rector\\Exception\\ShouldNotHappenException on the first String_, Float_ or Int_ literal. The rule replaced configured scalar values (e.g. the int 10 with SomeClass::FOOBAR_INT), but a bare literal target is too wide - the same number or string appears in unrelated contexts, so the rewrite corrupts unrelated code. configure() is an empty no-op, so old value mappings are ignored before the throw.","triggerScenarios":"The rule is still referenced in rector.php (withRules() or withConfiguredRule() with an old scalar=>const map, as in the rule's configured code sample) and rector visits any string/float/int literal; refactor() throws on the first literal.","commonSituations":"Configs that centralized 'magic numbers' via rector; rector upgrade breaking CI at the first literal encountered; users assuming their old value mapping still protects them because no config error appears.","solutions":["Remove ScalarValueToConstFetchRector from rector.php.","Replace the meaningful literals manually with the intended class constant / const fetch, reviewing each context.","If automation is needed, write a custom rule that matches the literal only in the exact contexts (specific methods/classes) where the constant applies."],"exampleFix":"// before (rector.php)\n->withConfiguredRule(ScalarValueToConstFetchRector::class, [\n    new ScalarValueToConstFetch(new Int_(10), new ClassConstFetch(new FullyQualified('SomeClass'), new Identifier('FOOBAR_INT'))),\n])\n\n// after (rector.php) - removed\n\n// before            // after\n$limit = 10;         $limit = SomeClass::FOOBAR_INT;","handlingStrategy":"validation","validationCode":"use Rector\\Transform\\Rector\\Scalar\\ScalarValueToConstFetchRector;\n\n$rules = [/* your list */ ScalarValueToConstFetchRector::class];\nif (in_array(ScalarValueToConstFetchRector::class, $rules, true)) {\n    throw new InvalidArgumentException('Rule deprecated; replace literals manually or with a context-scoped custom rule');\n}","typeGuard":null,"tryCatchPattern":"try {\n    exit($rectorApplication->run());\n} catch (\\Rector\\Exception\\ShouldNotHappenException $e) {\n    if (str_contains($e->getMessage(), 'ScalarValueToConstFetchRector')) {\n        fwrite(STDERR, 'Remove the rule; bare-scalar matches hit unrelated contexts.' . PHP_EOL);\n        exit(1);\n    }\n    throw $e;\n}","preventionTips":["Replace magic literals with constants in reviewed edits limited to the files that mean the constant.","If automating, match on parent context (method/class), never on the bare literal - the reason this rule was dropped.","Remember configure() is a no-op here: old ScalarValueToConstFetch mappings are ignored and cannot protect you."],"tags":["rector","php","constants","scalar","transform","deprecated-rule"],"backgroundTag":"deprecated-api-invoked","analyzedSha":"408fcb0ff1833e3d26cbc9b04c23a77565814a16","analyzedAt":"2026-08-21T05:11:02.643Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}