Skip to content

Commit

Permalink
[CodeQuality] Handle crash on for loop on OptionalParametersAfterRequ…
Browse files Browse the repository at this point in the history
…iredRector (#6543)

* [CodeQuality] Handle crash on for loop on OptionalParametersAfterRequiredRector

* Fix

* Fix

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
samsonasik and actions-user authored Dec 10, 2024
1 parent 1e028eb commit 2fa586b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector\Fixture;

final class SkipCountForLoop
{
public function __invoke($em)
{
$visitedProduct1 = null;
$visitedProduct2 = null;
$visitedProduct3 = null;

$products = $em->getRepository(Product::class)->findAll();
$visitedProducts = [];
for ($i = 0; $i < count($products), count($visitedProducts) <= 3; ++$i) {
$product = $products[$i];
if (!in_array($product, $exclude)) {
$varName = 'visitedProduct' . ($i + 1);
$visitedProducts[$varName] = $product;
}
}

extract($visitedProducts);
}
}
12 changes: 12 additions & 0 deletions src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use PhpParser\Node\Stmt\EnumCase;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Finally_;
use PhpParser\Node\Stmt\For_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Interface_;
Expand Down Expand Up @@ -222,6 +223,17 @@ public function processNodes(
return;
}

if ($node instanceof For_) {
foreach (array_merge($node->init, $node->cond, $node->loop) as $expr) {
$expr->setAttribute(AttributeKey::SCOPE, $mutatingScope);
if ($expr instanceof BinaryOp) {
$this->processBinaryOp($expr, $mutatingScope);
}
}

return;
}

if ($node instanceof Array_) {
$this->processArray($node, $mutatingScope);
return;
Expand Down

0 comments on commit 2fa586b

Please sign in to comment.