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
2 changes: 0 additions & 2 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
0,
\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE,
\Rector\NodeTypeResolver\Node\AttributeKey::REPRINT_RAW_VALUE,
\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::ORIGINAL_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::IS_UNREACHABLE,
\Rector\NodeTypeResolver\Node\AttributeKey::PHP_DOC_INFO,
Expand All @@ -67,7 +66,6 @@
0,
\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE,
\Rector\NodeTypeResolver\Node\AttributeKey::REPRINT_RAW_VALUE,
\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::ORIGINAL_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::IS_UNREACHABLE,
\Rector\NodeTypeResolver\Node\AttributeKey::PHP_DOC_INFO,
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -725,3 +725,7 @@ parameters:
-
message: '#Offset 0 does not exist on array<PhpParser\\Node\\Stmt>\|null#'
path: rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php

# resolve before Rector 1.0
- '#Call to deprecated method findParentType#'
- '#Call to deprecated method findParentByTypes#'
5 changes: 5 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
__DIR__ . '/packages/Testing/PHPUnit/Behavior/MovingFilesTrait.php',
],

// avoid simplifying itself
\Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector::class => [
__DIR__ . '/rules/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector.php',
],

// race condition with stmts aware patch and PHPStan type
\Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector::class => [
__DIR__ . '/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\Php73\Rector\FuncCall\RegexDashEscapeRector\Fixture;

final class SkipHeredoc
{
const COMPAT_PATTERN = <<<'CODE_SAMPLE'
preg_match('#[\w-()]#')
CODE_SAMPLE;
}
46 changes: 13 additions & 33 deletions rules/CodeQuality/Rector/FuncCall/SimplifyRegexPatternRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use Rector\Core\Php\Regex\RegexPatternArgumentManipulator;
use PhpParser\Node\Scalar\String_;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -32,11 +30,6 @@ final class SimplifyRegexPatternRector extends AbstractRector
'[\r\n\t\f\v ]' => '\s',
];

public function __construct(
private readonly RegexPatternArgumentManipulator $regexPatternArgumentManipulator
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Simplify regex pattern to known ranges', [
Expand Down Expand Up @@ -69,40 +62,27 @@ public function run($value)
*/
public function getNodeTypes(): array
{
return [FuncCall::class, StaticCall::class];
return [String_::class];
}

/**
* @param FuncCall|StaticCall $node
* @param String_ $node
*/
public function refactor(Node $node): ?Node
{
$patterns = $this->regexPatternArgumentManipulator->matchCallArgumentWithRegexPattern($node);
if ($patterns === []) {
return null;
}

$hasChanged = false;
foreach (self::COMPLEX_PATTERN_TO_SIMPLE as $complexPattern => $simple) {
$originalValue = $node->value;
$simplifiedValue = Strings::replace(
$node->value,
'#' . preg_quote($complexPattern, '#') . '#',
$simple
);

foreach ($patterns as $pattern) {
foreach (self::COMPLEX_PATTERN_TO_SIMPLE as $complexPattern => $simple) {
$originalValue = $pattern->value;
$simplifiedValue = Strings::replace(
$pattern->value,
'#' . preg_quote($complexPattern, '#') . '#',
$simple
);

if ($originalValue === $simplifiedValue) {
continue;
}

$pattern->value = $simplifiedValue;
$hasChanged = true;
if ($originalValue === $simplifiedValue) {
continue;
}
}

if ($hasChanged) {
$node->value = $simplifiedValue;
return $node;
}

Expand Down
52 changes: 16 additions & 36 deletions rules/Php73/Rector/FuncCall/RegexDashEscapeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Scalar\String_;
use Rector\Core\Php\Regex\RegexPatternArgumentManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StringUtils;
use Rector\Core\ValueObject\PhpVersionFeature;
Expand Down Expand Up @@ -44,13 +41,6 @@ final class RegexDashEscapeRector extends AbstractRector implements MinPhpVersio
*/
private const RIGHT_HAND_UNESCAPED_DASH_REGEX = '#(?<!\[)(?<!\\\\)-(\\\\(w|s|d)[.*]?)\]#i';

private bool $hasChanged = false;

public function __construct(
private readonly RegexPatternArgumentManipulator $regexPatternArgumentManipulator
) {
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ESCAPE_DASH_IN_REGEX;
Expand All @@ -76,51 +66,41 @@ public function getRuleDefinition(): RuleDefinition
*/
public function getNodeTypes(): array
{
return [FuncCall::class, StaticCall::class];
return [String_::class];
}

/**
* @param FuncCall|StaticCall $node
* @param String_ $node
*/
public function refactor(Node $node): ?Node
{
$regexArguments = $this->regexPatternArgumentManipulator->matchCallArgumentWithRegexPattern($node);
if ($regexArguments === []) {
$stringKind = $node->getAttribute(AttributeKey::KIND);
if (in_array($stringKind, [String_::KIND_HEREDOC, String_::KIND_NOWDOC], true)) {
return null;
}

foreach ($regexArguments as $regexArgument) {
if (StringUtils::isMatch($regexArgument->value, self::THREE_BACKSLASH_FOR_ESCAPE_NEXT_REGEX)) {
continue;
}

$this->escapeStringNode($regexArgument);
}

if (! $this->hasChanged) {
if (StringUtils::isMatch($node->value, self::THREE_BACKSLASH_FOR_ESCAPE_NEXT_REGEX)) {
return null;
}

return $node;
}

private function escapeStringNode(String_ $string): void
{
$stringValue = $string->value;
$stringValue = $node->value;

if (StringUtils::isMatch($stringValue, self::LEFT_HAND_UNESCAPED_DASH_REGEX)) {
$string->value = Strings::replace($stringValue, self::LEFT_HAND_UNESCAPED_DASH_REGEX, '$1\-');
$node->value = Strings::replace($stringValue, self::LEFT_HAND_UNESCAPED_DASH_REGEX, '$1\-');
// helped needed to skip re-escaping regular expression
$string->setAttribute(AttributeKey::IS_REGULAR_PATTERN, true);
$this->hasChanged = true;
return;
$node->setAttribute(AttributeKey::IS_REGULAR_PATTERN, true);

return $node;
}

if (StringUtils::isMatch($stringValue, self::RIGHT_HAND_UNESCAPED_DASH_REGEX)) {
$string->value = Strings::replace($stringValue, self::RIGHT_HAND_UNESCAPED_DASH_REGEX, '\-$1]');
$node->value = Strings::replace($stringValue, self::RIGHT_HAND_UNESCAPED_DASH_REGEX, '\-$1]');
// helped needed to skip re-escaping regular expression
$string->setAttribute(AttributeKey::IS_REGULAR_PATTERN, true);
$this->hasChanged = true;
$node->setAttribute(AttributeKey::IS_REGULAR_PATTERN, true);

return $node;
}

return null;
}
}
2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v67';
private const CACHE_KEY = 'v68';

private ContainerInterface|null $container = null;

Expand Down
Loading