Skip to content

Commit b7a2dd1

Browse files
committed
infer non-empty-list/array after isset($arr[$i])
1 parent 8b96391 commit b7a2dd1

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

src/Analyser/TypeSpecifier.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,18 @@ public function specifyTypesInCondition(
10651065
&& !$scope->getType($var->var) instanceof MixedType
10661066
) {
10671067
$dimType = $scope->getType($var->dim);
1068+
$varType = $scope->getType($var->var);
1069+
1070+
if ($varType->isArray()->yes()) {
1071+
$types = $types->unionWith(
1072+
$this->create(
1073+
$var->var,
1074+
new NonEmptyArrayType(),
1075+
$context,
1076+
$scope,
1077+
)->setRootExpr($expr),
1078+
);
1079+
}
10681080

10691081
if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) {
10701082
$types = $types->unionWith(
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Bug13674a;
4+
5+
use function assert;
6+
use function PHPStan\Testing\assertType;
7+
8+
class HelloWorld
9+
{
10+
/**
11+
* @param array<int> $arrayA
12+
* @param list<int> $listA
13+
*/
14+
public function sayHello($arrayA, $listA, int $i): void
15+
{
16+
if (isset($arrayA[$i])) {
17+
assertType('non-empty-array<int>', $arrayA);
18+
} else {
19+
assertType('array<int>', $arrayA);
20+
}
21+
assertType('array<int>', $arrayA);
22+
23+
if (isset($listA[$i])) {
24+
assertType('non-empty-list<int>', $listA);
25+
} else {
26+
assertType('list<int>', $listA);
27+
}
28+
assertType('list<int>', $listA);
29+
}
30+
}

0 commit comments

Comments
 (0)