From 5858c7601f347ff93c86aa45631c0f16f080ad21 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Mon, 30 Dec 2019 12:13:17 +0100 Subject: [PATCH] [CodingStyle] Skip re-escaping chars by SymplifyQuoteEscapeRector --- .../String_/SymplifyQuoteEscapeRector.php | 39 +++++++++++++------ .../Fixture/skip_inversed_other_chars.php.inc | 11 ++++++ 2 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 packages/CodingStyle/tests/Rector/String_/SymplifyQuoteEscapeRector/Fixture/skip_inversed_other_chars.php.inc diff --git a/packages/CodingStyle/src/Rector/String_/SymplifyQuoteEscapeRector.php b/packages/CodingStyle/src/Rector/String_/SymplifyQuoteEscapeRector.php index dfb447b8eeb..77898f6a667 100644 --- a/packages/CodingStyle/src/Rector/String_/SymplifyQuoteEscapeRector.php +++ b/packages/CodingStyle/src/Rector/String_/SymplifyQuoteEscapeRector.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Rector\CodingStyle\Rector\String_; +use Nette\Utils\Strings; use PhpParser\Node; use PhpParser\Node\Scalar\String_; use Rector\NodeTypeResolver\Node\AttributeKey; @@ -18,7 +19,7 @@ final class SymplifyQuoteEscapeRector extends AbstractRector { public function getDefinition(): RectorDefinition { - return new RectorDefinition('Prefer quote that not inside the string', [ + return new RectorDefinition('Prefer quote that are not inside the string', [ new CodeSample( <<<'PHP' class SomeClass @@ -62,21 +63,37 @@ PHP $singleQuoteCount = substr_count($node->value, "'"); if ($node->getAttribute(AttributeKey::KIND) === String_::KIND_SINGLE_QUOTED) { - if ($doubleQuoteCount === 0 && $singleQuoteCount > 0) { - $node->setAttribute(AttributeKey::KIND, String_::KIND_DOUBLE_QUOTED); - // invoke override - $node->setAttribute(AttributeKey::ORIGINAL_NODE, null); - } + $this->processSingleQuoted($node, $doubleQuoteCount, $singleQuoteCount); } if ($node->getAttribute(AttributeKey::KIND) === String_::KIND_DOUBLE_QUOTED) { - if ($singleQuoteCount === 0 && $doubleQuoteCount > 0) { - $node->setAttribute(AttributeKey::KIND, String_::KIND_SINGLE_QUOTED); - // invoke override - $node->setAttribute(AttributeKey::ORIGINAL_NODE, null); - } + $this->processDoubleQuoted($node, $singleQuoteCount, $doubleQuoteCount); } return $node; } + + private function processSingleQuoted(String_ $string, int $doubleQuoteCount, int $singleQuoteCount): void + { + if ($doubleQuoteCount === 0 && $singleQuoteCount > 0) { + // contains chars tha will be newly escaped + $matches = Strings::match($string->value, '#\\\\|\$#sim'); + if ($matches) { + return; + } + + $string->setAttribute(AttributeKey::KIND, String_::KIND_DOUBLE_QUOTED); + // invoke override + $string->setAttribute(AttributeKey::ORIGINAL_NODE, null); + } + } + + private function processDoubleQuoted(String_ $string, int $singleQuoteCount, int $doubleQuoteCount): void + { + if ($singleQuoteCount === 0 && $doubleQuoteCount > 0) { + $string->setAttribute(AttributeKey::KIND, String_::KIND_SINGLE_QUOTED); + // invoke override + $string->setAttribute(AttributeKey::ORIGINAL_NODE, null); + } + } } diff --git a/packages/CodingStyle/tests/Rector/String_/SymplifyQuoteEscapeRector/Fixture/skip_inversed_other_chars.php.inc b/packages/CodingStyle/tests/Rector/String_/SymplifyQuoteEscapeRector/Fixture/skip_inversed_other_chars.php.inc new file mode 100644 index 00000000000..ea2f3189745 --- /dev/null +++ b/packages/CodingStyle/tests/Rector/String_/SymplifyQuoteEscapeRector/Fixture/skip_inversed_other_chars.php.inc @@ -0,0 +1,11 @@ +