Skip to content

Commit 537219b

Browse files
authored
MinMaxFunctionReturnTypeExtension: Cleanup instanceof ConstantScalarType
1 parent 24cdeac commit 537219b

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

Diff for: phpstan-baseline.neon

+1-6
Original file line numberDiff line numberDiff line change
@@ -1410,12 +1410,7 @@ parameters:
14101410

14111411
-
14121412
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
1413-
count: 4
1414-
path: src/Type/Php/MinMaxFunctionReturnTypeExtension.php
1415-
1416-
-
1417-
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantValue\\(\\) or Type\\:\\:generalize\\(\\) instead\\.$#"
1418-
count: 1
1413+
count: 2
14191414
path: src/Type/Php/MinMaxFunctionReturnTypeExtension.php
14201415

14211416
-

Diff for: src/Type/Php/MinMaxFunctionReturnTypeExtension.php

+8-9
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()
190-
&& $secondType instanceof ConstantScalarType
188+
$firstType->isArray()->yes()
189+
&& $secondType->isConstantScalarValue()->yes()
191190
) {
192191
return $secondType;
193192
}
194193

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

Diff for: tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php

+31-3
Original file line numberDiff line numberDiff line change
@@ -2363,14 +2363,42 @@ public function dataBinaryOperations(): array
23632363
'array<int>',
23642364
'max($arrayOfUnknownIntegers, $arrayOfUnknownIntegers)',
23652365
],
2366-
/*[
2367-
'array(1, 1, 1, 1)',
2366+
[
2367+
'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+
],
23702374
[
23712375
'array<int>',
2376+
'max($arrayOfUnknownIntegers, 5)',
2377+
],
2378+
[
2379+
'array<int>|int', // could be array<int>
23722380
'max($arrayOfUnknownIntegers, $integer, $arrayOfUnknownIntegers)',
2373-
],*/
2381+
],
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+
],
2398+
[
2399+
'5',
2400+
'min(array(2, 2, 2), 5, array(1, 1, 1, 1))',
2401+
],
23742402
[
23752403
'1.1',
23762404
'min(...[1.1, 2.2, 3.3])',

0 commit comments

Comments
 (0)