Skip to content

Commit fbcfdf0

Browse files
committed
fix
1 parent 76a336b commit fbcfdf0

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,10 +708,9 @@ public function specifyTypesInCondition(
708708
&& ($numArg === null || $one->isSuperTypeOf($scope->getType($numArg))->yes())
709709
) {
710710
$dimFetch = new ArrayDimFetch($arrayArg, $expr->var);
711-
$iterableValueType = $arrayType->getFirstIterableValueType();
712711

713712
return $specifiedTypes->unionWith(
714-
$this->create($dimFetch, $iterableValueType, TypeSpecifierContext::createTrue(), $scope),
713+
$this->create($dimFetch, $arrayType->getIterableValueType(), TypeSpecifierContext::createTrue(), $scope),
715714
);
716715
}
717716
}

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ private static function findTestFiles(): iterable
6969
}
7070

7171
yield __DIR__ . '/../Rules/Methods/data/bug-6856.php';
72+
yield __DIR__ . '/../Rules/Arrays/data/bug-12981.php';
7273

7374
if (PHP_VERSION_ID < 80000) {
7475
yield __DIR__ . '/data/explode-php74.php';

tests/PHPStan/Rules/Arrays/data/bug-12981.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public function sayHello(array $arr): void
1212
{
1313
echo $arr[array_rand($arr)];
1414
$randIndex = array_rand($arr);
15+
assertType('bool|float|int|string', $arr[$randIndex]);
1516
echo $arr[$randIndex];
1617
}
1718

@@ -40,4 +41,21 @@ public function sayHello4(array $arr, int $num): void
4041
$randIndex = array_rand($arr, $num);
4142
echo $arr[$randIndex];
4243
}
44+
45+
public function sayHello5(): void
46+
{
47+
$arr = [
48+
1 => true,
49+
2 => false,
50+
'a' => 'hello',
51+
];
52+
assertType("'hello'|bool", $arr[array_rand($arr)]);
53+
54+
$arr = [
55+
1 => true,
56+
2 => null,
57+
'a' => 'hello',
58+
];
59+
assertType("'hello'|true|null", $arr[array_rand($arr)]);
60+
}
4361
}

0 commit comments

Comments
 (0)