2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-03-11 08:57:56 +00:00
|
|
|
|
|
|
|
namespace Rector\CodeQuality\Rector\FuncCall;
|
|
|
|
|
2019-03-11 12:28:41 +00:00
|
|
|
use Nette\Utils\Strings;
|
2019-03-11 08:57:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2019-03-31 11:51:34 +00:00
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Php\Regex\RegexPatternArgumentManipulator;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-03-11 08:57:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see http://php.net/manual/en/function.preg-match.php#105924
|
2019-09-04 12:10:29 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector\SimplifyRegexPatternRectorTest
|
2019-03-11 08:57:56 +00:00
|
|
|
*/
|
|
|
|
final class SimplifyRegexPatternRector extends AbstractRector
|
|
|
|
{
|
2019-03-11 12:28:41 +00:00
|
|
|
/**
|
2021-02-23 01:25:34 +00:00
|
|
|
* @var array<string, string>
|
2019-03-11 12:28:41 +00:00
|
|
|
*/
|
2020-02-18 22:09:25 +00:00
|
|
|
private const COMPLEX_PATTERN_TO_SIMPLE = [
|
2019-03-11 12:28:41 +00:00
|
|
|
'[0-9]' => '\d',
|
|
|
|
'[a-zA-Z0-9_]' => '\w',
|
2019-03-31 11:34:42 +00:00
|
|
|
'[A-Za-z0-9_]' => '\w',
|
|
|
|
'[0-9a-zA-Z_]' => '\w',
|
|
|
|
'[0-9A-Za-z_]' => '\w',
|
2019-03-11 12:28:41 +00:00
|
|
|
'[\r\n\t\f\v ]' => '\s',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var RegexPatternArgumentManipulator
|
|
|
|
*/
|
|
|
|
private $regexPatternArgumentManipulator;
|
|
|
|
|
|
|
|
public function __construct(RegexPatternArgumentManipulator $regexPatternArgumentManipulator)
|
|
|
|
{
|
|
|
|
$this->regexPatternArgumentManipulator = $regexPatternArgumentManipulator;
|
|
|
|
}
|
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2019-03-11 08:57:56 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition('Simplify regex pattern to known ranges', [
|
2019-03-11 08:57:56 +00:00
|
|
|
new CodeSample(
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2019-03-11 08:57:56 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run($value)
|
|
|
|
{
|
|
|
|
preg_match('#[a-zA-Z0-9+]#', $value);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2019-03-11 08:57:56 +00:00
|
|
|
,
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2019-03-11 08:57:56 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run($value)
|
|
|
|
{
|
|
|
|
preg_match('#[\w\d+]#', $value);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2019-03-11 08:57:56 +00:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-03-11 08:57:56 +00:00
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
2019-03-31 11:51:34 +00:00
|
|
|
return [FuncCall::class, StaticCall::class];
|
2019-03-11 08:57:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-31 11:51:34 +00:00
|
|
|
* @param FuncCall|StaticCall $node
|
2019-03-11 08:57:56 +00:00
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
2019-03-31 11:51:34 +00:00
|
|
|
$patterns = $this->regexPatternArgumentManipulator->matchCallArgumentWithRegexPattern($node);
|
2020-05-03 11:18:59 +00:00
|
|
|
if ($patterns === []) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-03-11 08:57:56 +00:00
|
|
|
|
2019-03-31 11:51:34 +00:00
|
|
|
foreach ($patterns as $pattern) {
|
2020-02-18 22:09:25 +00:00
|
|
|
foreach (self::COMPLEX_PATTERN_TO_SIMPLE as $complexPattern => $simple) {
|
2019-03-31 11:51:34 +00:00
|
|
|
$pattern->value = Strings::replace(
|
|
|
|
$pattern->value,
|
|
|
|
'#' . preg_quote($complexPattern, '#') . '#',
|
|
|
|
$simple
|
|
|
|
);
|
|
|
|
}
|
2019-03-11 12:28:41 +00:00
|
|
|
}
|
2019-03-11 08:57:56 +00:00
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|