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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\CodingStyle\Rector\String_;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Scalar\String_;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -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
Expand Down Expand Up @@ -62,21 +63,37 @@ public function refactor(Node $node): ?Node
$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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\CodingStyle\Tests\Rector\String_\SymplifyQuoteEscapeRector\Fixture;

class SkipInversedOtherChars
{
public function run()
{
$name = 'Declaration of Kedlubna\extendTest::add($message) should be compatible with Kedlubna\test::add(string $message = \'\')';
}
}