Skip to content
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
17 changes: 17 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6521,6 +6521,23 @@ private function enterForeach(MutatingScope $scope, MutatingScope $originalScope
}
}

if (
$stmt->expr instanceof FuncCall
&& $stmt->expr->name instanceof Name
&& $stmt->expr->name->toLowerString() === 'array_keys'
&& $stmt->valueVar instanceof Variable
) {
$args = $stmt->expr->getArgs();
if (count($args) >= 1) {
$arrayArg = $args[0]->value;
$scope = $scope->assignExpression(
new ArrayDimFetch($arrayArg, $stmt->valueVar),
$scope->getType($arrayArg)->getIterableValueType(),
$scope->getNativeType($arrayArg)->getIterableValueType(),
);
}
}

return $this->processVarAnnotation($scope, $vars, $stmt);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1000,4 +1000,11 @@ public function testBug10492(): void
]);
}

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

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

}
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-12926.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace Bug12926;

class HelloWorld
{
public function sayHello(array $arr): void
{
foreach (array_keys($arr) as $key) {
var_dump($arr[$key]);
}
}
}
Loading