Skip to content

Commit

Permalink
[DeadCode] Skip assign if cond on RemoveUnusedNonEmptyArrayBeforeFore…
Browse files Browse the repository at this point in the history
…achRector (#2918)

* [DeadCode] Skip assign if cond empty array used in foreach on RemoveUnusedNonEmptyArrayBeforeForeachRector

* [ci-review] Rector Rectify

* phpstan

* completely skip assign in if

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Sep 7, 2022
1 parent 3de25c9 commit 90adbf0
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 38 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

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

function foo(): array {}
// a comment
if ($data = foo()) {
foreach ($data as $row) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,3 @@ if ($keys = []) {
}
}

?>
-----
<?php

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

$values = [];
$keys = [];
foreach ($values as $value) {
echo array_search($value, $keys, true);
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PHPStan\Type\ArrayType;
Expand Down Expand Up @@ -94,7 +93,7 @@ public function refactor(Node $node): array|Node|null
$stmt = $node->stmts[0];

if ($node->cond instanceof Assign) {
return [new Expression($node->cond), $stmt];
return null;
}

$ifComments = $node->getAttribute(AttributeKey::COMMENTS) ?? [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AutoImportTest extends AbstractRectorTestCase
final class PartialValueDocblockRemovalAutoImportTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
Expand Down

0 comments on commit 90adbf0

Please sign in to comment.