2020-03-25 14:15:41 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\Php55;
|
|
|
|
|
|
|
|
use Nette\Utils\Strings;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Concat;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
|
|
|
use Rector\Core\Exception\ShouldNotHappenException;
|
|
|
|
use Rector\Core\PhpParser\Node\Value\ValueResolver;
|
|
|
|
|
|
|
|
final class RegexMatcher
|
|
|
|
{
|
2020-09-23 11:16:40 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
2020-10-30 01:04:33 +07:00
|
|
|
* @see https://regex101.com/r/Ok4wuE/1
|
2020-09-23 11:16:40 +02:00
|
|
|
*/
|
|
|
|
private const LAST_E_REGEX = '#(\w+)?e(\w+)?$#';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
2020-10-30 01:04:33 +07:00
|
|
|
* @see https://regex101.com/r/2NWVwT/1
|
2020-09-23 11:16:40 +02:00
|
|
|
*/
|
|
|
|
private const LETTER_SUFFIX_REGEX = '#(?<modifiers>\w+)$#';
|
|
|
|
|
2020-03-25 14:15:41 +01:00
|
|
|
/**
|
|
|
|
* @var ValueResolver
|
|
|
|
*/
|
|
|
|
private $valueResolver;
|
|
|
|
|
|
|
|
public function __construct(ValueResolver $valueResolver)
|
|
|
|
{
|
|
|
|
$this->valueResolver = $valueResolver;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function resolvePatternExpressionWithoutEIfFound(Expr $expr): ?Expr
|
|
|
|
{
|
|
|
|
if ($expr instanceof String_) {
|
|
|
|
$pattern = $this->valueResolver->getValue($expr);
|
|
|
|
|
|
|
|
if (! is_string($pattern)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$delimiter = $pattern[0];
|
|
|
|
if (! is_string($delimiter)) {
|
|
|
|
throw new ShouldNotHappenException();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var string $modifiers */
|
|
|
|
$modifiers = Strings::after($pattern, $delimiter, -1);
|
|
|
|
if (! Strings::contains($modifiers, 'e')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$patternWithoutE = $this->createPatternWithoutE($pattern, $delimiter, $modifiers);
|
|
|
|
return new String_($patternWithoutE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($expr instanceof Concat) {
|
|
|
|
return $this->matchConcat($expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createPatternWithoutE(string $pattern, string $delimiter, string $modifiers): string
|
|
|
|
{
|
|
|
|
$modifiersWithoutE = Strings::replace($modifiers, '#e#');
|
|
|
|
|
|
|
|
return Strings::before($pattern, $delimiter, -1) . $delimiter . $modifiersWithoutE;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function matchConcat(Expr $expr): ?Expr
|
|
|
|
{
|
|
|
|
$lastItem = $expr->right;
|
|
|
|
if (! $lastItem instanceof String_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-23 11:16:40 +02:00
|
|
|
$matches = Strings::match($lastItem->value, self::LETTER_SUFFIX_REGEX);
|
2020-03-25 14:15:41 +01:00
|
|
|
if (! isset($matches['modifiers'])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! Strings::contains($matches['modifiers'], 'e')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace last "e" in the code
|
2020-09-23 11:16:40 +02:00
|
|
|
$lastItem->value = Strings::replace($lastItem->value, self::LAST_E_REGEX, '$1$2');
|
2020-03-25 14:15:41 +01:00
|
|
|
|
|
|
|
return $expr;
|
|
|
|
}
|
|
|
|
}
|