Updated Rector to commit eef0e5d1f47500c9f56c5ccaa16523b11b47c8a7

eef0e5d1f4 Refactor PARENT_NODE away from JoinStringConcatRector (#3947)
This commit is contained in:
Tomas Votruba 2023-05-24 12:18:40 +00:00
parent 1f89bb9561
commit 50ec3d44e9
5 changed files with 26 additions and 63 deletions

View File

@ -9,7 +9,6 @@ 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;
/**
@ -27,10 +26,6 @@ final class JoinStringConcatRector extends AbstractRector
* @see https://stackoverflow.com/questions/4147646/determine-if-utf-8-text-is-all-ascii
*/
private const ASCII_REGEX = '#[^\\x00-\\x7F]#';
/**
* @var bool
*/
private $nodeReplacementIsRestricted = \false;
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Joins concat of 2 strings, unless the length is too long', [new CodeSample(<<<'CODE_SAMPLE'
@ -65,60 +60,28 @@ CODE_SAMPLE
*/
public function refactor(Node $node) : ?Node
{
$this->nodeReplacementIsRestricted = \false;
if (!$this->isTopMostConcatNode($node)) {
if (!$node->left instanceof String_) {
return null;
}
$joinedNode = $this->joinConcatIfStrings($node);
if (!$joinedNode instanceof String_) {
if (!$node->right instanceof String_) {
return null;
}
if ($this->nodeReplacementIsRestricted) {
return null;
}
return $joinedNode;
return $this->joinConcatIfStrings($node->left, $node->right);
}
private function isTopMostConcatNode(Concat $concat) : bool
private function joinConcatIfStrings(String_ $leftString, String_ $rightString) : ?String_
{
$parentNode = $concat->getAttribute(AttributeKey::PARENT_NODE);
return !$parentNode instanceof Concat;
}
/**
* @return \PhpParser\Node\Expr\BinaryOp\Concat|\PhpParser\Node\Scalar\String_
*/
private function joinConcatIfStrings(Concat $node)
{
$concat = clone $node;
if ($concat->left instanceof Concat) {
$concat->left = $this->joinConcatIfStrings($concat->left);
$leftValue = $leftString->value;
$rightValue = $rightString->value;
if ($leftValue === "\n" || $rightValue === "\n") {
return null;
}
if ($concat->right instanceof Concat) {
$concat->right = $this->joinConcatIfStrings($concat->right);
$joinedStringValue = $leftValue . $rightValue;
if (StringUtils::isMatch($joinedStringValue, self::ASCII_REGEX)) {
return null;
}
if (!$concat->left instanceof String_) {
return $node;
if (Strings::length($joinedStringValue) >= self::LINE_BREAK_POINT) {
return null;
}
if (!$concat->right instanceof String_) {
return $node;
}
$leftValue = $concat->left->value;
$rightValue = $concat->right->value;
if ($leftValue === "\n") {
$this->nodeReplacementIsRestricted = \true;
return $node;
}
if ($rightValue === "\n") {
$this->nodeReplacementIsRestricted = \true;
return $node;
}
$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;
}
return $resultString;
return new String_($joinedStringValue);
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'c050b66f9b9812f8f7735673e651deaccded0071';
public const PACKAGE_VERSION = 'eef0e5d1f47500c9f56c5ccaa16523b11b47c8a7';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-24 10:00:13';
public const RELEASE_DATE = '2023-05-24 12:13:50';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit6665049c1bf4f1bfd9e1d88569d19ca5::getLoader();
return ComposerAutoloaderInitdc6f3b0feb4502ccd83ba99a2353866a::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit6665049c1bf4f1bfd9e1d88569d19ca5
class ComposerAutoloaderInitdc6f3b0feb4502ccd83ba99a2353866a
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit6665049c1bf4f1bfd9e1d88569d19ca5
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit6665049c1bf4f1bfd9e1d88569d19ca5', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitdc6f3b0feb4502ccd83ba99a2353866a', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit6665049c1bf4f1bfd9e1d88569d19ca5', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitdc6f3b0feb4502ccd83ba99a2353866a', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit6665049c1bf4f1bfd9e1d88569d19ca5::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitdc6f3b0feb4502ccd83ba99a2353866a::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit6665049c1bf4f1bfd9e1d88569d19ca5::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInitdc6f3b0feb4502ccd83ba99a2353866a::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit6665049c1bf4f1bfd9e1d88569d19ca5
class ComposerStaticInitdc6f3b0feb4502ccd83ba99a2353866a
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3106,9 +3106,9 @@ class ComposerStaticInit6665049c1bf4f1bfd9e1d88569d19ca5
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit6665049c1bf4f1bfd9e1d88569d19ca5::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit6665049c1bf4f1bfd9e1d88569d19ca5::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit6665049c1bf4f1bfd9e1d88569d19ca5::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitdc6f3b0feb4502ccd83ba99a2353866a::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitdc6f3b0feb4502ccd83ba99a2353866a::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitdc6f3b0feb4502ccd83ba99a2353866a::$classMap;
}, null, ClassLoader::class);
}