Skip to content

Commit

Permalink
[DeadCode] Improve RemoveUnusedNonEmptyArrayBeforeForeachRector with …
Browse files Browse the repository at this point in the history
…&& (#3587)
  • Loading branch information
TomasVotruba committed Apr 8, 2023
1 parent a6ca5a0 commit 64cca77
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 9 deletions.
5 changes: 4 additions & 1 deletion packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,10 @@ public function getAnnotationClassNames(): array
public function getGenericTagClassNames(): array
{
/** @var GenericTagValueNode[] $genericTagValueNodes */
$genericTagValueNodes = $this->phpDocNodeByTypeFinder->findByType($this->phpDocNode, GenericTagValueNode::class);
$genericTagValueNodes = $this->phpDocNodeByTypeFinder->findByType(
$this->phpDocNode,
GenericTagValueNode::class
);

$resolvedClasses = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private function shouldSkipHasChildHasReturnType(array $childrenClassReflections
}

$childReturnType = $this->returnTypeInferer->inferFunctionLike($method);
if (!$returnType->isVoid()->yes()) {
if (! $returnType->isVoid()->yes()) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\Fixture;

final class IfNonEmptyAndBiggerThanZero
{
public function run(array $items)
{
if ($items && count($items) > 0) {
foreach ($items as $item) {
echo $item;
}
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\Fixture;

final class IfNonEmptyAndBiggerThanZero
{
public function run(array $items)
{
foreach ($items as $item) {
echo $item;
}
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Rector\DeadCode\Rector\If_;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Foreach_;
Expand Down Expand Up @@ -118,11 +120,16 @@ private function isUselessBeforeForeachCheck(If_ $if, Scope $scope): bool
}
}

if (($if->cond instanceof Variable || $this->propertyFetchAnalyzer->isPropertyFetch($if->cond))
&& $this->nodeComparator->areNodesEqual($if->cond, $foreachExpr)
$ifCond = $if->cond;
if ($ifCond instanceof BooleanAnd) {
return $this->isUselessBooleanAnd($ifCond, $foreachExpr);
}

if (($ifCond instanceof Variable || $this->propertyFetchAnalyzer->isPropertyFetch($ifCond))
&& $this->nodeComparator->areNodesEqual($ifCond, $foreachExpr)
) {
return $scope->getType($if->cond)
->isArray()
$ifType = $scope->getType($ifCond);
return $ifType->isArray()
->yes();
}

Expand All @@ -136,4 +143,17 @@ private function isUselessBeforeForeachCheck(If_ $if, Scope $scope): bool

return $this->countManipulator->isCounterHigherThanOne($if->cond, $foreachExpr);
}

private function isUselessBooleanAnd(BooleanAnd $booleanAnd, Expr $foreachExpr): bool
{
if (! $booleanAnd->left instanceof Variable) {
return false;
}

if (! $this->nodeComparator->areNodesEqual($booleanAnd->left, $foreachExpr)) {
return false;
}

return $this->countManipulator->isCounterHigherThanOne($booleanAnd->right, $foreachExpr);
}
}
1 change: 0 additions & 1 deletion rules/Php72/NodeFactory/AnonymousFunctionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
use PHPStan\Reflection\Php\PhpMethodReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\VoidType;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Node\NodeFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ private function processStrWithResult(
$expr->args[1] = new Arg($resultVariable);

$nextExpression = $stmtsAware->stmts[$key + 1];
$this->traverseNodesWithCallable($nextExpression, function (Node $node) use ($resultVariable, &$hasChanged): ?Variable {
$this->traverseNodesWithCallable($nextExpression, function (Node $node) use (
$resultVariable,
&$hasChanged
): ?Variable {
if (! $node instanceof FuncCall) {
return null;
}
Expand Down
1 change: 0 additions & 1 deletion rules/TypeDeclaration/TypeInferer/ReturnTypeInferer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use PHPStan\Type\VoidType;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Php\PhpVersionProvider;
Expand Down

0 comments on commit 64cca77

Please sign in to comment.