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
@@ -0,0 +1,29 @@
<?php

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

class AccentedChars
{
public function run()
{
$message = 'Merci d\'insérer un message.';
$message = "Il a dit \"été\"";
}
}

?>
-----
<?php

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

class AccentedChars
{
public function run()
{
$message = "Merci d'insérer un message.";
$message = 'Il a dit "été"';
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

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

class SkipEscapedMultibyteChars
{
public function run()
{
$csvHeader = "\xEF\xBB\xBF\"id\"";
$nonBreakingSpace = "\u{00A0}\"";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

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

class SkipInvalidUtf8
{
public function run()
{
$latin1 = 'Merci d\'insérer un message.';
}
}
38 changes: 37 additions & 1 deletion rules/CodingStyle/Rector/String_/SimplifyQuoteEscapeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\CodingStyle\Rector\String_;

use Nette\Utils\Validators;
use PhpParser\Node;
use PhpParser\Node\Scalar\String_;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -28,6 +29,11 @@ final class SimplifyQuoteEscapeRector extends AbstractRector
*/
private const string HAS_NON_PRINTABLE_CHARS = '#[\x00-\x1F\x80-\xFF]#';

/**
* @see https://regex101.com/r/tY7eKW/1
*/
private const string CONTROL_CHARS_REGEX = '#[\x00-\x1F]#';

private bool $hasChanged = false;

public function getRuleDefinition(): RuleDefinition
Expand Down Expand Up @@ -77,7 +83,7 @@ public function refactor(Node $node): ?String_
{
$this->hasChanged = false;

if (StringUtils::isMatch($node->value, self::HAS_NON_PRINTABLE_CHARS)) {
if ($this->hasNonPrintableChars($node->value)) {
return null;
}

Expand Down Expand Up @@ -125,6 +131,11 @@ private function processDoubleQuoted(String_ $string, int $singleQuoteCount, int
return;
}

// contains escape sequence that will be printed as raw char, e.g. "\u{00A0}"
if ($this->hasEscapeSequence($string)) {
return;
}

$string->setAttribute(AttributeKey::KIND, String_::KIND_SINGLE_QUOTED);
// invoke override
$string->setAttribute(AttributeKey::ORIGINAL_NODE, null);
Expand All @@ -137,4 +148,29 @@ private function isMatchEscapedChars(string $string): bool
{
return StringUtils::isMatch($string, self::ESCAPED_CHAR_REGEX);
}

private function hasEscapeSequence(String_ $string): bool
{
$rawValue = $string->getAttribute(AttributeKey::RAW_VALUE);
if (! is_string($rawValue)) {
return false;
}

// any escape sequence other than \" makes the written content differ from the value
return str_replace('\\"', '"', substr($rawValue, 1, -1)) !== $string->value;
}

private function hasNonPrintableChars(string $value): bool
{
if (! StringUtils::isMatch($value, self::HAS_NON_PRINTABLE_CHARS)) {
return false;
}

if (StringUtils::isMatch($value, self::CONTROL_CHARS_REGEX)) {
return true;
}

// bytes above ASCII are printable as part of valid UTF-8 text, e.g. "é"
return ! Validators::isUnicode($value);
}
}
Loading