Skip to content

Commit e679f11

Browse files
committed
Type::isConstantScalarValue(), Type::getConstantScalarTypes(), and Type::getConstantScalarValues()
1 parent de1fa93 commit e679f11

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+612
-71
lines changed

phpstan-baseline.neon

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ parameters:
6161
count: 1
6262
path: src/Analyser/NodeScopeResolver.php
6363

64+
-
65+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
66+
count: 2
67+
path: src/Analyser/TypeSpecifier.php
68+
6469
-
6570
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantBooleanType is error\\-prone and deprecated\\. Use Type\\:\\:isTrue\\(\\) or Type\\:\\:isFalse\\(\\) instead\\.$#"
6671
count: 5
@@ -380,6 +385,11 @@ parameters:
380385
count: 1
381386
path: src/Reflection/InitializerExprTypeResolver.php
382387

388+
-
389+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
390+
count: 22
391+
path: src/Reflection/InitializerExprTypeResolver.php
392+
383393
-
384394
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
385395
count: 4
@@ -420,6 +430,11 @@ parameters:
420430
count: 1
421431
path: src/Rules/Api/NodeConnectingVisitorAttributesRule.php
422432

433+
-
434+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
435+
count: 1
436+
path: src/Rules/Arrays/DuplicateKeysInLiteralArraysRule.php
437+
423438
-
424439
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantBooleanType is error\\-prone and deprecated\\. Use Type\\:\\:isTrue\\(\\) or Type\\:\\:isFalse\\(\\) instead\\.$#"
425440
count: 2
@@ -688,6 +703,11 @@ parameters:
688703
count: 1
689704
path: src/Type/Accessory/HasOffsetType.php
690705

706+
-
707+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
708+
count: 2
709+
path: src/Type/Accessory/HasOffsetValueType.php
710+
691711
-
692712
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
693713
count: 2
@@ -763,6 +783,11 @@ parameters:
763783
count: 1
764784
path: src/Type/Constant/ConstantArrayType.php
765785

786+
-
787+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
788+
count: 2
789+
path: src/Type/Constant/ConstantArrayType.php
790+
766791
-
767792
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
768793
count: 8
@@ -803,16 +828,31 @@ parameters:
803828
count: 1
804829
path: src/Type/Constant/ConstantBooleanType.php
805830

831+
-
832+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
833+
count: 4
834+
path: src/Type/Constant/ConstantBooleanType.php
835+
806836
-
807837
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantBooleanType is error\\-prone and deprecated\\. Use Type\\:\\:isTrue\\(\\) or Type\\:\\:isFalse\\(\\) instead\\.$#"
808838
count: 3
809839
path: src/Type/Constant/ConstantBooleanType.php
810840

841+
-
842+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
843+
count: 4
844+
path: src/Type/Constant/ConstantFloatType.php
845+
811846
-
812847
message: "#^Doing instanceof PHPStan\\\\Type\\\\FloatType is error\\-prone and deprecated\\. Use Type\\:\\:isFloat\\(\\) instead\\.$#"
813848
count: 1
814849
path: src/Type/Constant/ConstantFloatType.php
815850

851+
-
852+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
853+
count: 4
854+
path: src/Type/Constant/ConstantIntegerType.php
855+
816856
-
817857
message: "#^Doing instanceof PHPStan\\\\Type\\\\IntegerType is error\\-prone and deprecated\\. Use Type\\:\\:isInteger\\(\\) instead\\.$#"
818858
count: 1
@@ -823,6 +863,11 @@ parameters:
823863
count: 1
824864
path: src/Type/Constant/ConstantStringType.php
825865

866+
-
867+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
868+
count: 4
869+
path: src/Type/Constant/ConstantStringType.php
870+
826871
-
827872
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
828873
count: 4
@@ -853,6 +898,11 @@ parameters:
853898
count: 2
854899
path: src/Type/Enum/EnumCaseObjectType.php
855900

901+
-
902+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
903+
count: 2
904+
path: src/Type/ExponentiateHelper.php
905+
856906
-
857907
message: "#^Doing instanceof PHPStan\\\\Type\\\\Generic\\\\GenericObjectType is error\\-prone and deprecated\\.$#"
858908
count: 1
@@ -1058,6 +1108,11 @@ parameters:
10581108
count: 2
10591109
path: src/Type/Generic/TemplateUnionType.php
10601110

1111+
-
1112+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
1113+
count: 1
1114+
path: src/Type/IntegerRangeType.php
1115+
10611116
-
10621117
message: "#^Doing instanceof PHPStan\\\\Type\\\\IntegerType is error\\-prone and deprecated\\. Use Type\\:\\:isInteger\\(\\) instead\\.$#"
10631118
count: 3
@@ -1103,6 +1158,11 @@ parameters:
11031158
count: 2
11041159
path: src/Type/IterableType.php
11051160

1161+
-
1162+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
1163+
count: 3
1164+
path: src/Type/NullType.php
1165+
11061166
-
11071167
message: "#^Doing instanceof PHPStan\\\\Type\\\\NullType is error\\-prone and deprecated\\. Use Type\\:\\:isNull\\(\\) instead\\.$#"
11081168
count: 3
@@ -1184,6 +1244,11 @@ parameters:
11841244
count: 1
11851245
path: src/Type/Php/ArraySearchFunctionDynamicReturnTypeExtension.php
11861246

1247+
-
1248+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
1249+
count: 16
1250+
path: src/Type/Php/BcMathStringOrNullReturnTypeExtension.php
1251+
11871252
-
11881253
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
11891254
count: 1
@@ -1214,6 +1279,11 @@ parameters:
12141279
count: 1
12151280
path: src/Type/Php/DsMapDynamicReturnTypeExtension.php
12161281

1282+
-
1283+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
1284+
count: 2
1285+
path: src/Type/Php/FilterFunctionReturnTypeHelper.php
1286+
12171287
-
12181288
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
12191289
count: 1
@@ -1224,6 +1294,19 @@ parameters:
12241294
count: 1
12251295
path: src/Type/Php/FunctionExistsFunctionTypeSpecifyingExtension.php
12261296

1297+
-
1298+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
1299+
count: 1
1300+
path: src/Type/Php/ImplodeFunctionReturnTypeExtension.php
1301+
1302+
-
1303+
message: """
1304+
#^Call to deprecated method getConstantScalars\\(\\) of class PHPStan\\\\Type\\\\TypeUtils\\:
1305+
Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\)$#
1306+
"""
1307+
count: 2
1308+
path: src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php
1309+
12271310
-
12281311
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
12291312
count: 1
@@ -1242,6 +1325,11 @@ parameters:
12421325
count: 1
12431326
path: src/Type/Php/JsonThrowOnErrorDynamicReturnTypeExtension.php
12441327

1328+
-
1329+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
1330+
count: 1
1331+
path: src/Type/Php/JsonThrowOnErrorDynamicReturnTypeExtension.php
1332+
12451333
-
12461334
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
12471335
count: 1
@@ -1267,11 +1355,21 @@ parameters:
12671355
count: 1
12681356
path: src/Type/Php/MethodExistsTypeSpecifyingExtension.php
12691357

1358+
-
1359+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
1360+
count: 4
1361+
path: src/Type/Php/MinMaxFunctionReturnTypeExtension.php
1362+
12701363
-
12711364
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
12721365
count: 2
12731366
path: src/Type/Php/MinMaxFunctionReturnTypeExtension.php
12741367

1368+
-
1369+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
1370+
count: 1
1371+
path: src/Type/Php/NumberFormatFunctionDynamicReturnTypeExtension.php
1372+
12751373
-
12761374
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
12771375
count: 1
@@ -1302,6 +1400,11 @@ parameters:
13021400
count: 1
13031401
path: src/Type/Php/ReflectionMethodConstructorThrowTypeExtension.php
13041402

1403+
-
1404+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
1405+
count: 1
1406+
path: src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php
1407+
13051408
-
13061409
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
13071410
count: 1
@@ -1317,6 +1420,11 @@ parameters:
13171420
count: 1
13181421
path: src/Type/Php/StrlenFunctionReturnTypeExtension.php
13191422

1423+
-
1424+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
1425+
count: 1
1426+
path: src/Type/Php/TriggerErrorDynamicReturnTypeExtension.php
1427+
13201428
-
13211429
message: "#^Doing instanceof PHPStan\\\\Type\\\\ObjectType is error\\-prone and deprecated\\. Use Type\\:\\:isObject\\(\\) or Type\\:\\:getObjectClassNames\\(\\) instead\\.$#"
13221430
count: 3
@@ -1362,6 +1470,11 @@ parameters:
13621470
count: 1
13631471
path: src/Type/TypeCombinator.php
13641472

1473+
-
1474+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
1475+
count: 1
1476+
path: src/Type/TypeCombinator.php
1477+
13651478
-
13661479
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#"
13671480
count: 12
@@ -1462,6 +1575,11 @@ parameters:
14621575
count: 3
14631576
path: src/Type/UnionTypeHelper.php
14641577

1578+
-
1579+
message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#"
1580+
count: 4
1581+
path: src/Type/UnionTypeHelper.php
1582+
14651583
-
14661584
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantBooleanType is error\\-prone and deprecated\\. Use Type\\:\\:isTrue\\(\\) or Type\\:\\:isFalse\\(\\) instead\\.$#"
14671585
count: 2

src/Analyser/MutatingScope.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,13 +1389,12 @@ private function resolveType(string $exprString, Expr $node): Type
13891389
return $this->getType($node->var);
13901390
} elseif ($node instanceof Expr\PreInc || $node instanceof Expr\PreDec) {
13911391
$varType = $this->getType($node->var);
1392-
$varScalars = TypeUtils::getConstantScalars($varType);
1392+
$varScalars = $varType->getConstantScalarValues();
13931393
$stringType = new StringType();
13941394
if (count($varScalars) > 0) {
13951395
$newTypes = [];
13961396

1397-
foreach ($varScalars as $scalar) {
1398-
$varValue = $scalar->getValue();
1397+
foreach ($varScalars as $varValue) {
13991398
if ($node instanceof Expr\PreInc) {
14001399
++$varValue;
14011400
} else {

src/Analyser/TypeSpecifier.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public function specifyTypesInCondition(
227227
$exprLeftType = $scope->getType($expr->left);
228228
$exprRightType = $scope->getType($expr->right);
229229
if (
230-
$exprLeftType instanceof ConstantScalarType
230+
count($exprLeftType->getConstantScalarValues()) === 1
231231
|| count($exprLeftType->getEnumCases()) === 1
232232
|| ($exprLeftType->isConstantValue()->yes() && !$exprRightType->equals($exprLeftType) && $exprRightType->isSuperTypeOf($exprLeftType)->yes())
233233
) {
@@ -241,7 +241,7 @@ public function specifyTypesInCondition(
241241
);
242242
}
243243
if (
244-
$exprRightType instanceof ConstantScalarType
244+
count($exprRightType->getConstantScalarValues()) === 1
245245
|| count($exprRightType->getEnumCases()) === 1
246246
|| ($exprRightType->isConstantValue()->yes() && !$exprLeftType->equals($exprRightType) && $exprLeftType->isSuperTypeOf($exprRightType)->yes())
247247
) {

0 commit comments

Comments
 (0)