Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 18 additions & 10 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4545,11 +4545,15 @@ private function inferForLoopExpressions(For_ $stmt, Expr $lastCondExpr, Mutatin
&& $stmt->init[0]->var->name === $lastCondExpr->left->name
) {
$arrayArg = $lastCondExpr->right->getArgs()[0]->value;
$bodyScope = $bodyScope->assignExpression(
new ArrayDimFetch($lastCondExpr->right->getArgs()[0]->value, $lastCondExpr->left),
$bodyScope->getType($arrayArg)->getIterableValueType(),
$bodyScope->getNativeType($arrayArg)->getIterableValueType(),
);
$arrayType = $bodyScope->getType($arrayArg);
$arrayNativeType = $bodyScope->getNativeType($arrayArg);
if ($arrayType->isList()->yes() || $arrayNativeType->isList()->yes()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking the arrayType (and not the arrayNative type) should be enough

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything is already up to date. The reviewer's feedback was already addressed by the da0f51464 ("simplify") commit, which removed the $arrayNativeType check and only uses $arrayType->isList()->yes() — exactly what VincentLanglet suggested.

$bodyScope = $bodyScope->assignExpression(
new ArrayDimFetch($lastCondExpr->right->getArgs()[0]->value, $lastCondExpr->left),
$arrayType->getIterableValueType(),
$arrayNativeType->getIterableValueType(),
);
}
}

// count($items) > $i
Expand All @@ -4566,11 +4570,15 @@ private function inferForLoopExpressions(For_ $stmt, Expr $lastCondExpr, Mutatin
&& $stmt->init[0]->var->name === $lastCondExpr->right->name
) {
$arrayArg = $lastCondExpr->left->getArgs()[0]->value;
$bodyScope = $bodyScope->assignExpression(
new ArrayDimFetch($lastCondExpr->left->getArgs()[0]->value, $lastCondExpr->right),
$bodyScope->getType($arrayArg)->getIterableValueType(),
$bodyScope->getNativeType($arrayArg)->getIterableValueType(),
);
$arrayType = $bodyScope->getType($arrayArg);
$arrayNativeType = $bodyScope->getNativeType($arrayArg);
if ($arrayType->isList()->yes() || $arrayNativeType->isList()->yes()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking the arrayType( and not the arrayNativeType) should be enough

$bodyScope = $bodyScope->assignExpression(
new ArrayDimFetch($lastCondExpr->left->getArgs()[0]->value, $lastCondExpr->right),
$arrayType->getIterableValueType(),
$arrayNativeType->getIterableValueType(),
);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1240,4 +1240,20 @@ public function testBug14234(): void
]);
}

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

$this->analyse([__DIR__ . '/data/bug-13773.php'], [
[
'Offset int<0, max> might not exist on non-empty-array<int, string>.',
14,
],
[
'Offset int<0, max> might not exist on non-empty-array<int, string>.',
38,
],
]);
}

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

namespace Bug13773;

/** @return array<int, string> */
function getArray(): array {
return [100 => "hey"];
}

function testNonListArray(): void
{
$array = getArray();
for ($i = 0; $i < count($array); $i++) {
$a = $array[$i];
}
}

/** @param list<string> $list */
function testList(array $list): void
{
for ($i = 0; $i < count($list); $i++) {
$a = $list[$i];
}
}

/** @param list<string> $list */
function testListReversed(array $list): void
{
for ($i = 0; count($list) > $i; ++$i) {
$a = $list[$i];
}
}

/** @param array<int, string> $array */
function testNonListReversed(array $array): void
{
for ($i = 0; count($array) > $i; ++$i) {
$a = $array[$i];
}
}
Loading