diff --git a/rules-tests/Php55/Rector/FuncCall/PregReplaceEModifierRector/Fixture/interpolated_pattern.php.inc b/rules-tests/Php55/Rector/FuncCall/PregReplaceEModifierRector/Fixture/interpolated_pattern.php.inc new file mode 100644 index 00000000000..7e8b9a6f4bd --- /dev/null +++ b/rules-tests/Php55/Rector/FuncCall/PregReplaceEModifierRector/Fixture/interpolated_pattern.php.inc @@ -0,0 +1,41 @@ + +----- + diff --git a/rules-tests/Php55/Rector/FuncCall/PregReplaceEModifierRector/Fixture/skip_interpolated_pattern_no_e_modifier.php.inc b/rules-tests/Php55/Rector/FuncCall/PregReplaceEModifierRector/Fixture/skip_interpolated_pattern_no_e_modifier.php.inc new file mode 100644 index 00000000000..699cc1b7569 --- /dev/null +++ b/rules-tests/Php55/Rector/FuncCall/PregReplaceEModifierRector/Fixture/skip_interpolated_pattern_no_e_modifier.php.inc @@ -0,0 +1,17 @@ +value; - - $delimiter = $pattern[0]; - $delimiter = match ($delimiter) { - '(' => ')', - '{' => '}', - '[' => ']', - '<' => '>', - default => $delimiter - }; + $delimiter = $this->resolveClosingDelimiter($pattern); $modifiers = $this->resolveModifiers((string) Strings::after($pattern, $delimiter, -1)); if (! \str_contains($modifiers, 'e')) { @@ -55,9 +49,50 @@ public function resolvePatternExpressionWithoutEIfFound(Expr $expr): Concat|Stri return $this->matchConcat($expr); } + if ($expr instanceof InterpolatedString) { + return $this->matchInterpolatedString($expr); + } + return null; } + private function resolveClosingDelimiter(string $pattern): string + { + $delimiter = $pattern[0]; + return match ($delimiter) { + '(' => ')', + '{' => '}', + '[' => ']', + '<' => '>', + default => $delimiter + }; + } + + private function matchInterpolatedString(InterpolatedString $interpolatedString): ?InterpolatedString + { + $firstPart = $interpolatedString->parts[0] ?? null; + if (! $firstPart instanceof InterpolatedStringPart) { + return null; + } + + $lastPart = $interpolatedString->parts[array_key_last($interpolatedString->parts)]; + if (! $lastPart instanceof InterpolatedStringPart) { + return null; + } + + $delimiter = $this->resolveClosingDelimiter($firstPart->value); + $pattern = $lastPart->value; + + $modifiers = $this->resolveModifiers((string) Strings::after($pattern, $delimiter, -1)); + if (! \str_contains($modifiers, 'e')) { + return null; + } + + $lastPart->value = $this->createPatternWithoutE($pattern, $delimiter, $modifiers); + + return $interpolatedString; + } + private function resolveModifiers(string $modifiersCandidate): string { $modifiers = '';