Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 14 additions & 43 deletions src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,45 @@
namespace PHPStan\Rules\ForLoop;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Stmt\For_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Node\VariableWritesNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function is_string;
use function sprintf;

/**
* @implements Rule<For_>
* @implements Rule<VariableWritesNode>
*/
class OverwriteVariablesWithForLoopInitRule implements Rule
{

public function getNodeType(): string
{
return For_::class;
return VariableWritesNode::class;

Check failure on line 21 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, highest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 21 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, highest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 21 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, highest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 21 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, lowest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 21 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, highest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 21 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, lowest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 21 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, lowest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 21 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, lowest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 21 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, lowest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 21 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, lowest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 21 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, highest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 21 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, lowest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 21 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, highest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 21 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, highest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.
}

public function processNode(Node $node, Scope $scope): array
{
if ($node->isOpaque()) {

Check failure on line 26 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, highest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 26 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, highest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 26 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, highest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 26 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, lowest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 26 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, highest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 26 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, lowest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 26 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, lowest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 26 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, lowest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 26 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, lowest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 26 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, lowest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 26 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, highest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 26 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, lowest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 26 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, highest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 26 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, highest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.
return [];
}

$errors = [];
foreach ($node->init as $expr) {
if (!($expr instanceof Assign)) {
foreach ($node->getWrites() as $write) {

Check failure on line 31 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, highest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 31 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, highest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 31 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, highest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 31 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, lowest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 31 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, highest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 31 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, lowest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 31 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, lowest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 31 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, lowest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 31 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, lowest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 31 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, lowest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 31 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, highest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 31 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, lowest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 31 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, highest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 31 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, highest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.
// only a for loop that takes over a variable assigned before the loop
// and read after it - reusing a spent loop variable is harmless
$loop = $node->getVariableOverwritingLoop($write);

Check failure on line 34 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, highest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 34 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, highest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 34 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, highest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 34 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, lowest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 34 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, highest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 34 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, lowest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 34 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, lowest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 34 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, lowest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 34 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, lowest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 34 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, lowest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 34 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, highest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 34 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, lowest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 34 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, highest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 34 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, highest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.
if (!$loop instanceof For_) {
continue;
}

foreach ($this->checkValueVar($scope, $expr->var) as $error) {
$errors[] = $error;
}
}

return $errors;
}

/**
* @return list<IdentifierRuleError>
*/
private function checkValueVar(Scope $scope, Expr $expr): array
{
$errors = [];
if (
$expr instanceof Node\Expr\Variable
&& is_string($expr->name)
&& $scope->hasVariableType($expr->name)->yes()
) {
$errors[] = RuleErrorBuilder::message(sprintf('For loop initial assignment overwrites variable $%s.', $expr->name))
$errors[] = RuleErrorBuilder::message(sprintf('For loop initial assignment overwrites variable $%s.', $write->getVariableName()))

Check failure on line 39 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, highest)

Calling PHPStan\Node\Variable\VariableWrite::getVariableName() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 39 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, highest)

Calling PHPStan\Node\Variable\VariableWrite::getVariableName() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 39 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, highest)

Calling PHPStan\Node\Variable\VariableWrite::getVariableName() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 39 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, lowest)

Calling PHPStan\Node\Variable\VariableWrite::getVariableName() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 39 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, highest)

Calling PHPStan\Node\Variable\VariableWrite::getVariableName() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 39 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, lowest)

Calling PHPStan\Node\Variable\VariableWrite::getVariableName() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 39 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, lowest)

Calling PHPStan\Node\Variable\VariableWrite::getVariableName() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 39 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, lowest)

Calling PHPStan\Node\Variable\VariableWrite::getVariableName() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 39 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, lowest)

Calling PHPStan\Node\Variable\VariableWrite::getVariableName() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 39 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, lowest)

Calling PHPStan\Node\Variable\VariableWrite::getVariableName() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 39 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, highest)

Calling PHPStan\Node\Variable\VariableWrite::getVariableName() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 39 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, lowest)

Calling PHPStan\Node\Variable\VariableWrite::getVariableName() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 39 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, highest)

Calling PHPStan\Node\Variable\VariableWrite::getVariableName() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 39 in src/Rules/ForLoop/OverwriteVariablesWithForLoopInitRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, highest)

Calling PHPStan\Node\Variable\VariableWrite::getVariableName() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.
->identifier('for.variableOverwrite')
->line($loop->getStartLine())
->build();
}

if (
$expr instanceof Node\Expr\List_
|| $expr instanceof Node\Expr\Array_
) {
foreach ($expr->items as $item) {
if ($item === null) {
continue;
}

foreach ($this->checkValueVar($scope, $item->value) as $error) {
$errors[] = $error;
}
}
}

return $errors;
}

Expand Down
70 changes: 22 additions & 48 deletions src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,49 @@
namespace PHPStan\Rules\ForeachLoop;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Foreach_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Node\Variable\VariableWrite;
use PHPStan\Node\VariableWritesNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function is_string;
use function sprintf;

/**
* @implements Rule<Foreach_>
* @implements Rule<VariableWritesNode>
*/
class OverwriteVariablesWithForeachRule implements Rule
{

public function getNodeType(): string
{
return Foreach_::class;
return VariableWritesNode::class;

Check failure on line 22 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, highest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 22 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, highest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 22 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, highest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 22 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, lowest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 22 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, highest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 22 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, lowest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 22 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, lowest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 22 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, lowest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 22 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, lowest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 22 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, lowest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 22 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, highest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 22 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, lowest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 22 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, highest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 22 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, highest)

Accessing PHPStan\Node\VariableWritesNode::class is not covered by backward compatibility promise. The class might change in a minor PHPStan version.
}

public function processNode(Node $node, Scope $scope): array
{
$errors = [];
if (
$node->keyVar instanceof Node\Expr\Variable
&& is_string($node->keyVar->name)
&& $scope->hasVariableType($node->keyVar->name)->yes()
) {
$errors[] = RuleErrorBuilder::message(sprintf('Foreach overwrites $%s with its key variable.', $node->keyVar->name))
->identifier('foreach.keyOverwrite')
->build();
}

foreach ($this->checkValueVar($scope, $node->valueVar) as $error) {
$errors[] = $error;
if ($node->isOpaque()) {

Check failure on line 27 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, highest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 27 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, highest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 27 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, highest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 27 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, lowest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 27 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, highest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 27 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, lowest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 27 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, lowest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 27 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, lowest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 27 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, lowest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 27 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, lowest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 27 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, highest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 27 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, lowest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 27 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, highest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 27 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, highest)

Calling PHPStan\Node\VariableWritesNode::isOpaque() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.
return [];
}

return $errors;
}

/**
* @return list<IdentifierRuleError>
*/
private function checkValueVar(Scope $scope, Expr $expr): array
{
$errors = [];
if (
$expr instanceof Node\Expr\Variable
&& is_string($expr->name)
&& $scope->hasVariableType($expr->name)->yes()
) {
$errors[] = RuleErrorBuilder::message(sprintf('Foreach overwrites $%s with its value variable.', $expr->name))
->identifier('foreach.valueOverwrite')
->build();
}

if (
$expr instanceof Node\Expr\List_
|| $expr instanceof Node\Expr\Array_
) {
foreach ($expr->items as $item) {
if ($item === null) {
continue;
}

foreach ($this->checkValueVar($scope, $item->value) as $error) {
$errors[] = $error;
}
foreach ($node->getWrites() as $write) {

Check failure on line 32 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, highest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 32 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, highest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 32 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, highest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 32 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, lowest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 32 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, highest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 32 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, lowest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 32 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, lowest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 32 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, lowest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 32 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, lowest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 32 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, lowest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 32 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, highest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 32 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, lowest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 32 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, highest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 32 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, highest)

Calling PHPStan\Node\VariableWritesNode::getWrites() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.
// only a foreach that takes over a variable assigned before the loop
// and read after it - reusing a spent loop variable is harmless
$loop = $node->getVariableOverwritingLoop($write);

Check failure on line 35 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, highest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 35 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, highest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 35 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, highest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 35 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, lowest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 35 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, highest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 35 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, lowest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 35 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, lowest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 35 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, lowest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 35 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, lowest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 35 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, lowest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 35 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, highest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 35 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, lowest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 35 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, highest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 35 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, highest)

Calling PHPStan\Node\VariableWritesNode::getVariableOverwritingLoop() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.
if (!$loop instanceof Foreach_) {
continue;
}

$isKey = $write->getKind() === VariableWrite::KIND_FOREACH_KEY;

Check failure on line 40 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, highest)

Accessing PHPStan\Node\Variable\VariableWrite::KIND_FOREACH_KEY is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 40 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, highest)

Accessing PHPStan\Node\Variable\VariableWrite::KIND_FOREACH_KEY is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 40 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, highest)

Accessing PHPStan\Node\Variable\VariableWrite::KIND_FOREACH_KEY is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 40 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, lowest)

Accessing PHPStan\Node\Variable\VariableWrite::KIND_FOREACH_KEY is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 40 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, highest)

Accessing PHPStan\Node\Variable\VariableWrite::KIND_FOREACH_KEY is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 40 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, lowest)

Accessing PHPStan\Node\Variable\VariableWrite::KIND_FOREACH_KEY is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 40 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, lowest)

Accessing PHPStan\Node\Variable\VariableWrite::KIND_FOREACH_KEY is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 40 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, lowest)

Accessing PHPStan\Node\Variable\VariableWrite::KIND_FOREACH_KEY is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 40 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, lowest)

Accessing PHPStan\Node\Variable\VariableWrite::KIND_FOREACH_KEY is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 40 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, lowest)

Accessing PHPStan\Node\Variable\VariableWrite::KIND_FOREACH_KEY is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 40 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, highest)

Accessing PHPStan\Node\Variable\VariableWrite::KIND_FOREACH_KEY is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 40 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, lowest)

Accessing PHPStan\Node\Variable\VariableWrite::KIND_FOREACH_KEY is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 40 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, highest)

Accessing PHPStan\Node\Variable\VariableWrite::KIND_FOREACH_KEY is not covered by backward compatibility promise. The class might change in a minor PHPStan version.

Check failure on line 40 in src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, highest)

Accessing PHPStan\Node\Variable\VariableWrite::KIND_FOREACH_KEY is not covered by backward compatibility promise. The class might change in a minor PHPStan version.
$errors[] = RuleErrorBuilder::message(sprintf(
'Foreach overwrites $%s with its %s variable.',
$write->getVariableName(),
$isKey ? 'key' : 'value',
))
->identifier($isKey ? 'foreach.keyOverwrite' : 'foreach.valueOverwrite')
->line($loop->getStartLine())
->build();
}

return $errors;
Expand Down
40 changes: 31 additions & 9 deletions tests/Rules/ForLoop/OverwriteVariablesWithForLoopInitRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,61 @@ public function testRule(): void
],
[
'For loop initial assignment overwrites variable $i.',
20,
21,
],
[
'For loop initial assignment overwrites variable $j.',
20,
21,
],
[
'For loop initial assignment overwrites variable $i.',
24,
26,
],
[
'For loop initial assignment overwrites variable $i.',
35,
38,
],
[
'For loop initial assignment overwrites variable $j.',
35,
38,
],
[
'For loop initial assignment overwrites variable $i.',
39,
43,
],
[
'For loop initial assignment overwrites variable $j.',
39,
43,
],
[
'For loop initial assignment overwrites variable $i.',
50,
55,
],
[
'For loop initial assignment overwrites variable $i.',
54,
60,
],
]);
}

public function testLoopVariableReuse(): void
{
$this->analyse([__DIR__ . '/data/for-reuse.php'], [
[
'For loop initial assignment overwrites variable $i.',
61,
],
[
'For loop initial assignment overwrites variable $i.',
70,
],
[
'For loop initial assignment overwrites variable $i.',
78,
],
[
'For loop initial assignment overwrites variable $i.',
87,
],
]);
}
Expand Down
9 changes: 8 additions & 1 deletion tests/Rules/ForLoop/data/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@ public function simple(int $i): void
for($j = 0; $j < 10; ++$j){

}
echo $i, $j;
}

public function multi(int $i, int $j): void
{
for($i = 0, $j = 0; $i < 10; ++$i){

}
echo $i, $j;

for($i = 0, $k = 0; $i < 10; ++$i){

}
echo $i, $k;

for($k = 0, $l = 0; $k < 10; ++$k){

Expand All @@ -35,10 +38,12 @@ public function list(int $i, int $j, array $b): void
for(list($i, $j) = $b; $i < 10; ++$i){

}
echo $i, $j;

for(list($i, list($j, $k)) = $b; $i < 10; ++$i){

}
echo $i, $j;

for(list($k, list($l, $m)) = $b; $k < 10; ++$k){

Expand All @@ -50,13 +55,15 @@ public function array(int $i, array $b): void
for([$i, $j] = $b; $i < 10; ++$i){

}
echo $i, $j;

for([$i, [$j, $k]] = $b; $i < 10; ++$i){

}
echo $i;

for([$k, [$l, $m]] = $b; $k < 10; ++$k){

}
}
}
}
Loading
Loading