Updated Rector to commit 5f915d861b

5f915d861b handle cases where strlen arg is a Stringable object (#981)
This commit is contained in:
Tomas Votruba 2021-10-11 23:20:15 +00:00
parent c6a9b6bdf0
commit dbec2c82cd
6 changed files with 32 additions and 41 deletions

View File

@ -10,6 +10,8 @@ use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\String_;
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PHPStan\Type\StringType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -31,7 +33,7 @@ final class StrlenZeroToIdenticalEmptyStringRector extends \Rector\Core\Rector\A
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Changes strlen comparison to 0 to direct empty string compare', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function run($value)
public function run(string $value)
{
$empty = strlen($value) === 0;
}
@ -40,7 +42,7 @@ CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function run($value)
public function run(string $value)
{
$empty = $value === '';
}
@ -61,19 +63,19 @@ CODE_SAMPLE
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
if ($node->left instanceof \PhpParser\Node\Expr\FuncCall) {
return $this->processLeftIdentical($node, $node->left);
return $this->processIdentical($node->right, $node->left);
}
if ($node->right instanceof \PhpParser\Node\Expr\FuncCall) {
return $this->processRightIdentical($node, $node->right);
return $this->processIdentical($node->left, $node->right);
}
return null;
}
private function processLeftIdentical(\PhpParser\Node\Expr\BinaryOp\Identical $identical, \PhpParser\Node\Expr\FuncCall $funcCall) : ?\PhpParser\Node\Expr\BinaryOp\Identical
private function processIdentical(\PhpParser\Node\Expr $value, \PhpParser\Node\Expr\FuncCall $funcCall) : ?\PhpParser\Node\Expr\BinaryOp\Identical
{
if (!$this->isName($funcCall, 'strlen')) {
return null;
}
if (!$this->valueResolver->isValue($identical->right, 0)) {
if (!$this->valueResolver->isValue($value, 0)) {
return null;
}
if (!$this->argsAnalyzer->isArgInstanceInArgsPosition($funcCall->args, 0)) {
@ -83,23 +85,12 @@ CODE_SAMPLE
$firstArg = $funcCall->args[0];
/** @var Expr $variable */
$variable = $firstArg->value;
return new \PhpParser\Node\Expr\BinaryOp\Identical($variable, new \PhpParser\Node\Scalar\String_(''));
}
private function processRightIdentical(\PhpParser\Node\Expr\BinaryOp\Identical $identical, \PhpParser\Node\Expr\FuncCall $funcCall) : ?\PhpParser\Node\Expr\BinaryOp\Identical
{
if (!$this->isName($funcCall, 'strlen')) {
return null;
// Needs string cast if variable type is not string
// see https://github.com/rectorphp/rector/issues/6700
$isStringType = $this->nodeTypeResolver->getNativeType($variable) instanceof \PHPStan\Type\StringType;
if (!$isStringType) {
return new \PhpParser\Node\Expr\BinaryOp\Identical(new \PhpParser\Node\Expr\Cast\String_($variable), new \PhpParser\Node\Scalar\String_(''));
}
if (!$this->valueResolver->isValue($identical->left, 0)) {
return null;
}
if (!$this->argsAnalyzer->isArgInstanceInArgsPosition($funcCall->args, 0)) {
return null;
}
/** @var Arg $firstArg */
$firstArg = $funcCall->args[0];
/** @var Expr $variable */
$variable = $firstArg->value;
return new \PhpParser\Node\Expr\BinaryOp\Identical($variable, new \PhpParser\Node\Scalar\String_(''));
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '65c3a44c638f203cb2c10affa6dcc70af920e6f8';
public const PACKAGE_VERSION = '5f915d861b2a48043d8783568f4b825560b0a00b';
/**
* @var string
*/
public const RELEASE_DATE = '2021-10-11 08:31:02';
public const RELEASE_DATE = '2021-10-12 06:08:52';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20211011\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);

2
vendor/autoload.php vendored
View File

@ -4,4 +4,4 @@
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInitbd7b4ae0bb0524bad8e1548e61ab79c4::getLoader();
return ComposerAutoloaderInit86b67970e057798250bb7d712a4d5e0f::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitbd7b4ae0bb0524bad8e1548e61ab79c4
class ComposerAutoloaderInit86b67970e057798250bb7d712a4d5e0f
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInitbd7b4ae0bb0524bad8e1548e61ab79c4
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitbd7b4ae0bb0524bad8e1548e61ab79c4', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit86b67970e057798250bb7d712a4d5e0f', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInitbd7b4ae0bb0524bad8e1548e61ab79c4', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit86b67970e057798250bb7d712a4d5e0f', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitbd7b4ae0bb0524bad8e1548e61ab79c4::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit86b67970e057798250bb7d712a4d5e0f::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,19 +42,19 @@ class ComposerAutoloaderInitbd7b4ae0bb0524bad8e1548e61ab79c4
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInitbd7b4ae0bb0524bad8e1548e61ab79c4::$files;
$includeFiles = Composer\Autoload\ComposerStaticInit86b67970e057798250bb7d712a4d5e0f::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequirebd7b4ae0bb0524bad8e1548e61ab79c4($fileIdentifier, $file);
composerRequire86b67970e057798250bb7d712a4d5e0f($fileIdentifier, $file);
}
return $loader;
}
}
function composerRequirebd7b4ae0bb0524bad8e1548e61ab79c4($fileIdentifier, $file)
function composerRequire86b67970e057798250bb7d712a4d5e0f($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInitbd7b4ae0bb0524bad8e1548e61ab79c4
class ComposerStaticInit86b67970e057798250bb7d712a4d5e0f
{
public static $files = array (
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
@ -3896,9 +3896,9 @@ class ComposerStaticInitbd7b4ae0bb0524bad8e1548e61ab79c4
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitbd7b4ae0bb0524bad8e1548e61ab79c4::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitbd7b4ae0bb0524bad8e1548e61ab79c4::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitbd7b4ae0bb0524bad8e1548e61ab79c4::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit86b67970e057798250bb7d712a4d5e0f::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit86b67970e057798250bb7d712a4d5e0f::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit86b67970e057798250bb7d712a4d5e0f::$classMap;
}, null, ClassLoader::class);
}

View File

@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
spl_autoload_call('RectorPrefix20211011\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInitbd7b4ae0bb0524bad8e1548e61ab79c4', false) && !interface_exists('ComposerAutoloaderInitbd7b4ae0bb0524bad8e1548e61ab79c4', false) && !trait_exists('ComposerAutoloaderInitbd7b4ae0bb0524bad8e1548e61ab79c4', false)) {
spl_autoload_call('RectorPrefix20211011\ComposerAutoloaderInitbd7b4ae0bb0524bad8e1548e61ab79c4');
if (!class_exists('ComposerAutoloaderInit86b67970e057798250bb7d712a4d5e0f', false) && !interface_exists('ComposerAutoloaderInit86b67970e057798250bb7d712a4d5e0f', false) && !trait_exists('ComposerAutoloaderInit86b67970e057798250bb7d712a4d5e0f', false)) {
spl_autoload_call('RectorPrefix20211011\ComposerAutoloaderInit86b67970e057798250bb7d712a4d5e0f');
}
if (!class_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !interface_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !trait_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false)) {
spl_autoload_call('RectorPrefix20211011\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -3306,9 +3306,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20211011\print_node(...func_get_args());
}
}
if (!function_exists('composerRequirebd7b4ae0bb0524bad8e1548e61ab79c4')) {
function composerRequirebd7b4ae0bb0524bad8e1548e61ab79c4() {
return \RectorPrefix20211011\composerRequirebd7b4ae0bb0524bad8e1548e61ab79c4(...func_get_args());
if (!function_exists('composerRequire86b67970e057798250bb7d712a4d5e0f')) {
function composerRequire86b67970e057798250bb7d712a4d5e0f() {
return \RectorPrefix20211011\composerRequire86b67970e057798250bb7d712a4d5e0f(...func_get_args());
}
}
if (!function_exists('parseArgs')) {