From 24feb27f3db0ef0ae5c5a910d51ed66be9cde8cf Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 2 Aug 2026 23:55:25 +0200 Subject: [PATCH] [CodingStyle] Deprecate EncapsedStringsToSprintfRector --- .../AlwaysSprintfTest.php | 28 --- .../EncapsedStringsToSprintfRectorTest.php | 28 --- ...s_to_sprintf_should_escape_percent.php.inc | 27 --- .../Fixture/include_numbers.php.inc | 27 --- .../Fixture/int_type.php.inc | 51 ----- .../Fixture/just_value.php.inc | 29 --- .../Fixture/newline.php.inc | 31 --- .../Fixture/numberz.php.inc | 27 --- .../Fixture/only_three_concat.php.inc | 29 --- .../Fixture/prefixed_eol.php.inc | 27 --- .../Fixture/single_encapsed.php.inc | 31 --- .../Fixture/skip-hexa-chars.php.inc | 5 - .../Fixture/skip_in_heredoc.php.inc | 16 -- .../Fixture/skip_prefixed_multi_eol.php.inc | 11 - .../Fixture/translation_function.php.inc | 27 --- ...s_to_sprintf_should_escape_percent.php.inc | 27 --- .../include_numbers.php.inc | 27 --- .../FixtureAlwaysSprintf/just_value.php.inc | 29 --- .../FixtureAlwaysSprintf/newline.php.inc | 31 --- .../FixtureAlwaysSprintf/numberz.php.inc | 27 --- .../only_three_concat.php.inc | 29 --- .../FixtureAlwaysSprintf/prefixed_eol.php.inc | 27 --- .../single_encapsed.php.inc | 31 --- .../skip_in_heredoc.php.inc | 16 -- .../skip_prefixed_multi_eol.php.inc | 11 - .../config/always_sprintf.php | 13 -- .../config/configured_rule.php | 9 - .../EncapsedStringsToSprintfRector.php | 213 +----------------- src/Config/Level/CodingStyleLevel.php | 2 - .../EncapsedStringsInsideDeadInstanceTest.php | 31 --- .../Issues/Issue6481/Fixture/fixture.php.inc | 33 --- .../Issue6481/config/configured_rule.php | 10 - 32 files changed, 8 insertions(+), 952 deletions(-) delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/AlwaysSprintfTest.php delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/EncapsedStringsToSprintfRectorTest.php delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/encapsed_strings_to_sprintf_should_escape_percent.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/include_numbers.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/int_type.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/just_value.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/newline.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/numberz.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/only_three_concat.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/prefixed_eol.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/single_encapsed.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/skip-hexa-chars.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/skip_in_heredoc.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/skip_prefixed_multi_eol.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/translation_function.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/encapsed_strings_to_sprintf_should_escape_percent.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/include_numbers.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/just_value.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/newline.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/numberz.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/only_three_concat.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/prefixed_eol.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/single_encapsed.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/skip_in_heredoc.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/skip_prefixed_multi_eol.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/config/always_sprintf.php delete mode 100644 rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/config/configured_rule.php delete mode 100644 tests/Issues/Issue6481/EncapsedStringsInsideDeadInstanceTest.php delete mode 100644 tests/Issues/Issue6481/Fixture/fixture.php.inc delete mode 100644 tests/Issues/Issue6481/config/configured_rule.php diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/AlwaysSprintfTest.php b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/AlwaysSprintfTest.php deleted file mode 100644 index 6bbb14afd98..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/AlwaysSprintfTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/FixtureAlwaysSprintf'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/always_sprintf.php'; - } -} diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/EncapsedStringsToSprintfRectorTest.php b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/EncapsedStringsToSprintfRectorTest.php deleted file mode 100644 index 803c39e6d24..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/EncapsedStringsToSprintfRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/encapsed_strings_to_sprintf_should_escape_percent.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/encapsed_strings_to_sprintf_should_escape_percent.php.inc deleted file mode 100644 index 5f8a0c13b42..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/encapsed_strings_to_sprintf_should_escape_percent.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/include_numbers.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/include_numbers.php.inc deleted file mode 100644 index a9513793bc3..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/include_numbers.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/int_type.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/int_type.php.inc deleted file mode 100644 index 51e54602dd0..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/int_type.php.inc +++ /dev/null @@ -1,51 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/just_value.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/just_value.php.inc deleted file mode 100644 index 81bde398110..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/just_value.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/newline.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/newline.php.inc deleted file mode 100644 index 16fd8582ee1..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/newline.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/numberz.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/numberz.php.inc deleted file mode 100644 index 4c7187713ec..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/numberz.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/only_three_concat.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/only_three_concat.php.inc deleted file mode 100644 index 7ef090eb72d..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/only_three_concat.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/prefixed_eol.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/prefixed_eol.php.inc deleted file mode 100644 index 939982a9ef0..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/prefixed_eol.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/single_encapsed.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/single_encapsed.php.inc deleted file mode 100644 index f4d8035924f..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/single_encapsed.php.inc +++ /dev/null @@ -1,31 +0,0 @@ -format}"; - } -} - -?> ------ -format; - } -} - -?> diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/skip-hexa-chars.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/skip-hexa-chars.php.inc deleted file mode 100644 index 51b1af58fb8..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/Fixture/skip-hexa-chars.php.inc +++ /dev/null @@ -1,5 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/encapsed_strings_to_sprintf_should_escape_percent.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/encapsed_strings_to_sprintf_should_escape_percent.php.inc deleted file mode 100644 index 7c037508439..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/encapsed_strings_to_sprintf_should_escape_percent.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/include_numbers.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/include_numbers.php.inc deleted file mode 100644 index b898862884f..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/include_numbers.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/just_value.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/just_value.php.inc deleted file mode 100644 index c979c449f3d..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/just_value.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/newline.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/newline.php.inc deleted file mode 100644 index 36ba17f9110..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/newline.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/numberz.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/numberz.php.inc deleted file mode 100644 index bf8c1dcc52f..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/numberz.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/only_three_concat.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/only_three_concat.php.inc deleted file mode 100644 index 33d5c721a1f..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/only_three_concat.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/prefixed_eol.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/prefixed_eol.php.inc deleted file mode 100644 index 7ac3fd60568..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/prefixed_eol.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/single_encapsed.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/single_encapsed.php.inc deleted file mode 100644 index bad0396ab51..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/single_encapsed.php.inc +++ /dev/null @@ -1,31 +0,0 @@ -format}"; - } -} - -?> ------ -format; - } -} - -?> diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/skip_in_heredoc.php.inc b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/skip_in_heredoc.php.inc deleted file mode 100644 index afd560059c8..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/FixtureAlwaysSprintf/skip_in_heredoc.php.inc +++ /dev/null @@ -1,16 +0,0 @@ -ruleWithConfiguration(EncapsedStringsToSprintfRector::class, [ - EncapsedStringsToSprintfRector::ALWAYS => true, - ]); -}; diff --git a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/config/configured_rule.php b/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/config/configured_rule.php deleted file mode 100644 index 0df51bedde7..00000000000 --- a/rules-tests/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector/config/configured_rule.php +++ /dev/null @@ -1,9 +0,0 @@ -withRules([EncapsedStringsToSprintfRector::class]); diff --git a/rules/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector.php b/rules/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector.php index dd176f40d0c..45817086be4 100644 --- a/rules/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector.php +++ b/rules/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector.php @@ -4,63 +4,30 @@ namespace Rector\CodingStyle\Rector\Encapsed; -use Nette\Utils\Strings; use PhpParser\Node; -use PhpParser\Node\Arg; -use PhpParser\Node\Expr; -use PhpParser\Node\Expr\BinaryOp\Concat; -use PhpParser\Node\Expr\ConstFetch; -use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Expr\Variable; -use PhpParser\Node\InterpolatedStringPart; -use PhpParser\Node\Name; use PhpParser\Node\Scalar\InterpolatedString; -use PhpParser\Node\Scalar\String_; -use PHPStan\Type\Type; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; use Rector\Contract\Rector\ConfigurableRectorInterface; -use Rector\NodeTypeResolver\Node\AttributeKey; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see \Rector\Tests\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector\EncapsedStringsToSprintfRectorTest + * @deprecated This rule is deprecated, as turning "{$string}" interpolation into sprintf() or concat is a matter of personal preference. It can worsen readability and is rather a coding standard change. */ -final class EncapsedStringsToSprintfRector extends AbstractRector implements ConfigurableRectorInterface +final class EncapsedStringsToSprintfRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface { /** * @api */ public const string ALWAYS = 'always'; - /** - * @var array>> - */ - private const array FORMAT_SPECIFIERS = [ - '%s' => ['PHPStan\Type\StringType'], - '%d' => [ - 'PHPStan\Type\Constant\ConstantIntegerType', - 'PHPStan\Type\IntegerRangeType', - 'PHPStan\Type\IntegerType', - ], - ]; - - private bool $always = false; - - private string $sprintfFormat = ''; - - /** - * @var Expr[] - */ - private array $argumentVariables = []; - /** * @param array $configuration */ public function configure(array $configuration): void { - $this->always = $configuration[self::ALWAYS] ?? false; - } public function getRuleDefinition(): RuleDefinition @@ -85,23 +52,6 @@ public function getRuleDefinition(): RuleDefinition self::ALWAYS => false, ] ), - new ConfiguredCodeSample( - <<<'CODE_SAMPLE' -echo "Unsupported format {$format} - use another"; - -echo "Try {$allowed}"; -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -echo sprintf('Unsupported format %s - use another', $format); - -echo sprintf('Try %s', $allowed); -CODE_SAMPLE - , - [ - self::ALWAYS => true, - ] - ), ] ); } @@ -119,156 +69,9 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($this->shouldSkip($node)) { - return null; - } - - $this->sprintfFormat = ''; - $this->argumentVariables = []; - - foreach ($node->parts as $part) { - if ($part instanceof InterpolatedStringPart) { - $this->collectEncapsedStringPart($part); - } else { - $this->collectExpr($part); - } - } - - return $this->createSprintfFuncCallOrConcat($this->sprintfFormat, $this->argumentVariables); - } - - private function shouldSkip(InterpolatedString $interpolatedString): bool - { - if ($interpolatedString->hasAttribute(AttributeKey::DOC_LABEL)) { - return true; - } - - foreach ($interpolatedString->parts as $part) { - if (! $part instanceof InterpolatedStringPart) { - continue; - } - - if ($this->containsControlASCIIChar($part->value)) { - return true; - } - } - - return false; - } - - private function collectEncapsedStringPart(InterpolatedStringPart $interpolatedStringPart): void - { - $stringValue = $interpolatedStringPart->value; - if ($stringValue === "\n") { - $this->argumentVariables[] = new ConstFetch(new Name('PHP_EOL')); - $this->sprintfFormat .= '%s'; - return; - } - - $this->sprintfFormat .= Strings::replace($stringValue, '#%#', '%%'); - } - - private function collectExpr(Expr $expr): void - { - $type = $this->nodeTypeResolver->getType($expr); - $found = false; - - foreach (self::FORMAT_SPECIFIERS as $key => $types) { - if (in_array($type::class, $types, true)) { - $this->sprintfFormat .= $key; - $found = true; - break; - } - } - - if (! $found) { - $this->sprintfFormat .= '%s'; - } - - // remove: ${wrap} → $wrap - if ($expr instanceof Variable) { - $expr->setAttribute(AttributeKey::ORIGINAL_NODE, null); - } - - $this->argumentVariables[] = $expr; - } - - /** - * @param Expr[] $argumentVariables - */ - private function createSprintfFuncCallOrConcat(string $mask, array $argumentVariables): Concat|FuncCall|Expr|null - { - $bareMask = str_repeat('%s', count($argumentVariables)); - - if ($mask === $bareMask) { - if (count($argumentVariables) === 1) { - return $argumentVariables[0]; - } - - return $this->nodeFactory->createConcat($argumentVariables); - } - - if (! $this->always) { - $singleValueConcat = $this->createSingleValueEdgeConcat($argumentVariables, $mask); - if ($singleValueConcat instanceof Concat) { - return $singleValueConcat; - } - } - - // checks for windows or linux line ending. \n is contained in both. - if (\str_contains($mask, "\n")) { - return null; - } - - $string = $this->createString($mask); - - $arguments = [new Arg($string)]; - foreach ($argumentVariables as $argumentVariable) { - $arguments[] = new Arg($argumentVariable); - } - - return new FuncCall(new Name('sprintf'), $arguments); - } - - /** - * @param Expr[] $argumentVariables - */ - private function createSingleValueEdgeConcat(array $argumentVariables, string $mask): ?Concat - { - if (count($argumentVariables) !== 1) { - return null; - } - - if (substr_count($mask, '%s') !== 1 && substr_count($mask, '%d') !== 1) { - return null; - } - - $cleanMask = Strings::replace($mask, '#\%\%#', '%'); - - if (str_ends_with($mask, '%s') || str_ends_with($mask, '%d')) { - $bareString = new String_(substr($cleanMask, 0, -2)); - return new Concat($bareString, $argumentVariables[0]); - } - - if (str_starts_with($mask, '%s') || str_starts_with($mask, '%d')) { - $bareString = new String_(substr($cleanMask, 2)); - return new Concat($argumentVariables[0], $bareString); - } - - return null; - } - - private function createString(string $value): String_ - { - $kind = str_contains($value, "'") ? String_::KIND_DOUBLE_QUOTED : String_::KIND_SINGLE_QUOTED; - - return new String_($value, [ - 'kind' => $kind, - ]); - } - - private function containsControlASCIIChar(string $content): bool - { - return (bool) Strings::match($content, '#[\x00-\x08\x0B\x0C\x0E-\x1F]#'); + throw new ShouldNotHappenException(sprintf( + '"%s" is deprecated, as converting string interpolation to sprintf() is a personal preference that can worsen readability', + self::class + )); } } diff --git a/src/Config/Level/CodingStyleLevel.php b/src/Config/Level/CodingStyleLevel.php index d5ffff62baf..8ee4b4507ec 100644 --- a/src/Config/Level/CodingStyleLevel.php +++ b/src/Config/Level/CodingStyleLevel.php @@ -13,7 +13,6 @@ use Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector; use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector; use Rector\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector; -use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector; use Rector\CodingStyle\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector; use Rector\CodingStyle\Rector\FuncCall\CallUserFuncArrayToVariadicRector; use Rector\CodingStyle\Rector\FuncCall\CallUserFuncToMethodCallRector; @@ -67,7 +66,6 @@ final class CodingStyleLevel StringClassNameToClassConstantRector::class, CatchExceptionNameMatchingTypeRector::class, SplitDoubleAssignRector::class, - EncapsedStringsToSprintfRector::class, WrapEncapsedVariableInCurlyBracesRector::class, NewlineBeforeNewAssignSetRector::class, MakeInheritedMethodVisibilitySameAsParentRector::class, diff --git a/tests/Issues/Issue6481/EncapsedStringsInsideDeadInstanceTest.php b/tests/Issues/Issue6481/EncapsedStringsInsideDeadInstanceTest.php deleted file mode 100644 index 2ca647fc427..00000000000 --- a/tests/Issues/Issue6481/EncapsedStringsInsideDeadInstanceTest.php +++ /dev/null @@ -1,31 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/tests/Issues/Issue6481/Fixture/fixture.php.inc b/tests/Issues/Issue6481/Fixture/fixture.php.inc deleted file mode 100644 index 2b725a6caca..00000000000 --- a/tests/Issues/Issue6481/Fixture/fixture.php.inc +++ /dev/null @@ -1,33 +0,0 @@ - ------ - diff --git a/tests/Issues/Issue6481/config/configured_rule.php b/tests/Issues/Issue6481/config/configured_rule.php deleted file mode 100644 index 604c7750d2c..00000000000 --- a/tests/Issues/Issue6481/config/configured_rule.php +++ /dev/null @@ -1,10 +0,0 @@ -withRules([EncapsedStringsToSprintfRector::class, RemoveDeadInstanceOfRector::class]);