Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

63 changes: 9 additions & 54 deletions rules/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
*/
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', [
Expand Down Expand Up @@ -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
));
}
}
2 changes: 0 additions & 2 deletions src/Config/Level/CodeQualityLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -132,7 +131,6 @@ final class CodeQualityLevel
ConsecutiveNullCompareReturnsToNullCoalesceQueueRector::class,
UseIdenticalOverEqualWithSameTypeRector::class,
SimplifyBoolIdenticalTrueRector::class,
SimplifyRegexPatternRector::class,
BooleanNotIdenticalToNotIdenticalRector::class,
AndAssignsToSeparateLinesRector::class,
CompactToVariablesRector::class,
Expand Down
52 changes: 0 additions & 52 deletions src/NodeNameResolver/Regex/RegexPatternDetector.php

This file was deleted.

Loading