add IS_REGULAR_PATTERN constant

This commit is contained in:
TomasVotruba 2020-04-25 16:41:02 +02:00
parent 8c486b52e7
commit 81f00b2366
4 changed files with 11 additions and 3 deletions

View File

@ -41,6 +41,7 @@ expectedArguments(
\Rector\NodeTypeResolver\Node\AttributeKey::PHP_DOC_INFO,
\Rector\NodeTypeResolver\Node\AttributeKey::KIND,
\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_SHORT_NAME,
\Rector\NodeTypeResolver\Node\AttributeKey::IS_REGULAR_PATTERN,
);
expectedArguments(
@ -67,6 +68,7 @@ expectedArguments(
\Rector\NodeTypeResolver\Node\AttributeKey::PHP_DOC_INFO,
\Rector\NodeTypeResolver\Node\AttributeKey::KIND,
\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_SHORT_NAME,
\Rector\NodeTypeResolver\Node\AttributeKey::IS_REGULAR_PATTERN,
);
expectedArguments(

View File

@ -144,4 +144,9 @@ final class AttributeKey
* @var string
*/
public const METHOD_CALL_NODE_CALLER_NAME = 'methodCallVariableName';
/**
* @var string
*/
public const IS_REGULAR_PATTERN = 'is_regular_pattern';
}

View File

@ -14,6 +14,7 @@ use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\NodeTypeResolver\Node\AttributeKey;
/**
* @see https://3v4l.org/dRG8U
@ -92,14 +93,14 @@ PHP
if (Strings::match($stringValue, self::LEFT_HAND_UNESCAPED_DASH_PATTERN)) {
$stringNode->value = Strings::replace($stringValue, self::LEFT_HAND_UNESCAPED_DASH_PATTERN, '$1\-');
// helped needed to skip re-escaping regular expression
$stringNode->setAttribute('is_regular_pattern', true);
$stringNode->setAttribute(AttributeKey::IS_REGULAR_PATTERN, true);
return;
}
if (Strings::match($stringValue, self::RIGHT_HAND_UNESCAPED_DASH_PATTERN)) {
$stringNode->value = Strings::replace($stringValue, self::RIGHT_HAND_UNESCAPED_DASH_PATTERN, '\-$1]');
// helped needed to skip re-escaping regular expression
$stringNode->setAttribute('is_regular_pattern', true);
$stringNode->setAttribute(AttributeKey::IS_REGULAR_PATTERN, true);
}
}
}

View File

@ -285,7 +285,7 @@ final class BetterStandardPrinter extends Standard
*/
protected function pScalar_String(String_ $node): string
{
if ($node->getAttribute('is_regular_pattern')) {
if ($node->getAttribute(AttributeKey::IS_REGULAR_PATTERN)) {
$kind = $node->getAttribute(AttributeKey::KIND, String_::KIND_SINGLE_QUOTED);
if ($kind === String_::KIND_DOUBLE_QUOTED) {
return $this->wrapValueWith($node, '"');