Updated Rector to commit da67bb97a141fd0feb2ab90f5576138489f2f67d

da67bb97a1 Add boolean and support to RemoveDeadInstanceOfRector (#5748)
This commit is contained in:
Tomas Votruba 2024-03-21 11:05:56 +00:00
parent 00697fa4b6
commit c950e4d97b
2 changed files with 40 additions and 14 deletions

View File

@ -6,6 +6,7 @@ namespace Rector\DeadCode\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Instanceof_;
@ -64,7 +65,7 @@ CODE_SAMPLE
}
/**
* @param If_ $node
* @return Stmt[]|null|int
* @return Stmt[]|null|int|If_
*/
public function refactor(Node $node)
{
@ -74,6 +75,9 @@ CODE_SAMPLE
if ($node->cond instanceof BooleanNot && $node->cond->expr instanceof Instanceof_) {
return $this->refactorStmtAndInstanceof($node, $node->cond->expr);
}
if ($node->cond instanceof BooleanAnd) {
return $this->refactorIfWithBooleanAnd($node);
}
if ($node->cond instanceof Instanceof_) {
return $this->refactorStmtAndInstanceof($node, $node->cond);
}
@ -84,17 +88,7 @@ CODE_SAMPLE
*/
private function refactorStmtAndInstanceof(If_ $if, Instanceof_ $instanceof)
{
if (!$instanceof->class instanceof Name) {
return null;
}
// handle in another rule
if ($this->isPropertyFetch($instanceof->expr) || $instanceof->expr instanceof CallLike) {
return null;
}
$classType = $this->nodeTypeResolver->getType($instanceof->class);
$exprType = $this->nodeTypeResolver->getType($instanceof->expr);
$isSameStaticTypeOrSubtype = $classType->equals($exprType) || $classType->isSuperTypeOf($exprType)->yes();
if (!$isSameStaticTypeOrSubtype) {
if ($this->isInstanceofTheSameType($instanceof) !== \true) {
return null;
}
if ($this->shouldSkipFromNotTypedParam($instanceof)) {
@ -125,4 +119,36 @@ CODE_SAMPLE
}
return $expr instanceof StaticPropertyFetch;
}
private function isInstanceofTheSameType(Instanceof_ $instanceof) : ?bool
{
if (!$instanceof->class instanceof Name) {
return null;
}
// handled in another rule
if ($this->isPropertyFetch($instanceof->expr) || $instanceof->expr instanceof CallLike) {
return null;
}
$classType = $this->nodeTypeResolver->getType($instanceof->class);
$exprType = $this->nodeTypeResolver->getType($instanceof->expr);
if ($classType->equals($exprType)) {
return \true;
}
return $classType->isSuperTypeOf($exprType)->yes();
}
private function refactorIfWithBooleanAnd(If_ $if) : ?\PhpParser\Node\Stmt\If_
{
if (!$if->cond instanceof BooleanAnd) {
return null;
}
$booleanAnd = $if->cond;
if (!$booleanAnd->left instanceof Instanceof_) {
return null;
}
$instanceof = $booleanAnd->left;
if ($this->isInstanceofTheSameType($instanceof) !== \true) {
return null;
}
$if->cond = $booleanAnd->right;
return $if;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '0c177409ed477e978433597639335fb2a78bc331';
public const PACKAGE_VERSION = 'da67bb97a141fd0feb2ab90f5576138489f2f67d';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-03-21 11:01:32';
public const RELEASE_DATE = '2024-03-21 11:03:36';
/**
* @var int
*/