Skip to content

Commit ec3400f

Browse files
staabmondrejmirtes
authored andcommitted
more tests
1 parent 97934fe commit ec3400f

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,14 +1007,27 @@ private function specifyTypesForConstantBinaryExpression(
10071007
) {
10081008
$result = [];
10091009
foreach ($argType->getTypes() as $innerType) {
1010-
if (!$innerType->getArraySize()->isSuperTypeOf($constantType)->yes()) {
1010+
if (!$innerType->isConstantArray()->yes()) {
1011+
$result = null;
1012+
break;
1013+
}
1014+
1015+
$arraySize = $innerType->getArraySize();
1016+
if (!$arraySize instanceof ConstantIntegerType) {
1017+
$result = null;
1018+
break;
1019+
}
1020+
1021+
if (!$constantType->isSuperTypeOf($arraySize)->yes()) {
10111022
continue;
10121023
}
10131024

10141025
$result[] = $innerType;
10151026
}
10161027

1017-
return $this->create($exprNode->getArgs()[0]->value, TypeCombinator::union(...$result), $context, false, $scope, $rootExpr);
1028+
if ($result !== null) {
1029+
return $this->create($exprNode->getArgs()[0]->value, TypeCombinator::union(...$result), $context, false, $scope, $rootExpr);
1030+
}
10181031
}
10191032

10201033
if ($context->truthy() || $constantType->getValue() === 0) {

tests/PHPStan/Analyser/nsrt/narrow-tagged-union.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,42 @@ public function nestedArrays(array $arr): void
7878
}
7979
assertType("array{array<int>, numeric-string}|array{string, '', non-empty-string}", $arr);
8080
}
81+
82+
/** @param array{string, '', non-empty-string}|array{array<int>, numeric-string} $arr */
83+
public function nonArray(array $arr): void
84+
{
85+
// don't narrow when $arr contains recursive arrays
86+
if (count($arr, COUNT_RECURSIVE) === 3) {
87+
assertType("array{array<int>, numeric-string}|array{string, '', non-empty-string}", $arr);
88+
} else {
89+
assertType("array{array<int>, numeric-string}|array{string, '', non-empty-string}", $arr);
90+
}
91+
assertType("array{array<int>, numeric-string}|array{string, '', non-empty-string}", $arr);
92+
93+
if (count($arr, COUNT_NORMAL) === 3) {
94+
assertType("array{string, '', non-empty-string}", $arr);
95+
} else {
96+
assertType("array{array<int>, numeric-string}", $arr);
97+
}
98+
assertType("array{array<int>, numeric-string}|array{string, '', non-empty-string}", $arr);
99+
}
100+
101+
public function arrayIntRangeSize(): void
102+
{
103+
$x = [];
104+
if (rand(0,1)) {
105+
$x[] = 'ab';
106+
}
107+
if (rand(0,1)) {
108+
$x[] = 'xy';
109+
}
110+
111+
if (count($x) === 1) {
112+
assertType("array{'xy'}|array{0: 'ab', 1?: 'xy'}", $x);
113+
} else {
114+
assertType("array{}|array{'xy'}|array{0: 'ab', 1?: 'xy'}", $x);
115+
}
116+
assertType("array{}|array{'xy'}|array{0: 'ab', 1?: 'xy'}", $x);
117+
}
81118
}
82119

0 commit comments

Comments
 (0)