Skip to content

Fix false positives on existing-offsets after assign #3874

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 2 commits into from
Mar 11, 2025
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
3 changes: 1 addition & 2 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5480,8 +5480,7 @@ private function processAssignVar(
}

if ($originalVar->dim instanceof Variable || $originalVar->dim instanceof Node\Scalar) {
$currentVarType = $scope->getType($originalVar);
if (!$originalValueToWrite->isSuperTypeOf($currentVarType)->yes()) {
if (!$scope->hasExpressionType($originalVar)->yes()) {
$scope = $scope->assignExpression(
$originalVar,
$originalValueToWrite,
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ private static function findTestFiles(): iterable
yield __DIR__ . '/../Rules/Classes/data/bug-11591-property-tag.php';
yield __DIR__ . '/../Rules/Classes/data/mixin-trait-use.php';

yield __DIR__ . '/../Rules/Arrays/data/bug-11679.php';
yield __DIR__ . '/../Rules/Methods/data/bug-4801.php';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,20 @@ public function testArrayDimFetchOnArrayKeyFirsOrLastOrCount(): void
]);
}

public function testBug12406(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;

$this->analyse([__DIR__ . '/data/bug-12406.php'], []);
}

public function testBug11679(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;

$this->analyse([__DIR__ . '/data/bug-11679.php'], []);
}

public function testBug8649(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
Expand Down
39 changes: 39 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-11679.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Bug11679;

use function PHPStan\Testing\assertType;

class WorkingExample
{
/** @var array{foo?: bool} */
private array $arr = [];

public function sayHello(): bool
{
assertType('array{foo?: bool}', $this->arr);
if (!isset($this->arr['foo'])) {
$this->arr['foo'] = true;
assertType('array{foo: true}', $this->arr);
}
assertType('array{foo: bool}', $this->arr);
return $this->arr['foo']; // PHPStan realizes optional 'foo' is set
}
}

class NonworkingExample
{
/** @var array<int, array{foo?: bool}> */
private array $arr = [];

public function sayHello(int $index): bool
{
assertType('array<int, array{foo?: bool}>', $this->arr);
if (!isset($this->arr[$index]['foo'])) {
$this->arr[$index]['foo'] = true;
assertType('non-empty-array<int, array{foo: true}>', $this->arr);
}
assertType('array<int, array{foo?: bool}>', $this->arr);
return $this->arr[$index]['foo']; // PHPStan does not realize 'foo' is set
}
}
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-12406.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Bug12406;

final class HelloWorld
{
/** @var array<string, int> */
protected array $words = [];

public function sayHello(string $word, int $count): void
{
$this->words[$word] ??= 0;
$this->words[$word] += $count;
}
}
Loading