Skip to content

Fixed false-positive with by-ref parameters and array-functions #4036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 31, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5487,6 +5487,7 @@ private function processAssignVar(
$nativeValueToWrite = $scope->getNativeType($assignedExpr);
$originalValueToWrite = $valueToWrite;
$originalNativeValueToWrite = $nativeValueToWrite;
$scopeBeforeAssignEval = $scope;

// 3. eval assigned expr
$result = $processExprCallback($scope);
Expand Down Expand Up @@ -5542,11 +5543,11 @@ private function processAssignVar(

if ($varType->isArray()->yes() || !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->yes()) {
if ($var instanceof Variable && is_string($var->name)) {
$nodeCallback(new VariableAssignNode($var, $assignedPropertyExpr), $scope);
$nodeCallback(new VariableAssignNode($var, $assignedPropertyExpr), $scopeBeforeAssignEval);
$scope = $scope->assignVariable($var->name, $valueToWrite, $nativeValueToWrite, TrinaryLogic::createYes());
} else {
if ($var instanceof PropertyFetch || $var instanceof StaticPropertyFetch) {
$nodeCallback(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scope);
$nodeCallback(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scopeBeforeAssignEval);
if ($var instanceof PropertyFetch && $var->name instanceof Node\Identifier && !$isAssignOp) {
$scope = $scope->assignInitializedProperty($scope->getType($var->var), $var->name->toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ public function testBenevolentArrayKey(): void
$this->analyse([__DIR__ . '/data/benevolent-array-key.php'], []);
}

public function testBug13093(): void
{
$this->analyse([__DIR__ . '/data/bug-13093.php'], []);
}

}
40 changes: 40 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-13093.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Bug13093;

use function array_shift;
use function count;
use Generator;
use function PHPStan\debugScope;
use function PHPStan\dumpType;

class MutantProcessContainer {}

final class ParallelProcessRunner
{
/**
* @var array<int, MutantProcessContainer>
*/
private array $nextMutantProcessKillerContainer = [];

/**
* @param MutantProcessContainer[] $bucket
* @param Generator<MutantProcessContainer> $input
*/
public function fillBucketOnce(array &$bucket, Generator $input, int $threadCount): int
{
if (count($bucket) >= $threadCount || !$input->valid()) {
if ($this->nextMutantProcessKillerContainer !== []) {
$bucket[] = array_shift($this->nextMutantProcessKillerContainer);
}

return 0;
}

return 1;
}

}

Loading