Skip to content
Closed
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
15 changes: 14 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,20 @@ private function resolveType(string $exprString, Expr $node): Type
&& !$node instanceof Expr\ArrowFunction
&& $this->hasExpressionType($node)->yes()
) {
return $this->expressionTypes[$exprString]->getType();
$expressionType = $this->expressionTypes[$exprString]->getType();
if (
$node instanceof Expr\ArrayDimFetch
&& $node->dim instanceof Variable
&& $this->getType($node->var)->isArray()->yes()
) {
$dimType = $this->getType($node->dim);
$arrayType = $this->getType($node->var);
$computedType = $arrayType->getOffsetValueType($dimType);
if ($expressionType->isSuperTypeOf($computedType)->yes()) {
return $computedType;
}
}
return $expressionType;
}

if ($node instanceof AlwaysRememberedExpr) {
Expand Down
35 changes: 35 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-11705.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types = 1);

namespace Bug11705;

use function PHPStan\Testing\assertType;

/**
* @param array<string,string|array<int,string>> $theInput
* @phpstan-param array{'name':string,'owners':array<int,string>} $theInput
* @param array<int,string> $theTags
*/
function Example(array $theInput, array $theTags): void
{
foreach ($theTags as $tag) {
if (!array_key_exists($tag, $theInput)) {
continue;
}
switch ($tag) {
case 'name':
assertType("'name'", $tag);
assertType('string', $theInput[$tag]);
echo $theInput['name'];
if ($tag === 'name') {
echo "Of course it is...";
}
// After the always-true if, $tag should still be 'name'
assertType("'name'", $tag);
assertType('string', $theInput[$tag]);
echo $theInput[$tag];
break;
default:
// fall out
}
}
}
Loading