From 9d6b4b6b3595258a6a953805b6aae57a8658f824 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Wed, 4 Sep 2024 21:21:33 +0200 Subject: [PATCH] Fix build --- composer.json | 2 +- src/Type/MatchesTypeSpecifyingExtension.php | 22 +-------- src/Type/TypeShapeReturnTypeExtension.php | 53 +++------------------ 3 files changed, 9 insertions(+), 68 deletions(-) diff --git a/composer.json b/composer.json index 95318ea..71f6c1a 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ ], "require": { "php": "^7.4 || ^8.0", - "phpstan/phpstan": "^1.7" + "phpstan/phpstan": "^1.12" }, "conflict": { "azjezz/psl": "<1.6||>=4.0" diff --git a/src/Type/MatchesTypeSpecifyingExtension.php b/src/Type/MatchesTypeSpecifyingExtension.php index f72da6d..9c8db42 100644 --- a/src/Type/MatchesTypeSpecifyingExtension.php +++ b/src/Type/MatchesTypeSpecifyingExtension.php @@ -10,7 +10,6 @@ use PHPStan\Analyser\TypeSpecifierContext; use PHPStan\Reflection\MethodReflection; use PHPStan\Type\MethodTypeSpecifyingExtension; -use PHPStan\Type\TypeWithClassName; use Psl\Type\TypeInterface; class MatchesTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension @@ -41,29 +40,10 @@ public function specifyTypes(MethodReflection $methodReflection, MethodCall $nod } $specType = $scope->getType($node->var); - if (!$specType instanceof TypeWithClassName) { - return new SpecifiedTypes(); - } - - $specTypeReflection = $specType->getClassReflection(); - if ($specTypeReflection === null) { - return new SpecifiedTypes(); - } - - $typeInterfaceAncestor = $specTypeReflection->getAncestorWithClassName(TypeInterface::class); - if ($typeInterfaceAncestor === null) { - return new SpecifiedTypes(); - } - - $typeMap = $typeInterfaceAncestor->getActiveTemplateTypeMap(); - $t = $typeMap->getType('T'); - if ($t === null) { - return new SpecifiedTypes(); - } return $this->typeSpecifier->create( $args[0]->value, - $t, + $specType->getTemplateType(TypeInterface::class, 'T'), $context ); } diff --git a/src/Type/TypeShapeReturnTypeExtension.php b/src/Type/TypeShapeReturnTypeExtension.php index 80d63b2..fdfa305 100644 --- a/src/Type/TypeShapeReturnTypeExtension.php +++ b/src/Type/TypeShapeReturnTypeExtension.php @@ -8,11 +8,11 @@ use PHPStan\Type\Constant\ConstantArrayType; use PHPStan\Type\Constant\ConstantArrayTypeBuilder; use PHPStan\Type\DynamicFunctionReturnTypeExtension; +use PHPStan\Type\ErrorType; use PHPStan\Type\Generic\GenericObjectType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; use PHPStan\Type\TypeUtils; -use PHPStan\Type\TypeWithClassName; use Psl\Type\Internal\OptionalType; use Psl\Type\TypeInterface; use function count; @@ -40,12 +40,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, $results = []; foreach ($arrays as $array) { - $result = $this->createResult($array); - if ($result === null) { - return null; - } - - $results[] = $result; + $results[] = $this->createResult($array); } return new GenericObjectType( @@ -56,32 +51,12 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, ); } - private function createResult(ConstantArrayType $arrayType): ?Type + private function createResult(ConstantArrayType $arrayType): Type { $builder = ConstantArrayTypeBuilder::createEmpty(); foreach ($arrayType->getKeyTypes() as $key) { $valueType = $arrayType->getOffsetValueType($key); - if (!$valueType instanceof TypeWithClassName) { - return null; - } - - $valueClassReflection = $valueType->getClassReflection(); - if ($valueClassReflection === null) { - return null; - } - - $typeInterfaceAncestor = $valueClassReflection->getAncestorWithClassName(TypeInterface::class); - if ($typeInterfaceAncestor === null) { - return null; - } - - $typeMap = $typeInterfaceAncestor->getActiveTemplateTypeMap(); - $t = $typeMap->getType('T'); - if ($t === null) { - return null; - } - - [$type, $optional] = $this->extractOptional($t); + [$type, $optional] = $this->extractOptional($valueType->getTemplateType(TypeInterface::class, 'T')); $builder->setOffsetValueType($key, $type, $optional); } @@ -94,26 +69,12 @@ private function createResult(ConstantArrayType $arrayType): ?Type */ private function extractOptional(Type $type): array { - if (!$type instanceof TypeWithClassName) { - return [$type, false]; - } - - $classReflection = $type->getClassReflection(); - if ($classReflection === null) { - return [$type, false]; - } - $optionalTypeAncestor = $classReflection->getAncestorWithClassName(OptionalType::class); - if ($optionalTypeAncestor === null) { - return [$type, false]; - } - - $typeMap = $optionalTypeAncestor->getActiveTemplateTypeMap(); - $t = $typeMap->getType('T'); - if ($t === null) { + $optionalType = $type->getTemplateType(OptionalType::class, 'T'); + if ($optionalType instanceof ErrorType) { return [$type, false]; } - return [$t, true]; + return [$optionalType, true]; } }