Skip to content

Commit 1492332

Browse files
authored
False positive "non-empty-array<int, int> might not be a list" when change existing list key
1 parent ac71c94 commit 1492332

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5609,11 +5609,24 @@ private function processAssignVar(
56095609
} else {
56105610
$varForSetOffsetValue = $var->var;
56115611
}
5612-
$assignedPropertyExpr = new SetOffsetValueTypeExpr(
5613-
$varForSetOffsetValue,
5614-
$var->dim,
5615-
$assignedPropertyExpr,
5616-
);
5612+
5613+
if (
5614+
$var === $originalVar
5615+
&& $var->dim !== null
5616+
&& $scope->hasExpressionType($var)->yes()
5617+
) {
5618+
$assignedPropertyExpr = new SetExistingOffsetValueTypeExpr(
5619+
$varForSetOffsetValue,
5620+
$var->dim,
5621+
$assignedPropertyExpr,
5622+
);
5623+
} else {
5624+
$assignedPropertyExpr = new SetOffsetValueTypeExpr(
5625+
$varForSetOffsetValue,
5626+
$var->dim,
5627+
$assignedPropertyExpr,
5628+
);
5629+
}
56175630
$dimFetchStack[] = $var;
56185631
$var = $var->var;
56195632
}

tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,4 +935,11 @@ public function testBug11777(): void
935935
$this->analyse([__DIR__ . '/data/bug-11777.php'], []);
936936
}
937937

938+
public function testBug13035(): void
939+
{
940+
$this->checkExplicitMixed = true;
941+
$this->checkImplicitMixed = true;
942+
$this->analyse([__DIR__ . '/data/bug-13035.php'], []);
943+
}
944+
938945
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Bug13035;
4+
5+
use function PHPStan\debugScope;
6+
use function PHPStan\Testing\assertType;
7+
8+
class HelloWorld
9+
{
10+
/**
11+
* @var list<int>
12+
*/
13+
public array $list = [];
14+
15+
public function bug(int $offset): void
16+
{
17+
if (isset($this->list[$offset])) {
18+
$this->list[$offset] = 123;
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)