|
4 | 4 |
|
5 | 5 | namespace Rector\CodingStyle\Rector\String_; |
6 | 6 |
|
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Scalar\String_; |
| 9 | +use Rector\NodeTypeResolver\Node\AttributeKey; |
| 10 | +use Rector\Rector\AbstractRector; |
| 11 | +use Rector\Util\StringUtils; |
| 12 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 13 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 14 | + |
7 | 15 | /** |
8 | 16 | * @see \Rector\Tests\CodingStyle\Rector\String_\SimplifyQuoteEscapeRector\SimplifyQuoteEscapeRectorTest |
9 | | - * |
10 | | - * @phpstan-ignore class.extendsDeprecatedClass,symplify.forbiddenExtendOfNonAbstractClass |
11 | 17 | */ |
12 | | -final class SimplifyQuoteEscapeRector extends SymplifyQuoteEscapeRector |
| 18 | +class SimplifyQuoteEscapeRector extends AbstractRector |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var string |
| 22 | + * @see https://regex101.com/r/qEkCe9/2 |
| 23 | + */ |
| 24 | + private const ESCAPED_CHAR_REGEX = '#\\\\|\$|\\n|\\t#sim'; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var string |
| 28 | + * @see https://alvinalexander.com/php/how-to-remove-non-printable-characters-in-string-regex/ |
| 29 | + * @see https://regex101.com/r/lGUhRb/1 |
| 30 | + */ |
| 31 | + private const HAS_NON_PRINTABLE_CHARS = '#[\x00-\x1F\x80-\xFF]#'; |
| 32 | + |
| 33 | + private bool $hasChanged = false; |
| 34 | + |
| 35 | + public function getRuleDefinition(): RuleDefinition |
| 36 | + { |
| 37 | + return new RuleDefinition( |
| 38 | + 'Prefer quote that are not inside the string', |
| 39 | + [ |
| 40 | + new CodeSample( |
| 41 | + <<<'CODE_SAMPLE' |
| 42 | +class SomeClass |
| 43 | +{ |
| 44 | + public function run() |
| 45 | + { |
| 46 | + $name = "\" Tom"; |
| 47 | + $name = '\' Sara'; |
| 48 | + } |
| 49 | +} |
| 50 | +CODE_SAMPLE |
| 51 | + , |
| 52 | + <<<'CODE_SAMPLE' |
| 53 | +class SomeClass |
13 | 54 | { |
| 55 | + public function run() |
| 56 | + { |
| 57 | + $name = '" Tom'; |
| 58 | + $name = "' Sara"; |
| 59 | + } |
| 60 | +} |
| 61 | +CODE_SAMPLE |
| 62 | + ), |
| 63 | + ] |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @return array<class-string<Node>> |
| 69 | + */ |
| 70 | + public function getNodeTypes(): array |
| 71 | + { |
| 72 | + return [String_::class]; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param String_ $node |
| 77 | + */ |
| 78 | + public function refactor(Node $node): ?String_ |
| 79 | + { |
| 80 | + $this->hasChanged = false; |
| 81 | + |
| 82 | + if (StringUtils::isMatch($node->value, self::HAS_NON_PRINTABLE_CHARS)) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + $doubleQuoteCount = substr_count($node->value, '"'); |
| 87 | + $singleQuoteCount = substr_count($node->value, "'"); |
| 88 | + $kind = $node->getAttribute(AttributeKey::KIND); |
| 89 | + |
| 90 | + if ($kind === String_::KIND_SINGLE_QUOTED) { |
| 91 | + $this->processSingleQuoted($node, $doubleQuoteCount, $singleQuoteCount); |
| 92 | + } |
| 93 | + |
| 94 | + $quoteKind = $node->getAttribute(AttributeKey::KIND); |
| 95 | + if ($quoteKind === String_::KIND_DOUBLE_QUOTED) { |
| 96 | + $this->processDoubleQuoted($node, $singleQuoteCount, $doubleQuoteCount); |
| 97 | + } |
| 98 | + |
| 99 | + if (! $this->hasChanged) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + return $node; |
| 104 | + } |
| 105 | + |
| 106 | + private function processSingleQuoted(String_ $string, int $doubleQuoteCount, int $singleQuoteCount): void |
| 107 | + { |
| 108 | + if ($doubleQuoteCount === 0 && $singleQuoteCount > 0) { |
| 109 | + // contains chars that will be newly escaped |
| 110 | + if ($this->isMatchEscapedChars($string->value)) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + $string->setAttribute(AttributeKey::KIND, String_::KIND_DOUBLE_QUOTED); |
| 115 | + // invoke override |
| 116 | + $string->setAttribute(AttributeKey::ORIGINAL_NODE, null); |
| 117 | + |
| 118 | + $this->hasChanged = true; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private function processDoubleQuoted(String_ $string, int $singleQuoteCount, int $doubleQuoteCount): void |
| 123 | + { |
| 124 | + if ($singleQuoteCount === 0 && $doubleQuoteCount > 0) { |
| 125 | + // contains chars that will be newly escaped |
| 126 | + if ($this->isMatchEscapedChars($string->value)) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + $string->setAttribute(AttributeKey::KIND, String_::KIND_SINGLE_QUOTED); |
| 131 | + // invoke override |
| 132 | + $string->setAttribute(AttributeKey::ORIGINAL_NODE, null); |
| 133 | + |
| 134 | + $this->hasChanged = true; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private function isMatchEscapedChars(string $string): bool |
| 139 | + { |
| 140 | + return StringUtils::isMatch($string, self::ESCAPED_CHAR_REGEX); |
| 141 | + } |
14 | 142 | } |
0 commit comments