{"record":{"id":"cbea45e1628ebe57","repo":"rectorphp/rector","slug":"s-is-deprecated-as-converting-string-interpola","errorCode":null,"errorMessage":"\"%s\" is deprecated, as converting string interpolation to sprintf() is a personal preference that can worsen readability","messagePattern":"\"(.+?)\" is deprecated, as converting string interpolation to sprintf\\(\\) is a personal preference that can worsen readability","errorType":"exception","errorClass":"ShouldNotHappenException","httpStatus":null,"severity":"error","filePath":"rules/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector.php","lineNumber":56,"sourceCode":"echo sprintf('Unsupported format %s - use another', $format);\n\necho 'Try ' . $allowed;\nCODE_SAMPLE\n, [self::ALWAYS => \\false])]);\n    }\n    /**\n     * @return array<class-string<Node>>\n     */\n    public function getNodeTypes(): array\n    {\n        return [InterpolatedString::class];\n    }\n    /**\n     * @param InterpolatedString $node\n     */\n    public function refactor(Node $node): ?Node\n    {\n        throw new ShouldNotHappenException(sprintf('\"%s\" is deprecated, as converting string interpolation to sprintf() is a personal preference that can worsen readability', self::class));\n    }\n}\n","sourceCodeStart":38,"sourceCodeEnd":59,"githubUrl":"https://github.com/rectorphp/rector/blob/408fcb0ff1833e3d26cbc9b04c23a77565814a16/rules/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector.php#L38-L59","documentation":"EncapsedStringsToSprintfRector used to rewrite interpolated strings like \"Hello $name, you are $age\" into sprintf('Hello %s, you are %d', $name, $age). It is deprecated because that conversion is a personal preference that often worsens readability — interpolation is the idiomatic PHP style. refactor() now throws ShouldNotHappenException on every InterpolatedString node; the rule performs no rewrite and serves only to abort stale configurations.","triggerScenarios":"Registering rules/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector.php in config and running rector over any file containing a double-quoted string with variables. The throw fires at rules/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector.php:56 on the first interpolated string.","commonSituations":"Configs that adopted this rule for translation-tooling workflows (some translation extractors prefer sprintf) still reference it after a Rector upgrade. Because interpolated strings are everywhere, the exception appears on the first analysed file and looks like an internal Rector failure rather than a config issue.","solutions":["Remove EncapsedStringsToSprintfRector::class from rector.php / imported sets and re-run dry-run","If your translation pipeline genuinely needs sprintf(), convert the translatable strings manually or scope a custom rule to that one directory","Keep normal strings interpolated — it is the clearer, idiomatic form","Check shared set bundles your config imports for lingering references"],"exampleFix":"// before (rector.php)\n->withRules([\n    \\Rector\\CodingStyle\\Rector\\Encapsed\\EncapsedStringsToSprintfRector::class,\n]);\n// code:\n$message = \"Hello $name, you are $age\";\n\n// after (rector.php)\n// rule removed\n// code kept as interpolation; convert by hand only where tools require it:\n$message = sprintf('Hello %s, you are %d', $name, $age);","handlingStrategy":"validation","validationCode":"$deprecated = ['EncapsedStringsToSprintfRector'];\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":["Keep user-facing strings interpolated; convert to sprintf() only where tooling (translation extractors) needs it","Scope any sprintf conversion to the specific directory via a custom rule or manual pass","Prune deprecated rules during each Rector upgrade","Dry-run in CI on representative files to catch guard throws"],"tags":["rector","php","deprecated-rule","string-interpolation","sprintf","code-style"],"backgroundTag":"rector-deprecated-rule","analyzedSha":"408fcb0ff1833e3d26cbc9b04c23a77565814a16","analyzedAt":"2026-08-21T05:11:02.643Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}