Skip to content

Commit eec834e

Browse files
committed
Fixed false-positive with by-ref parameters and array-functions
1 parent d04535d commit eec834e

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5487,6 +5487,7 @@ private function processAssignVar(
54875487
$nativeValueToWrite = $scope->getNativeType($assignedExpr);
54885488
$originalValueToWrite = $valueToWrite;
54895489
$originalNativeValueToWrite = $nativeValueToWrite;
5490+
$scopeBeforeAssignEval = $scope;
54905491

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

55435544
if ($varType->isArray()->yes() || !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($varType)->yes()) {
55445545
if ($var instanceof Variable && is_string($var->name)) {
5545-
$nodeCallback(new VariableAssignNode($var, $assignedPropertyExpr), $scope);
5546+
$nodeCallback(new VariableAssignNode($var, $assignedPropertyExpr), $scopeBeforeAssignEval);
55465547
$scope = $scope->assignVariable($var->name, $valueToWrite, $nativeValueToWrite, TrinaryLogic::createYes());
55475548
} else {
55485549
if ($var instanceof PropertyFetch || $var instanceof StaticPropertyFetch) {
5549-
$nodeCallback(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scope);
5550+
$nodeCallback(new PropertyAssignNode($var, $assignedPropertyExpr, $isAssignOp), $scopeBeforeAssignEval);
55505551
if ($var instanceof PropertyFetch && $var->name instanceof Node\Identifier && !$isAssignOp) {
55515552
$scope = $scope->assignInitializedProperty($scope->getType($var->var), $var->name->toString());
55525553
}

tests/PHPStan/Rules/Variables/ParameterOutAssignedTypeRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ public function testBenevolentArrayKey(): void
6464
$this->analyse([__DIR__ . '/data/benevolent-array-key.php'], []);
6565
}
6666

67+
public function testBug13093(): void
68+
{
69+
$this->analyse([__DIR__ . '/data/bug-13093.php'], []);
70+
}
71+
6772
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bug13093;
6+
7+
use function array_shift;
8+
use function count;
9+
use Generator;
10+
use function PHPStan\debugScope;
11+
use function PHPStan\dumpType;
12+
13+
class MutantProcessContainer {}
14+
15+
final class ParallelProcessRunner
16+
{
17+
/**
18+
* @var array<int, MutantProcessContainer>
19+
*/
20+
private array $nextMutantProcessKillerContainer = [];
21+
22+
/**
23+
* @param MutantProcessContainer[] $bucket
24+
* @param Generator<MutantProcessContainer> $input
25+
*/
26+
public function fillBucketOnce(array &$bucket, Generator $input, int $threadCount): int
27+
{
28+
if (count($bucket) >= $threadCount || !$input->valid()) {
29+
if ($this->nextMutantProcessKillerContainer !== []) {
30+
$bucket[] = array_shift($this->nextMutantProcessKillerContainer);
31+
}
32+
33+
return 0;
34+
}
35+
36+
return 1;
37+
}
38+
39+
}
40+

0 commit comments

Comments
 (0)