2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\FuncCall;
|
2019-03-11 08:57:56 +00:00
|
|
|
|
2024-03-01 20:02:28 +00:00
|
|
|
use RectorPrefix202403\Nette\Utils\Strings;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2023-06-07 12:49:26 +00:00
|
|
|
use PhpParser\Node\Scalar\String_;
|
2023-06-22 19:58:23 +00:00
|
|
|
use Rector\NodeNameResolver\Regex\RegexPatternDetector;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-03-11 08:57:56 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog 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
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class SimplifyRegexPatternRector extends AbstractRector
|
2019-03-11 08:57:56 +00:00
|
|
|
{
|
2023-06-22 19:58:23 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\NodeNameResolver\Regex\RegexPatternDetector
|
|
|
|
*/
|
|
|
|
private $regexPatternDetector;
|
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
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
private const COMPLEX_PATTERN_TO_SIMPLE = ['[0-9]' => '\\d', '[a-zA-Z0-9_]' => '\\w', '[A-Za-z0-9_]' => '\\w', '[0-9a-zA-Z_]' => '\\w', '[0-9A-Za-z_]' => '\\w', '[\\r\\n\\t\\f\\v ]' => '\\s'];
|
2023-06-22 19:58:23 +00:00
|
|
|
public function __construct(RegexPatternDetector $regexPatternDetector)
|
|
|
|
{
|
|
|
|
$this->regexPatternDetector = $regexPatternDetector;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-03-11 08:57:56 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Simplify regex pattern to known ranges', [new CodeSample(<<<'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
|
2021-05-09 20:15:43 +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
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
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
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-03-11 08:57:56 +00:00
|
|
|
{
|
2023-06-07 12:49:26 +00:00
|
|
|
return [String_::class];
|
2019-03-11 08:57:56 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-06-07 12:49:26 +00:00
|
|
|
* @param String_ $node
|
2019-03-11 08:57:56 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-03-11 08:57:56 +00:00
|
|
|
{
|
2023-06-22 19:58:23 +00:00
|
|
|
if (!$this->regexPatternDetector->isRegexPattern($node->value)) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-06-07 12:49:26 +00:00
|
|
|
foreach (self::COMPLEX_PATTERN_TO_SIMPLE as $complexPattern => $simple) {
|
|
|
|
$originalValue = $node->value;
|
|
|
|
$simplifiedValue = Strings::replace($node->value, '#' . \preg_quote($complexPattern, '#') . '#', $simple);
|
|
|
|
if ($originalValue === $simplifiedValue) {
|
|
|
|
continue;
|
2019-03-31 11:51:34 +00:00
|
|
|
}
|
2024-01-23 16:37:59 +00:00
|
|
|
if (\strpos($originalValue, '[^' . $complexPattern) !== \false) {
|
2024-01-23 15:52:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-06-07 12:49:26 +00:00
|
|
|
$node->value = $simplifiedValue;
|
2021-12-28 14:03:15 +00:00
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
return null;
|
2019-03-11 08:57:56 +00:00
|
|
|
}
|
|
|
|
}
|