Skip to content

Commit 2c21ad1

Browse files
committed
Fix false positives on existing-offsets after assign
1 parent 54ca85b commit 2c21ad1

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5480,8 +5480,7 @@ private function processAssignVar(
54805480
}
54815481

54825482
if ($originalVar->dim instanceof Variable || $originalVar->dim instanceof Node\Scalar) {
5483-
$currentVarType = $scope->getType($originalVar);
5484-
if (!$originalValueToWrite->isSuperTypeOf($currentVarType)->yes()) {
5483+
if (!$scope->hasExpressionType($originalVar)->yes()) {
54855484
$scope = $scope->assignExpression(
54865485
$originalVar,
54875486
$originalValueToWrite,

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,13 @@ public function testArrayDimFetchOnArrayKeyFirsOrLastOrCount(): void
849849
]);
850850
}
851851

852+
public function testBug12406(): void
853+
{
854+
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
855+
856+
$this->analyse([__DIR__ . '/data/bug-12406.php'], []);
857+
}
858+
852859
public function testBug8649(): void
853860
{
854861
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Bug12406;
4+
5+
final class HelloWorld
6+
{
7+
/** @var array<string, int> */
8+
protected array $words = [];
9+
10+
public function sayHello(string $word, int $count): void
11+
{
12+
$this->words[$word] ??= 0;
13+
$this->words[$word] += $count;
14+
}
15+
}

0 commit comments

Comments
 (0)