Skip to content

Commit

Permalink
[CodeQuality] Skip with ascii on JoinStringConcatRector (#3497)
Browse files Browse the repository at this point in the history
* [CodeQuality] Skip with multibyte on JoinStringConcatRector

* Fixed 🎉

* eol

* verify with regex

* rename fixture

* fix phpstan
  • Loading branch information
samsonasik committed Mar 21, 2023
1 parent 6481ecd commit 28e9cdb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Concat\JoinStringConcatRector\Fixture;

final class SkipWithAscii
{
public function run(string $content)
{
return mb_strpos($content, "\x1f" . "\x8b" . "\x08") === 0;
}
}
12 changes: 12 additions & 0 deletions rules/CodeQuality/Rector/Concat/JoinStringConcatRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Scalar\String_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Util\StringUtils;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -25,6 +26,13 @@ final class JoinStringConcatRector extends AbstractRector

private bool $nodeReplacementIsRestricted = false;

/**
* @var string
* @see https://regex101.com/r/VaXM1t/1
* @see https://stackoverflow.com/questions/4147646/determine-if-utf-8-text-is-all-ascii
*/
private const ASCII_REGEX = '#[^\x00-\x7F]#';

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -126,6 +134,10 @@ private function joinConcatIfStrings(Concat $node): Concat | String_
}

$resultString = new String_($leftValue . $rightValue);
if (StringUtils::isMatch($resultString->value, self::ASCII_REGEX)) {
return $node;
}

if (Strings::length($resultString->value) >= self::LINE_BREAK_POINT) {
$this->nodeReplacementIsRestricted = true;
return $node;
Expand Down

0 comments on commit 28e9cdb

Please sign in to comment.