Skip to content

Commit

Permalink
Implement getConstantStrings() on Type
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Nov 9, 2022
1 parent f4a1214 commit 144304c
Show file tree
Hide file tree
Showing 63 changed files with 212 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/Analyser/MutatingScope.php
Expand Up @@ -2957,7 +2957,7 @@ private function expressionTypeIsUnchangeable(ExpressionTypeHolder $typeHolder):
&& $expr->name instanceof FullyQualified
&& $expr->name->toLowerString() === 'function_exists'
&& isset($expr->getArgs()[0])
&& count(TypeUtils::getConstantStrings($this->getType($expr->getArgs()[0]->value))) === 1
&& count($this->getType($expr->getArgs()[0]->value)->getConstantStrings()) === 1
&& (new ConstantBooleanType(true))->isSuperTypeOf($type)->yes();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/InitializerExprTypeResolver.php
Expand Up @@ -376,7 +376,7 @@ public function getConcatType(Expr $left, Expr $right, callable $getTypeCallback

// we limit the number of union-types for performance reasons
if ($leftStringType instanceof UnionType && count($leftStringType->getTypes()) <= 16 && $rightStringType instanceof ConstantStringType) {
$constantStrings = TypeUtils::getConstantStrings($leftStringType);
$constantStrings = $leftStringType->getConstantStrings();
if (count($constantStrings) > 0) {
$strings = [];
foreach ($constantStrings as $constantString) {
Expand All @@ -391,7 +391,7 @@ public function getConcatType(Expr $left, Expr $right, callable $getTypeCallback
}
}
if ($rightStringType instanceof UnionType && count($rightStringType->getTypes()) <= 16 && $leftStringType instanceof ConstantStringType) {
$constantStrings = TypeUtils::getConstantStrings($rightStringType);
$constantStrings = $rightStringType->getConstantStrings();
if (count($constantStrings) > 0) {
$strings = [];
foreach ($constantStrings as $constantString) {
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Classes/InstantiationRule.php
Expand Up @@ -234,7 +234,7 @@ private function getClassNames(Node $node, Scope $scope): array
return array_merge(
array_map(
static fn (ConstantStringType $type): array => [$type->getValue(), true],
TypeUtils::getConstantStrings($type),
$type->getConstantStrings(),
),
array_map(
static fn (string $name): array => [$name, false],
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/DeadCode/UnusedPrivateMethodRule.php
Expand Up @@ -14,7 +14,6 @@
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeUtils;
use function array_map;
use function count;
use function sprintf;
Expand Down Expand Up @@ -76,7 +75,7 @@ public function processNode(Node $node, Scope $scope): array
$methodNames = [$methodCallNode->name->toString()];
} else {
$methodNameType = $callScope->getType($methodCallNode->name);
$strings = TypeUtils::getConstantStrings($methodNameType);
$strings = $methodNameType->getConstantStrings();
if (count($strings) === 0) {
return [];
}
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/DeadCode/UnusedPrivatePropertyRule.php
Expand Up @@ -13,7 +13,6 @@
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeUtils;
use function array_key_exists;
use function array_map;
use function count;
Expand Down Expand Up @@ -125,7 +124,7 @@ public function processNode(Node $node, Scope $scope): array
$propertyNames = [$fetch->name->toString()];
} else {
$propertyNameType = $usage->getScope()->getType($fetch->name);
$strings = TypeUtils::getConstantStrings($propertyNameType);
$strings = $propertyNameType->getConstantStrings();
if (count($strings) === 0) {
return [];
}
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Functions/PrintfParametersRule.php
Expand Up @@ -9,7 +9,6 @@
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\TypeUtils;
use function array_filter;
use function count;
use function in_array;
Expand Down Expand Up @@ -73,7 +72,7 @@ public function processNode(Node $node, Scope $scope): array

$formatArgType = $scope->getType($args[$formatArgumentPosition]->value);
$placeHoldersCount = null;
foreach (TypeUtils::getConstantStrings($formatArgType) as $formatString) {
foreach ($formatArgType->getConstantStrings() as $formatString) {
$format = $formatString->getValue();
$tempPlaceHoldersCount = $this->getPlaceholdersCount($name, $format);
if ($placeHoldersCount === null) {
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Properties/AccessPropertiesRule.php
Expand Up @@ -16,7 +16,6 @@
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\VerbosityLevel;
use function array_map;
use function array_merge;
Expand Down Expand Up @@ -48,7 +47,7 @@ public function processNode(Node $node, Scope $scope): array
if ($node->name instanceof Identifier) {
$names = [$node->name->name];
} else {
$names = array_map(static fn (ConstantStringType $type): string => $type->getValue(), TypeUtils::getConstantStrings($scope->getType($node->name)));
$names = array_map(static fn (ConstantStringType $type): string => $type->getValue(), $scope->getType($node->name)->getConstantStrings());
}

$errors = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Properties/AccessStaticPropertiesRule.php
Expand Up @@ -54,7 +54,7 @@ public function processNode(Node $node, Scope $scope): array
if ($node->name instanceof Node\VarLikeIdentifier) {
$names = [$node->name->name];
} else {
$names = array_map(static fn (ConstantStringType $type): string => $type->getValue(), TypeUtils::getConstantStrings($scope->getType($node->name)));
$names = array_map(static fn (ConstantStringType $type): string => $type->getValue(), $scope->getType($node->name)->getConstantStrings());
}

$errors = [];
Expand Down
5 changes: 2 additions & 3 deletions src/Rules/Properties/PropertyReflectionFinder.php
Expand Up @@ -9,7 +9,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use function array_map;

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

$reflections = [];
Expand Down Expand Up @@ -58,7 +57,7 @@ public function findPropertyReflectionsFromNode($propertyFetch, Scope $scope): a
if ($propertyFetch->name instanceof VarLikeIdentifier) {
$names = [$propertyFetch->name->name];
} else {
$names = array_map(static fn (ConstantStringType $name): string => $name->getValue(), TypeUtils::getConstantStrings($scope->getType($propertyFetch->name)));
$names = array_map(static fn (ConstantStringType $name): string => $name->getValue(), $scope->getType($propertyFetch->name)->getConstantStrings());
}

$reflections = [];
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Regexp/RegularExpressionPatternRule.php
Expand Up @@ -10,7 +10,6 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\TypeUtils;
use function in_array;
use function sprintf;
use function str_starts_with;
Expand Down Expand Up @@ -65,7 +64,7 @@ private function extractPatterns(FuncCall $functionCall, Scope $scope): array

$patternStrings = [];

foreach (TypeUtils::getConstantStrings($patternType) as $constantStringType) {
foreach ($patternType->getConstantStrings() as $constantStringType) {
if (
!in_array($functionName, [
'preg_match',
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryArrayListType.php
Expand Up @@ -55,6 +55,11 @@ public function getConstantArrays(): array
return [];
}

public function getConstantStrings(): array
{
return [];
}

public function accepts(Type $type, bool $strictTypes): TrinaryLogic
{
if ($type instanceof CompoundType) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryLiteralStringType.php
Expand Up @@ -46,6 +46,11 @@ public function getReferencedClasses(): array
return [];
}

public function getConstantStrings(): array
{
return [];
}

public function accepts(Type $type, bool $strictTypes): TrinaryLogic
{
if ($type instanceof MixedType) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNonEmptyStringType.php
Expand Up @@ -46,6 +46,11 @@ public function getReferencedClasses(): array
return [];
}

public function getConstantStrings(): array
{
return [];
}

public function accepts(Type $type, bool $strictTypes): TrinaryLogic
{
if ($type instanceof CompoundType) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNonFalsyStringType.php
Expand Up @@ -47,6 +47,11 @@ public function getReferencedClasses(): array
return [];
}

public function getConstantStrings(): array
{
return [];
}

public function accepts(Type $type, bool $strictTypes): TrinaryLogic
{
if ($type instanceof CompoundType) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNumericStringType.php
Expand Up @@ -46,6 +46,11 @@ public function getReferencedClasses(): array
return [];
}

public function getConstantStrings(): array
{
return [];
}

public function accepts(Type $type, bool $strictTypes): TrinaryLogic
{
if ($type instanceof CompoundType) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetType.php
Expand Up @@ -58,6 +58,11 @@ public function getReferencedClasses(): array
return [];
}

public function getConstantStrings(): array
{
return [];
}

public function accepts(Type $type, bool $strictTypes): TrinaryLogic
{
if ($type instanceof CompoundType) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetValueType.php
Expand Up @@ -57,6 +57,11 @@ public function getReferencedClasses(): array
return [];
}

public function getConstantStrings(): array
{
return [];
}

public function accepts(Type $type, bool $strictTypes): TrinaryLogic
{
if ($type instanceof CompoundType) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasPropertyType.php
Expand Up @@ -39,6 +39,11 @@ public function getReferencedClasses(): array
return [];
}

public function getConstantStrings(): array
{
return [];
}

public function getPropertyName(): string
{
return $this->propertyName;
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/NonEmptyArrayType.php
Expand Up @@ -52,6 +52,11 @@ public function getConstantArrays(): array
return [];
}

public function getConstantStrings(): array
{
return [];
}

public function accepts(Type $type, bool $strictTypes): TrinaryLogic
{
if ($type instanceof CompoundType) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/OversizedArrayType.php
Expand Up @@ -51,6 +51,11 @@ public function getConstantArrays(): array
return [];
}

public function getConstantStrings(): array
{
return [];
}

public function accepts(Type $type, bool $strictTypes): TrinaryLogic
{
if ($type instanceof CompoundType) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/BooleanType.php
Expand Up @@ -38,6 +38,11 @@ public function __construct()
{
}

public function getConstantStrings(): array
{
return [];
}

public function describe(VerbosityLevel $level): string
{
return 'bool';
Expand Down
5 changes: 5 additions & 0 deletions src/Type/CallableType.php
Expand Up @@ -74,6 +74,11 @@ public function getReferencedClasses(): array
return array_merge($classes, $this->returnType->getReferencedClasses());
}

public function getConstantStrings(): array
{
return [];
}

public function accepts(Type $type, bool $strictTypes): TrinaryLogic
{
if ($type instanceof CompoundType && !$type instanceof self) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ClosureType.php
Expand Up @@ -230,6 +230,11 @@ public function getConstant(string $constantName): ConstantReflection
return $this->objectType->getConstant($constantName);
}

public function getConstantStrings(): array
{
return [];
}

public function isIterable(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Constant/ConstantStringType.php
Expand Up @@ -64,6 +64,11 @@ public function getValue(): string
return $this->value;
}

public function getConstantStrings(): array
{
return [$this];
}

public function isClassStringType(): TrinaryLogic
{
if ($this->isClassString) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/FloatType.php
Expand Up @@ -46,6 +46,11 @@ public function getReferencedClasses(): array
return [];
}

public function getConstantStrings(): array
{
return [];
}

public function accepts(Type $type, bool $strictTypes): TrinaryLogic
{
if ($type instanceof self || $type->isInteger()->yes()) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IntegerType.php
Expand Up @@ -41,6 +41,11 @@ public function describe(VerbosityLevel $level): string
return 'int';
}

public function getConstantStrings(): array
{
return [];
}

/**
* @param mixed[] $properties
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IntersectionType.php
Expand Up @@ -111,6 +111,11 @@ public function getConstantArrays(): array
return UnionTypeHelper::getConstantArrays($this->getTypes());
}

public function getConstantStrings(): array
{
return UnionTypeHelper::getConstantStrings($this->getTypes());
}

public function accepts(Type $otherType, bool $strictTypes): TrinaryLogic
{
foreach ($this->types as $type) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IterableType.php
Expand Up @@ -60,6 +60,11 @@ public function getReferencedClasses(): array
);
}

public function getConstantStrings(): array
{
return [];
}

public function accepts(Type $type, bool $strictTypes): TrinaryLogic
{
if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/MixedType.php
Expand Up @@ -73,6 +73,11 @@ public function getConstantArrays(): array
return [];
}

public function getConstantStrings(): array
{
return [];
}

public function accepts(Type $type, bool $strictTypes): TrinaryLogic
{
return TrinaryLogic::createYes();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/NeverType.php
Expand Up @@ -56,6 +56,11 @@ public function getConstantArrays(): array
return [];
}

public function getConstantStrings(): array
{
return [];
}

public function accepts(Type $type, bool $strictTypes): TrinaryLogic
{
return TrinaryLogic::createYes();
Expand Down

0 comments on commit 144304c

Please sign in to comment.