diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/Fixture/fixture.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/Fixture/fixture.php.inc deleted file mode 100644 index a457be2406a..00000000000 --- a/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,33 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/Fixture/other_delimiter.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/Fixture/other_delimiter.php.inc deleted file mode 100644 index 72d130079b8..00000000000 --- a/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/Fixture/other_delimiter.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/Fixture/pattern_in_constant.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/Fixture/pattern_in_constant.php.inc deleted file mode 100644 index 7ab89835547..00000000000 --- a/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/Fixture/pattern_in_constant.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/Fixture/skip_direct_square_bracket_as_delimiter.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/Fixture/skip_direct_square_bracket_as_delimiter.php.inc deleted file mode 100644 index ec48d5b2bda..00000000000 --- a/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/Fixture/skip_direct_square_bracket_as_delimiter.php.inc +++ /dev/null @@ -1,13 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/Fixture/with_square_bracket_as_delimiter.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/Fixture/with_square_bracket_as_delimiter.php.inc deleted file mode 100644 index 0a4f9014352..00000000000 --- a/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/Fixture/with_square_bracket_as_delimiter.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/SimplifyRegexPatternRectorTest.php b/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/SimplifyRegexPatternRectorTest.php deleted file mode 100644 index 7ad57569f3e..00000000000 --- a/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/SimplifyRegexPatternRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/config/configured_rule.php deleted file mode 100644 index 6bd47f6935d..00000000000 --- a/rules-tests/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector/config/configured_rule.php +++ /dev/null @@ -1,9 +0,0 @@ -withRules([SimplifyRegexPatternRector::class]); diff --git a/rules/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector.php b/rules/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector.php index 4c29d7a9853..6c19a25b21a 100644 --- a/rules/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector.php +++ b/rules/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector.php @@ -4,39 +4,19 @@ namespace Rector\CodeQuality\Rector\FuncCall; -use Nette\Utils\Strings; use PhpParser\Node; use PhpParser\Node\Scalar\String_; -use Rector\NodeNameResolver\Regex\RegexPatternDetector; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see \Rector\Tests\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector\SimplifyRegexPatternRectorTest + * @deprecated This rule is deprecated, as shortening ranges like "[a-zA-Z0-9_]" to "\w" is a matter of personal preference. It can worsen regex readability and is rather a coding standard change. */ -final class SimplifyRegexPatternRector extends AbstractRector +final class SimplifyRegexPatternRector extends AbstractRector implements DeprecatedInterface { - /** - * Using double quote "\d", "\w", "\s" to avoid unescaped issue on scoped build - * Reproduced with php-scoper 0.18.17, @see https://github.com/rectorphp/rector/issues/9395 - * - * @var array - */ - private const array 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", - ]; - - public function __construct( - private readonly RegexPatternDetector $regexPatternDetector - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Simplify regex pattern to known ranges', [ @@ -75,36 +55,11 @@ public function getNodeTypes(): array /** * @param String_ $node */ - public function refactor(Node $node): ?Node + public function refactor(Node $node): ?String_ { - if (! $this->regexPatternDetector->isRegexPattern($node->value)) { - return null; - } - - 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; - } - - if (str_contains($originalValue, '[^' . $complexPattern)) { - continue; - } - - if ($complexPattern === $node->value) { - continue; - } - - $node->value = $simplifiedValue; - return $node; - } - - return null; + throw new ShouldNotHappenException(sprintf( + '"%s" is deprecated, as simplifying regex ranges is a personal preference that can worsen readability', + self::class + )); } } diff --git a/src/Config/Level/CodeQualityLevel.php b/src/Config/Level/CodeQualityLevel.php index 8e68890fb4f..5e0fd9f6a12 100644 --- a/src/Config/Level/CodeQualityLevel.php +++ b/src/Config/Level/CodeQualityLevel.php @@ -43,7 +43,6 @@ use Rector\CodeQuality\Rector\FuncCall\SetTypeToCastRector; use Rector\CodeQuality\Rector\FuncCall\SimplifyFuncGetArgsCountRector; use Rector\CodeQuality\Rector\FuncCall\SimplifyInArrayValuesRector; -use Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector; use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector; use Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector; use Rector\CodeQuality\Rector\FuncCall\SortCallLikeNamedArgsRector; @@ -132,7 +131,6 @@ final class CodeQualityLevel ConsecutiveNullCompareReturnsToNullCoalesceQueueRector::class, UseIdenticalOverEqualWithSameTypeRector::class, SimplifyBoolIdenticalTrueRector::class, - SimplifyRegexPatternRector::class, BooleanNotIdenticalToNotIdenticalRector::class, AndAssignsToSeparateLinesRector::class, CompactToVariablesRector::class, diff --git a/src/NodeNameResolver/Regex/RegexPatternDetector.php b/src/NodeNameResolver/Regex/RegexPatternDetector.php deleted file mode 100644 index 4a9e87dd5a8..00000000000 --- a/src/NodeNameResolver/Regex/RegexPatternDetector.php +++ /dev/null @@ -1,52 +0,0 @@ - - */ - private const array START_AND_END_DELIMITERS = [ - '(' => ')', - '{' => '}', - '[' => ']', - '<' => '>', - ]; - - public function isRegexPattern(string $name): bool - { - if (strlen($name) <= 2) { - return false; - } - - $firstChar = $name[0]; - $lastChar = $name[strlen($name) - 1]; - if ($firstChar !== $lastChar) { - foreach (self::START_AND_END_DELIMITERS as $start => $end) { - if ($firstChar !== $start) { - continue; - } - - if ($lastChar !== $end) { - continue; - } - - return true; - } - - return false; - } - - return in_array($firstChar, self::POSSIBLE_DELIMITERS, true); - } -}