Skip to content

Commit 11728d7

Browse files
committed
generalize array logic
1 parent f9fe72c commit 11728d7

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,11 +1428,6 @@ parameters:
14281428
count: 2
14291429
path: src/Type/Php/MinMaxFunctionReturnTypeExtension.php
14301430

1431-
-
1432-
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantValue\\(\\) or Type\\:\\:generalize\\(\\) instead\\.$#"
1433-
count: 1
1434-
path: src/Type/Php/MinMaxFunctionReturnTypeExtension.php
1435-
14361431
-
14371432
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
14381433
count: 2

src/Type/Php/MinMaxFunctionReturnTypeExtension.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use PHPStan\Type\Constant\ConstantArrayType;
1212
use PHPStan\Type\Constant\ConstantBooleanType;
1313
use PHPStan\Type\ConstantScalarType;
14-
use PHPStan\Type\ConstantType;
1514
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1615
use PHPStan\Type\ErrorType;
1716
use PHPStan\Type\Type;
@@ -152,16 +151,16 @@ private function processType(
152151
{
153152
$resultType = null;
154153
foreach ($types as $type) {
155-
if (!$type instanceof ConstantType) {
156-
return TypeCombinator::union(...$types);
157-
}
158-
159154
if ($resultType === null) {
160155
$resultType = $type;
161156
continue;
162157
}
163158

164159
$compareResult = $this->compareTypes($resultType, $type);
160+
if ($compareResult === null) {
161+
return TypeCombinator::union(...$types);
162+
}
163+
165164
if ($functionName === 'min') {
166165
if ($compareResult === $type) {
167166
$resultType = $type;
@@ -186,15 +185,15 @@ private function compareTypes(
186185
): ?Type
187186
{
188187
if (
189-
$firstType->isConstantArray()->yes()
188+
$firstType->isArray()->yes()
190189
&& $secondType->isConstantScalarValue()->yes()
191190
) {
192191
return $secondType;
193192
}
194193

195194
if (
196195
$firstType->isConstantScalarValue()->yes()
197-
&& $secondType->isConstantArray()->yes()
196+
&& $secondType->isArray()->yes()
198197
) {
199198
return $firstType;
200199
}

tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,10 +2367,34 @@ public function dataBinaryOperations(): array
23672367
'array{1, 1, 1, 1}',
23682368
'max(array(2, 2, 2), 5, array(1, 1, 1, 1))',
23692369
],
2370+
[
2371+
'array{int, int, int}',
2372+
'max($arrayOfIntegers, 5)',
2373+
],
2374+
[
2375+
'array<int>',
2376+
'max($arrayOfUnknownIntegers, 5)',
2377+
],
23702378
[
23712379
'array<int>|int', // could be array<int>
23722380
'max($arrayOfUnknownIntegers, $integer, $arrayOfUnknownIntegers)',
23732381
],
2382+
[
2383+
'array<int>',
2384+
'max($arrayOfUnknownIntegers, $conditionalInt)',
2385+
],
2386+
[
2387+
'5',
2388+
'min($arrayOfIntegers, 5)',
2389+
],
2390+
[
2391+
'5',
2392+
'min($arrayOfUnknownIntegers, 5)',
2393+
],
2394+
[
2395+
'1|2',
2396+
'min($arrayOfUnknownIntegers, $conditionalInt)',
2397+
],
23742398
[
23752399
'5',
23762400
'min(array(2, 2, 2), 5, array(1, 1, 1, 1))',

0 commit comments

Comments
 (0)