\\w+)$#'; /** * @var \Rector\Core\PhpParser\Node\Value\ValueResolver */ private $valueResolver; public function __construct(\Rector\Core\PhpParser\Node\Value\ValueResolver $valueResolver) { $this->valueResolver = $valueResolver; } public function resolvePatternExpressionWithoutEIfFound(\PhpParser\Node\Expr $expr) : ?\PhpParser\Node\Expr { if ($expr instanceof \PhpParser\Node\Scalar\String_) { $pattern = $this->valueResolver->getValue($expr); if (!\is_string($pattern)) { return null; } $delimiter = $pattern[0]; if (!\is_string($delimiter)) { throw new \Rector\Core\Exception\ShouldNotHappenException(); } /** @var string $modifiers */ $modifiers = \RectorPrefix20210814\Nette\Utils\Strings::after($pattern, $delimiter, -1); if (\strpos($modifiers, 'e') === \false) { return null; } $patternWithoutE = $this->createPatternWithoutE($pattern, $delimiter, $modifiers); return new \PhpParser\Node\Scalar\String_($patternWithoutE); } if ($expr instanceof \PhpParser\Node\Expr\BinaryOp\Concat) { return $this->matchConcat($expr); } return null; } private function createPatternWithoutE(string $pattern, string $delimiter, string $modifiers) : string { $modifiersWithoutE = \RectorPrefix20210814\Nette\Utils\Strings::replace($modifiers, '#e#', ''); return \RectorPrefix20210814\Nette\Utils\Strings::before($pattern, $delimiter, -1) . $delimiter . $modifiersWithoutE; } private function matchConcat(\PhpParser\Node\Expr\BinaryOp\Concat $concat) : ?\PhpParser\Node\Expr { $lastItem = $concat->right; if (!$lastItem instanceof \PhpParser\Node\Scalar\String_) { return null; } $matches = \RectorPrefix20210814\Nette\Utils\Strings::match($lastItem->value, self::LETTER_SUFFIX_REGEX); if (!isset($matches['modifiers'])) { return null; } if (\strpos($matches['modifiers'], 'e') === \false) { return null; } // replace last "e" in the code $lastItem->value = \RectorPrefix20210814\Nette\Utils\Strings::replace($lastItem->value, self::LAST_E_REGEX, '$1$2'); return $concat; } }