Skip to content

Commit 1b92af0

Browse files
authored
Implement getConstantStrings() on Type
1 parent 926ec9d commit 1b92af0

File tree

64 files changed

+214
-58
lines changed

Some content is hidden

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

64 files changed

+214
-58
lines changed

src/Analyser/MutatingScope.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2971,7 +2971,7 @@ private function expressionTypeIsUnchangeable(ExpressionTypeHolder $typeHolder):
29712971
&& $expr->name instanceof FullyQualified
29722972
&& $expr->name->toLowerString() === 'function_exists'
29732973
&& isset($expr->getArgs()[0])
2974-
&& count(TypeUtils::getConstantStrings($this->getType($expr->getArgs()[0]->value))) === 1
2974+
&& count($this->getType($expr->getArgs()[0]->value)->getConstantStrings()) === 1
29752975
&& (new ConstantBooleanType(true))->isSuperTypeOf($type)->yes();
29762976
}
29772977

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ public function getConcatType(Expr $left, Expr $right, callable $getTypeCallback
376376

377377
// we limit the number of union-types for performance reasons
378378
if ($leftStringType instanceof UnionType && count($leftStringType->getTypes()) <= 16 && $rightStringType instanceof ConstantStringType) {
379-
$constantStrings = TypeUtils::getConstantStrings($leftStringType);
379+
$constantStrings = $leftStringType->getConstantStrings();
380380
if (count($constantStrings) > 0) {
381381
$strings = [];
382382
foreach ($constantStrings as $constantString) {
@@ -391,7 +391,7 @@ public function getConcatType(Expr $left, Expr $right, callable $getTypeCallback
391391
}
392392
}
393393
if ($rightStringType instanceof UnionType && count($rightStringType->getTypes()) <= 16 && $leftStringType instanceof ConstantStringType) {
394-
$constantStrings = TypeUtils::getConstantStrings($rightStringType);
394+
$constantStrings = $rightStringType->getConstantStrings();
395395
if (count($constantStrings) > 0) {
396396
$strings = [];
397397
foreach ($constantStrings as $constantString) {

src/Rules/Classes/InstantiationRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ private function getClassNames(Node $node, Scope $scope): array
234234
return array_merge(
235235
array_map(
236236
static fn (ConstantStringType $type): array => [$type->getValue(), true],
237-
TypeUtils::getConstantStrings($type),
237+
$type->getConstantStrings(),
238238
),
239239
array_map(
240240
static fn (string $name): array => [$name, false],

src/Rules/DeadCode/UnusedPrivateMethodRule.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PHPStan\Type\Constant\ConstantStringType;
1515
use PHPStan\Type\MixedType;
1616
use PHPStan\Type\ObjectType;
17-
use PHPStan\Type\TypeUtils;
1817
use function array_map;
1918
use function count;
2019
use function sprintf;
@@ -76,7 +75,7 @@ public function processNode(Node $node, Scope $scope): array
7675
$methodNames = [$methodCallNode->name->toString()];
7776
} else {
7877
$methodNameType = $callScope->getType($methodCallNode->name);
79-
$strings = TypeUtils::getConstantStrings($methodNameType);
78+
$strings = $methodNameType->getConstantStrings();
8079
if (count($strings) === 0) {
8180
return [];
8281
}

src/Rules/DeadCode/UnusedPrivatePropertyRule.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use PHPStan\Type\Constant\ConstantStringType;
1414
use PHPStan\Type\MixedType;
1515
use PHPStan\Type\ObjectType;
16-
use PHPStan\Type\TypeUtils;
1716
use function array_key_exists;
1817
use function array_map;
1918
use function count;
@@ -125,7 +124,7 @@ public function processNode(Node $node, Scope $scope): array
125124
$propertyNames = [$fetch->name->toString()];
126125
} else {
127126
$propertyNameType = $usage->getScope()->getType($fetch->name);
128-
$strings = TypeUtils::getConstantStrings($propertyNameType);
127+
$strings = $propertyNameType->getConstantStrings();
129128
if (count($strings) === 0) {
130129
return [];
131130
}

src/Rules/Functions/PrintfParametersRule.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PHPStan\Php\PhpVersion;
1010
use PHPStan\Rules\Rule;
1111
use PHPStan\Rules\RuleErrorBuilder;
12-
use PHPStan\Type\TypeUtils;
1312
use function array_filter;
1413
use function count;
1514
use function in_array;
@@ -73,7 +72,7 @@ public function processNode(Node $node, Scope $scope): array
7372

7473
$formatArgType = $scope->getType($args[$formatArgumentPosition]->value);
7574
$placeHoldersCount = null;
76-
foreach (TypeUtils::getConstantStrings($formatArgType) as $formatString) {
75+
foreach ($formatArgType->getConstantStrings() as $formatString) {
7776
$format = $formatString->getValue();
7877
$tempPlaceHoldersCount = $this->getPlaceholdersCount($name, $format);
7978
if ($placeHoldersCount === null) {

src/Rules/Properties/AccessPropertiesRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function processNode(Node $node, Scope $scope): array
4848
if ($node->name instanceof Identifier) {
4949
$names = [$node->name->name];
5050
} else {
51-
$names = array_map(static fn (ConstantStringType $type): string => $type->getValue(), TypeUtils::getConstantStrings($scope->getType($node->name)));
51+
$names = array_map(static fn (ConstantStringType $type): string => $type->getValue(), $scope->getType($node->name)->getConstantStrings());
5252
}
5353

5454
$errors = [];

src/Rules/Properties/AccessStaticPropertiesRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function processNode(Node $node, Scope $scope): array
5555
if ($node->name instanceof Node\VarLikeIdentifier) {
5656
$names = [$node->name->name];
5757
} else {
58-
$names = array_map(static fn (ConstantStringType $type): string => $type->getValue(), TypeUtils::getConstantStrings($scope->getType($node->name)));
58+
$names = array_map(static fn (ConstantStringType $type): string => $type->getValue(), $scope->getType($node->name)->getConstantStrings());
5959
}
6060

6161
$errors = [];

src/Rules/Properties/PropertyReflectionFinder.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PHPStan\Analyser\Scope;
1010
use PHPStan\Type\Constant\ConstantStringType;
1111
use PHPStan\Type\Type;
12-
use PHPStan\Type\TypeUtils;
1312
use function array_map;
1413

1514
class PropertyReflectionFinder
@@ -25,7 +24,7 @@ public function findPropertyReflectionsFromNode($propertyFetch, Scope $scope): a
2524
if ($propertyFetch->name instanceof Node\Identifier) {
2625
$names = [$propertyFetch->name->name];
2726
} else {
28-
$names = array_map(static fn (ConstantStringType $name): string => $name->getValue(), TypeUtils::getConstantStrings($scope->getType($propertyFetch->name)));
27+
$names = array_map(static fn (ConstantStringType $name): string => $name->getValue(), $scope->getType($propertyFetch->name)->getConstantStrings());
2928
}
3029

3130
$reflections = [];
@@ -58,7 +57,7 @@ public function findPropertyReflectionsFromNode($propertyFetch, Scope $scope): a
5857
if ($propertyFetch->name instanceof VarLikeIdentifier) {
5958
$names = [$propertyFetch->name->name];
6059
} else {
61-
$names = array_map(static fn (ConstantStringType $name): string => $name->getValue(), TypeUtils::getConstantStrings($scope->getType($propertyFetch->name)));
60+
$names = array_map(static fn (ConstantStringType $name): string => $name->getValue(), $scope->getType($propertyFetch->name)->getConstantStrings());
6261
}
6362

6463
$reflections = [];

src/Rules/Regexp/RegularExpressionPatternRule.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PHPStan\Rules\Rule;
1111
use PHPStan\Rules\RuleErrorBuilder;
1212
use PHPStan\Type\Constant\ConstantStringType;
13-
use PHPStan\Type\TypeUtils;
1413
use function in_array;
1514
use function sprintf;
1615
use function str_starts_with;
@@ -65,7 +64,7 @@ private function extractPatterns(FuncCall $functionCall, Scope $scope): array
6564

6665
$patternStrings = [];
6766

68-
foreach (TypeUtils::getConstantStrings($patternType) as $constantStringType) {
67+
foreach ($patternType->getConstantStrings() as $constantStringType) {
6968
if (
7069
!in_array($functionName, [
7170
'preg_match',

0 commit comments

Comments
 (0)