Skip to content

Commit

Permalink
[DeadCode] Skip nullable array on not empty on RemoveUnusedNonEmptyAr…
Browse files Browse the repository at this point in the history
…rayBeforeForeachRector (#2935)

* [DeadCode] Skip nullable array on not empty on RemoveUnusedNonEmptyArrayBeforeForeachRector

* Fixed 🎉

* clean up

* clean up

* eol
  • Loading branch information
samsonasik committed Sep 18, 2022
1 parent 5f05178 commit 4923a14
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

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

class SkipNullableArrayOnNotEmpty
{
public function getPrice()
{
$tierPrices = random_int(0, 1) ? [] : null;

if (! empty($tierPrices)) {
foreach ($tierPrices as $tierPrice) {
if ($tierPrice >= 0) {
return $tierPrice;
}
}
}

return 0.0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ArrayType;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\Php\ReservedKeywordAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\DeadCode\NodeManipulator\CountManipulator;
use Rector\DeadCode\UselessIfCondBeforeForeachDetector;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -23,7 +24,7 @@
/**
* @see \Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\RemoveUnusedNonEmptyArrayBeforeForeachRectorTest
*/
final class RemoveUnusedNonEmptyArrayBeforeForeachRector extends AbstractRector
final class RemoveUnusedNonEmptyArrayBeforeForeachRector extends AbstractScopeAwareRector
{
public function __construct(
private readonly CountManipulator $countManipulator,
Expand Down Expand Up @@ -83,9 +84,9 @@ public function getNodeTypes(): array
* @param If_ $node
* @return Stmt[]|Foreach_|null
*/
public function refactor(Node $node): array|Node|null
public function refactorWithScope(Node $node, Scope $scope): array|Node|null
{
if (! $this->isUselessBeforeForeachCheck($node)) {
if (! $this->isUselessBeforeForeachCheck($node, $scope)) {
return null;
}

Expand All @@ -105,7 +106,7 @@ public function refactor(Node $node): array|Node|null
return $stmt;
}

private function isUselessBeforeForeachCheck(If_ $if): bool
private function isUselessBeforeForeachCheck(If_ $if, Scope $scope): bool
{
if (! $this->ifManipulator->isIfWithOnly($if, Foreach_::class)) {
return false;
Expand All @@ -126,7 +127,7 @@ private function isUselessBeforeForeachCheck(If_ $if): bool
return true;
}

if ($this->uselessIfCondBeforeForeachDetector->isMatchingNotEmpty($if, $foreachExpr)) {
if ($this->uselessIfCondBeforeForeachDetector->isMatchingNotEmpty($if, $foreachExpr, $scope)) {
return true;
}

Expand Down
7 changes: 3 additions & 4 deletions rules/DeadCode/UselessIfCondBeforeForeachDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\If_;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ArrayType;
use Rector\Core\NodeAnalyzer\ParamAnalyzer;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeTypeResolver\NodeTypeResolver;

final class UselessIfCondBeforeForeachDetector
{
public function __construct(
private readonly NodeTypeResolver $nodeTypeResolver,
private readonly NodeComparator $nodeComparator,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ParamAnalyzer $paramAnalyzer
Expand All @@ -34,7 +33,7 @@ public function __construct(
* Matches:
* !empty($values)
*/
public function isMatchingNotEmpty(If_ $if, Expr $foreachExpr): bool
public function isMatchingNotEmpty(If_ $if, Expr $foreachExpr, Scope $scope): bool
{
$cond = $if->cond;
if (! $cond instanceof BooleanNot) {
Expand All @@ -53,7 +52,7 @@ public function isMatchingNotEmpty(If_ $if, Expr $foreachExpr): bool
}

// is array though?
$arrayType = $this->nodeTypeResolver->getType($empty->expr);
$arrayType = $scope->getType($empty->expr);
if (! $arrayType instanceof ArrayType) {
return false;
}
Expand Down

0 comments on commit 4923a14

Please sign in to comment.