Skip to content

Commit

Permalink
RemoveUnusedNonEmptyArrayBeforeForeachRector: ignore phpdoc types (#5169
Browse files Browse the repository at this point in the history
)

* RemoveUnusedNonEmptyArrayBeforeForeachRector: ignore phpdoc types

* Update property_foreach.php.inc

* Update property_foreach.php.inc

* fix
  • Loading branch information
staabm committed Oct 14, 2023
1 parent d55a35b commit c639ef8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ namespace Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeac

final class PropertyForeach
{
private $items = [];

public function __construct(array $items)
{
$this->items = $items;
}
private array $items = [];

public function run()
{
Expand All @@ -28,12 +23,7 @@ namespace Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeac

final class PropertyForeach
{
private $items = [];

public function __construct(array $items)
{
$this->items = $items;
}
private array $items = [];

public function run()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

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

final class SkipIfNonEmptyAndBiggerThanZeroPhpdoc
{
/**
* @param array $items
*/
public function run($items)
{
if ($items && count($items) > 0) {
foreach ($items as $item) {
echo $item;
}
}
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ private function isUselessBeforeForeachCheck(If_ $if, Scope $scope): bool
if (is_string($variableName) && $this->reservedKeywordAnalyzer->isNativeVariable($variableName)) {
return false;
}

$ifType = $scope->getNativeType($foreachExpr);
if (!$ifType->isArray()->yes()) {
return false;
}
}

$ifCond = $if->cond;
Expand All @@ -121,7 +126,7 @@ private function isUselessBeforeForeachCheck(If_ $if, Scope $scope): bool
if (($ifCond instanceof Variable || $this->propertyFetchAnalyzer->isPropertyFetch($ifCond))
&& $this->nodeComparator->areNodesEqual($ifCond, $foreachExpr)
) {
$ifType = $scope->getType($ifCond);
$ifType = $scope->getNativeType($ifCond);
return $ifType->isArray()
->yes();
}
Expand Down

0 comments on commit c639ef8

Please sign in to comment.