Updated Rector to commit eb463524bc550781d37cb170c416329bfe727992

eb463524bc Remove PARENT_NODE from CountArrayToEmptyArrayComparisonRector (#3965)
This commit is contained in:
Tomas Votruba 2023-05-25 15:20:14 +00:00
parent 3a65d40447
commit 48f64cd534
5 changed files with 99 additions and 78 deletions

View File

@ -16,7 +16,6 @@ use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\If_;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
@ -43,56 +42,44 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [FuncCall::class, BooleanNot::class];
return [Identical::class, NotIdentical::class, BooleanNot::class, Greater::class, Smaller::class, If_::class, ElseIf_::class];
}
/**
* @param FuncCall|BooleanNot $node
* @param Identical|NotIdentical|BooleanNot|Greater|Smaller|If_|ElseIf_ $node
*/
public function refactor(Node $node) : ?Node
{
if ($node instanceof BooleanNot) {
return $this->processMarkTruthyNegation($node);
return $this->refactorBooleanNot($node);
}
if (!$this->isName($node, 'count')) {
return null;
if ($node instanceof Identical || $node instanceof NotIdentical) {
if ($node->left instanceof FuncCall) {
$expr = $this->matchCountFuncCallArgExpr($node->left);
} elseif ($node->right instanceof FuncCall) {
$expr = $this->matchCountFuncCallArgExpr($node->right);
} else {
return null;
}
if (!$expr instanceof Expr) {
return null;
}
// not pass array type, skip
if (!$this->isArray($expr)) {
return null;
}
return $this->refactorIdenticalOrNotIdentical($node, $expr);
}
if (!isset($node->getArgs()[0])) {
return null;
if ($node instanceof Smaller || $node instanceof Greater) {
return $this->refactorGreaterOrSmaller($node);
}
/** @var Expr $expr */
$expr = $node->getArgs()[0]->value;
// not pass array type, skip
if (!$this->isArray($expr)) {
return null;
}
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if (!$parentNode instanceof Node) {
return null;
}
$processIdentical = $this->processIdenticalOrNotIdentical($parentNode, $node, $expr);
if ($processIdentical instanceof Expr) {
return $processIdentical;
}
$processGreaterOrSmaller = $this->processGreaterOrSmaller($parentNode, $node, $expr);
if ($processGreaterOrSmaller instanceof NotIdentical) {
return $processGreaterOrSmaller;
}
return $this->processMarkTruthy($parentNode, $node, $expr);
return $this->refactorIfElseIf($node);
}
private function processMarkTruthyNegation(BooleanNot $booleanNot) : ?Identical
private function refactorBooleanNot(BooleanNot $booleanNot) : ?Identical
{
if (!$booleanNot->expr instanceof FuncCall) {
$expr = $this->matchCountFuncCallArgExpr($booleanNot->expr);
if (!$expr instanceof Expr) {
return null;
}
$funcCall = $booleanNot->expr;
if (!$this->isName($funcCall, 'count')) {
return null;
}
if (!isset($funcCall->getArgs()[0])) {
return null;
}
/** @var Expr $expr */
$expr = $funcCall->getArgs()[0]->value;
// not pass array type, skip
if (!$this->isArray($expr)) {
return null;
@ -103,43 +90,77 @@ CODE_SAMPLE
{
return $this->getType($expr)->isArray()->yes();
}
private function processIdenticalOrNotIdentical(Node $node, FuncCall $funcCall, Expr $expr) : ?Expr
/**
* @param \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\NotIdentical $binaryOp
* @return \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\BinaryOp\NotIdentical|null
*/
private function refactorIdenticalOrNotIdentical($binaryOp, Expr $expr)
{
if (($node instanceof Identical || $node instanceof NotIdentical) && $node->right instanceof LNumber && $node->right->value === 0) {
$this->removeNode($funcCall);
$node->right = new Array_([]);
return $expr;
if ($this->isZeroLNumber($binaryOp->right)) {
$binaryOp->left = $expr;
$binaryOp->right = new Array_([]);
return $binaryOp;
}
if (($node instanceof Identical || $node instanceof NotIdentical) && $node->left instanceof LNumber && $node->left->value === 0) {
$this->removeNode($funcCall);
$node->left = new Array_([]);
return $expr;
if ($this->isZeroLNumber($binaryOp->left)) {
$binaryOp->left = new Array_([]);
$binaryOp->right = $expr;
return $binaryOp;
}
return null;
}
private function processGreaterOrSmaller(Node $node, FuncCall $funcCall, Expr $expr) : ?NotIdentical
/**
* @param \PhpParser\Node\Expr\BinaryOp\Greater|\PhpParser\Node\Expr\BinaryOp\Smaller $binaryOp
*/
private function refactorGreaterOrSmaller($binaryOp) : ?\PhpParser\Node\Expr\BinaryOp\NotIdentical
{
if ($node instanceof Greater && $node->right instanceof LNumber && $node->right->value === 0) {
$this->removeNode($funcCall);
$this->removeNode($node->right);
return new NotIdentical($expr, new Array_([]));
if ($binaryOp instanceof Greater) {
$leftExpr = $this->matchCountFuncCallArgExpr($binaryOp->left);
if (!$leftExpr instanceof Expr) {
return null;
}
if (!$this->isZeroLNumber($binaryOp->right)) {
return null;
}
return new NotIdentical($leftExpr, new Array_([]));
}
if ($node instanceof Smaller && $node->left instanceof LNumber && $node->left->value === 0) {
$this->removeNode($funcCall);
$this->removeNode($node->left);
return new NotIdentical(new Array_([]), $expr);
}
return null;
}
private function processMarkTruthy(Node $node, FuncCall $funcCall, Expr $expr) : ?Expr
{
if (!$node instanceof If_ && !$node instanceof ElseIf_) {
$rightExpr = $this->matchCountFuncCallArgExpr($binaryOp->right);
if (!$rightExpr instanceof Expr) {
return null;
}
if ($node->cond === $funcCall) {
$node->cond = new NotIdentical($expr, new Array_([]));
return $node->cond;
if (!$this->isZeroLNumber($binaryOp->left)) {
return null;
}
return null;
return new NotIdentical(new Array_([]), $rightExpr);
}
/**
* @param \PhpParser\Node\Stmt\If_|\PhpParser\Node\Stmt\ElseIf_ $ifElseIf
* @return \PhpParser\Node\Stmt\If_|\PhpParser\Node\Stmt\ElseIf_|null
*/
private function refactorIfElseIf($ifElseIf)
{
$expr = $this->matchCountFuncCallArgExpr($ifElseIf->cond);
if (!$expr instanceof Expr) {
return null;
}
$ifElseIf->cond = new NotIdentical($expr, new Array_([]));
return $ifElseIf;
}
private function matchCountFuncCallArgExpr(Expr $expr) : ?Expr
{
if (!$expr instanceof FuncCall) {
return null;
}
if (!$this->isName($expr, 'count')) {
return null;
}
$firstArg = $expr->getArgs()[0];
return $firstArg->value;
}
private function isZeroLNumber(Expr $expr) : bool
{
if (!$expr instanceof LNumber) {
return \false;
}
return $expr->value === 0;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'a244917a428baa6fa06fabaa4e0be15fd3154e50';
public const PACKAGE_VERSION = 'eb463524bc550781d37cb170c416329bfe727992';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-25 15:47:30';
public const RELEASE_DATE = '2023-05-25 16:16:16';
/**
* @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 ComposerAutoloaderInite19991332cb57cb150253d78c194e633::getLoader();
return ComposerAutoloaderInitc50a9902db5deb0fa0cb9a4c96befbbc::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInite19991332cb57cb150253d78c194e633
class ComposerAutoloaderInitc50a9902db5deb0fa0cb9a4c96befbbc
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInite19991332cb57cb150253d78c194e633
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInite19991332cb57cb150253d78c194e633', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitc50a9902db5deb0fa0cb9a4c96befbbc', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInite19991332cb57cb150253d78c194e633', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitc50a9902db5deb0fa0cb9a4c96befbbc', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInite19991332cb57cb150253d78c194e633::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitc50a9902db5deb0fa0cb9a4c96befbbc::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInite19991332cb57cb150253d78c194e633::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInitc50a9902db5deb0fa0cb9a4c96befbbc::$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 ComposerStaticInite19991332cb57cb150253d78c194e633
class ComposerStaticInitc50a9902db5deb0fa0cb9a4c96befbbc
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3096,9 +3096,9 @@ class ComposerStaticInite19991332cb57cb150253d78c194e633
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInite19991332cb57cb150253d78c194e633::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInite19991332cb57cb150253d78c194e633::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInite19991332cb57cb150253d78c194e633::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitc50a9902db5deb0fa0cb9a4c96befbbc::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitc50a9902db5deb0fa0cb9a4c96befbbc::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitc50a9902db5deb0fa0cb9a4c96befbbc::$classMap;
}, null, ClassLoader::class);
}