Skip to content

Commit

Permalink
[CodeQuality] Skip not regex on SimplifyRegexPatternRector (#4322)
Browse files Browse the repository at this point in the history
* [CodeQuality] Skip not regex on SimplifyRegexPatternRector

* [CodeQuality] Skip not regex on SimplifyRegexPatternRector

* [CodeQuality] Skip not regex on SimplifyRegexPatternRector
  • Loading branch information
samsonasik committed Jun 22, 2023
1 parent dcca122 commit e80557a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector\Fixture;

class SkipNotRegex
{
public function run($value)
{
echo '[0-9] and \d are a bit different.';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node;
use PhpParser\Node\Scalar\String_;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeNameResolver\Regex\RegexPatternDetector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -30,6 +31,10 @@ final class SimplifyRegexPatternRector extends AbstractRector
'[\r\n\t\f\v ]' => '\s',
];

public function __construct(private readonly RegexPatternDetector $regexPatternDetector)
{
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Simplify regex pattern to known ranges', [
Expand Down Expand Up @@ -70,6 +75,10 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->regexPatternDetector->isRegexPattern($node->value)) {
return null;
}

foreach (self::COMPLEX_PATTERN_TO_SIMPLE as $complexPattern => $simple) {
$originalValue = $node->value;
$simplifiedValue = Strings::replace(
Expand Down

0 comments on commit e80557a

Please sign in to comment.