Skip to content

Fix false positives on existing-offsets after assign #3893

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 6 commits into from
Mar 22, 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
7 changes: 6 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5496,7 +5496,12 @@ private function processAssignVar(
}

if ($originalVar->dim instanceof Variable || $originalVar->dim instanceof Node\Scalar) {
if (!$scope->hasExpressionType($originalVar)->yes()) {
$currentVarType = $scope->getType($originalVar);
$currentVarNativeType = $scope->getNativeType($originalVar);
if (
!$originalValueToWrite->isSuperTypeOf($currentVarType)->yes()
|| !$originalNativeValueToWrite->isSuperTypeOf($currentVarNativeType)->yes()
) {
$scope = $scope->assignExpression(
$originalVar,
$originalValueToWrite,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,22 @@ public function testBug12406(): void
$this->analyse([__DIR__ . '/data/bug-12406.php'], []);
}

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

$this->analyse([__DIR__ . '/data/bug-12406b.php'], [
[
'Offset int<0, max> might not exist on non-empty-list<array{0: string, 1: non-empty-string, 2: non-falsy-string, 3: numeric-string, 4?: numeric-string, 5?: numeric-string}>.',
22,
],
[
'Offset int<0, max> might not exist on non-empty-list<array{0: string, 1: non-empty-string, 2: non-falsy-string, 3: numeric-string, 4?: numeric-string, 5?: numeric-string}>.',
23,
],
]);
}

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

namespace Bug12406_2;

final class Foo
{

public function sayHello(string $s): void
{
$stats = [];
preg_match_all('~
commit[ ][a-z0-9]{40}\\n
Author:\s+(.+)\s+<[^>]+>\\n
AuthorDate:[^\\n]+\\n
Commit:[^\\n]+\\n
CommitDate:[^\\n]+\\n\\n
(\s+(?:[^\n]+\n)+)\n
[ ](\\d+)[ ]files?[ ]changed,(?:[ ](\\d+)[ ]insertions?\\(\\+\\),?)?(?:[ ](\\d+)[ ]deletions?\\(-\\))?
~mx', $s, $matches, PREG_SET_ORDER);

for ($i = 0; $i < count($matches); $i++) {
$author = $matches[$i][1];
$files = (int) $matches[$i][3];
$insertions = (int) ($matches[$i][4] ?? 0);
$deletions = (int) ($matches[$i][5] ?? 0);

$stats[$author]['commits'] = ($stats[$author]['commits'] ?? 0) + 1;
$stats[$author]['files'] = ($stats[$author]['files'] ?? 0) + $files;
$stats[$author]['insertions'] = ($stats[$author]['insertions'] ?? 0) + $insertions;
$stats[$author]['deletions'] = ($stats[$author]['deletions'] ?? 0) + $deletions;
$stats[$author]['diff'] = ($stats[$author]['diff'] ?? 0) + $insertions - $deletions;
}
}

}
Loading