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\Php73\Rector\FuncCall;
|
2018-12-16 12:09:08 +00:00
|
|
|
|
2024-05-02 02:21:56 +00:00
|
|
|
use RectorPrefix202405\Nette\Utils\Strings;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\Util\StringUtils;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2018-12-16 12:09:08 +00:00
|
|
|
/**
|
2022-05-12 19:17:07 +00:00
|
|
|
* @changelog https://3v4l.org/dRG8U
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php73\Rector\FuncCall\RegexDashEscapeRector\RegexDashEscapeRectorTest
|
2018-12-16 12:09:08 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RegexDashEscapeRector extends AbstractRector implements MinPhpVersionInterface
|
2018-12-16 12:09:08 +00:00
|
|
|
{
|
2021-04-15 15:55:53 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
* @see https://regex101.com/r/iQbGgZ/1
|
|
|
|
*
|
|
|
|
* Use {2} as detected only 2 after $this->regexPatternArgumentManipulator->matchCallArgumentWithRegexPattern() call
|
|
|
|
*/
|
|
|
|
private const THREE_BACKSLASH_FOR_ESCAPE_NEXT_REGEX = '#(?<=[^\\\\])\\\\{2}(?=[^\\\\])#';
|
2018-12-16 12:09:08 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
2020-10-29 18:04:33 +00:00
|
|
|
* @see https://regex101.com/r/YgVJFp/1
|
2018-12-16 12:09:08 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
private const LEFT_HAND_UNESCAPED_DASH_REGEX = '#(\\[.*?\\\\(w|s|d))-(?!\\])#i';
|
2018-12-16 20:56:54 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
2022-08-12 17:18:44 +00:00
|
|
|
* @see https://regex101.com/r/TBVme9/9
|
2018-12-16 20:56:54 +00:00
|
|
|
*/
|
2022-08-12 17:18:44 +00:00
|
|
|
private const RIGHT_HAND_UNESCAPED_DASH_REGEX = '#(?<!\\[)(?<!\\\\)-(\\\\(w|s|d)[.*]?)\\]#i';
|
2021-09-24 14:11:08 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::ESCAPE_DASH_IN_REGEX;
|
2021-09-24 14:11:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-12-16 12:09:08 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Escape - in some cases', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-12-16 20:56:54 +00:00
|
|
|
preg_match("#[\w-()]#", 'some text');
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-12-16 20:56:54 +00:00
|
|
|
preg_match("#[\w\-()]#", 'some text');
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-12-16 12:09:08 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-12-16 12:09:08 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-12-16 12:09:08 +00:00
|
|
|
{
|
2023-06-07 12:49:26 +00:00
|
|
|
return [String_::class];
|
2018-12-16 12:09:08 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-06-07 12:49:26 +00:00
|
|
|
* @param String_ $node
|
2018-12-16 12:09:08 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-12-16 12:09:08 +00:00
|
|
|
{
|
2023-06-07 12:49:26 +00:00
|
|
|
$stringKind = $node->getAttribute(AttributeKey::KIND);
|
|
|
|
if (\in_array($stringKind, [String_::KIND_HEREDOC, String_::KIND_NOWDOC], \true)) {
|
2019-03-11 12:28:41 +00:00
|
|
|
return null;
|
2018-12-16 12:09:08 +00:00
|
|
|
}
|
2023-06-07 12:49:26 +00:00
|
|
|
if (StringUtils::isMatch($node->value, self::THREE_BACKSLASH_FOR_ESCAPE_NEXT_REGEX)) {
|
2022-06-09 10:09:44 +00:00
|
|
|
return null;
|
2021-12-28 14:03:15 +00:00
|
|
|
}
|
2023-06-07 12:49:26 +00:00
|
|
|
$stringValue = $node->value;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (StringUtils::isMatch($stringValue, self::LEFT_HAND_UNESCAPED_DASH_REGEX)) {
|
2023-06-07 12:49:26 +00:00
|
|
|
$node->value = Strings::replace($stringValue, self::LEFT_HAND_UNESCAPED_DASH_REGEX, '$1\\-');
|
2018-12-16 20:56:54 +00:00
|
|
|
// helped needed to skip re-escaping regular expression
|
2023-06-07 12:49:26 +00:00
|
|
|
$node->setAttribute(AttributeKey::IS_REGULAR_PATTERN, \true);
|
|
|
|
return $node;
|
2018-12-16 13:42:59 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (StringUtils::isMatch($stringValue, self::RIGHT_HAND_UNESCAPED_DASH_REGEX)) {
|
2023-06-07 12:49:26 +00:00
|
|
|
$node->value = Strings::replace($stringValue, self::RIGHT_HAND_UNESCAPED_DASH_REGEX, '\\-$1]');
|
2018-12-16 20:56:54 +00:00
|
|
|
// helped needed to skip re-escaping regular expression
|
2023-06-07 12:49:26 +00:00
|
|
|
$node->setAttribute(AttributeKey::IS_REGULAR_PATTERN, \true);
|
|
|
|
return $node;
|
2018-12-16 20:56:54 +00:00
|
|
|
}
|
2023-06-07 12:49:26 +00:00
|
|
|
return null;
|
2018-12-16 12:09:08 +00:00
|
|
|
}
|
|
|
|
}
|