From 714877be8cafc1ba08610929e4dcb0d43273cc8d Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 26 Sep 2024 13:42:29 +0200 Subject: [PATCH] Introduce `@internal` `getOnlyVariant()` method on FunctionReflection/ExtendedMethodReflection to use instead of `selectSingle()` --- src/Analyser/MutatingScope.php | 2 +- .../AnnotationMethodReflection.php | 6 +++++ src/Reflection/ClassReflection.php | 2 +- .../Dummy/ChangedTypeMethodReflection.php | 12 +++++++++ .../Dummy/DummyConstructorReflection.php | 6 +++++ .../Dummy/DummyMethodReflection.php | 6 +++++ src/Reflection/ExtendedMethodReflection.php | 5 ++++ src/Reflection/FunctionReflection.php | 5 ++++ .../Native/NativeFunctionReflection.php | 15 ++++++++--- .../Native/NativeMethodReflection.php | 12 +++++++++ .../Php/ClosureCallMethodReflection.php | 6 +++++ .../Php/EnumCasesMethodReflection.php | 6 +++++ src/Reflection/Php/ExitFunctionReflection.php | 5 ++++ .../PhpFunctionFromParserNodeReflection.php | 5 ++++ src/Reflection/Php/PhpFunctionReflection.php | 5 ++++ src/Reflection/Php/PhpMethodReflection.php | 5 ++++ ...alObjectCratesClassReflectionExtension.php | 5 ++-- src/Reflection/ResolvedMethodReflection.php | 5 ++++ .../Type/IntersectionTypeMethodReflection.php | 11 ++++++++ .../Type/UnionTypeMethodReflection.php | 6 +++++ .../WrappedExtendedMethodReflection.php | 5 ++++ src/Rules/Methods/MethodSignatureRule.php | 9 +++---- src/Type/ObjectType.php | 25 ++++++------------- .../Analyser/AnalyserIntegrationTest.php | 5 ++-- .../ArgumentsNormalizerLegacyTest.php | 9 +++---- ...onsMethodsClassReflectionExtensionTest.php | 3 +-- .../Reflection/ClassReflectionTest.php | 2 +- tests/PHPStan/Reflection/MixedTypeTest.php | 4 +-- 28 files changed, 149 insertions(+), 43 deletions(-) diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 135a0deae9..086a09c3c2 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -5514,7 +5514,7 @@ private function exactInstantiation(New_ $node, string $className): ?Type $assignedToProperty = $node->getAttribute(NewAssignedToPropertyVisitor::ATTRIBUTE_NAME); if ($assignedToProperty !== null) { - $constructorVariant = ParametersAcceptorSelector::selectSingle($constructorMethod->getVariants()); + $constructorVariant = $constructorMethod->getOnlyVariant(); $classTemplateTypes = $classReflection->getTemplateTypeMap()->getTypes(); $originalClassTemplateTypes = $classTemplateTypes; foreach ($constructorVariant->getParameters() as $parameter) { diff --git a/src/Reflection/Annotations/AnnotationMethodReflection.php b/src/Reflection/Annotations/AnnotationMethodReflection.php index 0486d11048..b4065332ce 100644 --- a/src/Reflection/Annotations/AnnotationMethodReflection.php +++ b/src/Reflection/Annotations/AnnotationMethodReflection.php @@ -7,6 +7,7 @@ use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ExtendedMethodReflection; use PHPStan\Reflection\FunctionVariantWithPhpDocs; +use PHPStan\Reflection\ParametersAcceptorWithPhpDocs; use PHPStan\TrinaryLogic; use PHPStan\Type\Generic\TemplateTypeMap; use PHPStan\Type\MixedType; @@ -83,6 +84,11 @@ public function getVariants(): array return $this->variants; } + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs + { + return $this->getVariants()[0]; + } + public function getNamedArgumentsVariants(): ?array { return null; diff --git a/src/Reflection/ClassReflection.php b/src/Reflection/ClassReflection.php index 9527e526b7..d980be7864 100644 --- a/src/Reflection/ClassReflection.php +++ b/src/Reflection/ClassReflection.php @@ -1305,7 +1305,7 @@ private function findAttributeFlags(): ?int return null; } $attributeConstructor = $attributeClass->getConstructor(); - $attributeConstructorVariant = ParametersAcceptorSelector::selectSingle($attributeConstructor->getVariants()); + $attributeConstructorVariant = $attributeConstructor->getOnlyVariant(); if (count($arguments) === 0) { $flagType = $attributeConstructorVariant->getParameters()[0]->getDefaultValue(); diff --git a/src/Reflection/Dummy/ChangedTypeMethodReflection.php b/src/Reflection/Dummy/ChangedTypeMethodReflection.php index 69d7a7a1a6..b81e30e846 100644 --- a/src/Reflection/Dummy/ChangedTypeMethodReflection.php +++ b/src/Reflection/Dummy/ChangedTypeMethodReflection.php @@ -7,8 +7,10 @@ use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ExtendedMethodReflection; use PHPStan\Reflection\ParametersAcceptorWithPhpDocs; +use PHPStan\ShouldNotHappenException; use PHPStan\TrinaryLogic; use PHPStan\Type\Type; +use function count; use function is_bool; final class ChangedTypeMethodReflection implements ExtendedMethodReflection @@ -62,6 +64,16 @@ public function getVariants(): array return $this->variants; } + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs + { + $variants = $this->getVariants(); + if (count($variants) !== 1) { + throw new ShouldNotHappenException(); + } + + return $variants[0]; + } + public function getNamedArgumentsVariants(): ?array { return $this->namedArgumentsVariants; diff --git a/src/Reflection/Dummy/DummyConstructorReflection.php b/src/Reflection/Dummy/DummyConstructorReflection.php index 7f81cd1363..ca67c3c2e2 100644 --- a/src/Reflection/Dummy/DummyConstructorReflection.php +++ b/src/Reflection/Dummy/DummyConstructorReflection.php @@ -7,6 +7,7 @@ use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ExtendedMethodReflection; use PHPStan\Reflection\FunctionVariantWithPhpDocs; +use PHPStan\Reflection\ParametersAcceptorWithPhpDocs; use PHPStan\TrinaryLogic; use PHPStan\Type\Generic\TemplateTypeMap; use PHPStan\Type\MixedType; @@ -66,6 +67,11 @@ public function getVariants(): array ]; } + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs + { + return $this->getVariants()[0]; + } + public function getNamedArgumentsVariants(): ?array { return null; diff --git a/src/Reflection/Dummy/DummyMethodReflection.php b/src/Reflection/Dummy/DummyMethodReflection.php index 79e85fda5b..ba4faeded3 100644 --- a/src/Reflection/Dummy/DummyMethodReflection.php +++ b/src/Reflection/Dummy/DummyMethodReflection.php @@ -6,6 +6,7 @@ use PHPStan\Reflection\ClassMemberReflection; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ExtendedMethodReflection; +use PHPStan\Reflection\ParametersAcceptorWithPhpDocs; use PHPStan\Reflection\ReflectionProviderStaticAccessor; use PHPStan\Reflection\TrivialParametersAcceptor; use PHPStan\TrinaryLogic; @@ -58,6 +59,11 @@ public function getVariants(): array ]; } + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs + { + return $this->getVariants()[0]; + } + public function getNamedArgumentsVariants(): ?array { return null; diff --git a/src/Reflection/ExtendedMethodReflection.php b/src/Reflection/ExtendedMethodReflection.php index 14f7061aa4..7cb583f1dc 100644 --- a/src/Reflection/ExtendedMethodReflection.php +++ b/src/Reflection/ExtendedMethodReflection.php @@ -27,6 +27,11 @@ interface ExtendedMethodReflection extends MethodReflection */ public function getVariants(): array; + /** + * @internal + */ + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs; + /** * @return ParametersAcceptorWithPhpDocs[]|null */ diff --git a/src/Reflection/FunctionReflection.php b/src/Reflection/FunctionReflection.php index bdd5ed8d63..e09ceb27ed 100644 --- a/src/Reflection/FunctionReflection.php +++ b/src/Reflection/FunctionReflection.php @@ -18,6 +18,11 @@ public function getFileName(): ?string; */ public function getVariants(): array; + /** + * @internal + */ + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs; + /** * @return ParametersAcceptorWithPhpDocs[]|null */ diff --git a/src/Reflection/Native/NativeFunctionReflection.php b/src/Reflection/Native/NativeFunctionReflection.php index 2dd98f2951..c2a61a0131 100644 --- a/src/Reflection/Native/NativeFunctionReflection.php +++ b/src/Reflection/Native/NativeFunctionReflection.php @@ -5,8 +5,10 @@ use PHPStan\Reflection\Assertions; use PHPStan\Reflection\FunctionReflection; use PHPStan\Reflection\ParametersAcceptorWithPhpDocs; +use PHPStan\ShouldNotHappenException; use PHPStan\TrinaryLogic; use PHPStan\Type\Type; +use function count; final class NativeFunctionReflection implements FunctionReflection { @@ -46,14 +48,21 @@ public function getFileName(): ?string return null; } - /** - * @return ParametersAcceptorWithPhpDocs[] - */ public function getVariants(): array { return $this->variants; } + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs + { + $variants = $this->getVariants(); + if (count($variants) !== 1) { + throw new ShouldNotHappenException(); + } + + return $variants[0]; + } + public function getNamedArgumentsVariants(): ?array { return $this->namedArgumentsVariants; diff --git a/src/Reflection/Native/NativeMethodReflection.php b/src/Reflection/Native/NativeMethodReflection.php index 425f75edd6..d588cea558 100644 --- a/src/Reflection/Native/NativeMethodReflection.php +++ b/src/Reflection/Native/NativeMethodReflection.php @@ -10,10 +10,12 @@ use PHPStan\Reflection\ParametersAcceptorWithPhpDocs; use PHPStan\Reflection\Php\BuiltinMethodReflection; use PHPStan\Reflection\ReflectionProvider; +use PHPStan\ShouldNotHappenException; use PHPStan\TrinaryLogic; use PHPStan\Type\Type; use PHPStan\Type\TypehintHelper; use ReflectionException; +use function count; use function strtolower; final class NativeMethodReflection implements ExtendedMethodReflection @@ -109,6 +111,16 @@ public function getVariants(): array return $this->variants; } + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs + { + $variants = $this->getVariants(); + if (count($variants) !== 1) { + throw new ShouldNotHappenException(); + } + + return $variants[0]; + } + public function getNamedArgumentsVariants(): ?array { return $this->namedArgumentsVariants; diff --git a/src/Reflection/Php/ClosureCallMethodReflection.php b/src/Reflection/Php/ClosureCallMethodReflection.php index 0f47e2c746..be5d97660f 100644 --- a/src/Reflection/Php/ClosureCallMethodReflection.php +++ b/src/Reflection/Php/ClosureCallMethodReflection.php @@ -10,6 +10,7 @@ use PHPStan\Reflection\Native\NativeParameterReflection; use PHPStan\Reflection\ParameterReflection; use PHPStan\Reflection\ParameterReflectionWithPhpDocs; +use PHPStan\Reflection\ParametersAcceptorWithPhpDocs; use PHPStan\Reflection\PassedByReference; use PHPStan\TrinaryLogic; use PHPStan\Type\ClosureType; @@ -105,6 +106,11 @@ public function getVariants(): array ]; } + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs + { + return $this->getVariants()[0]; + } + public function getNamedArgumentsVariants(): ?array { return null; diff --git a/src/Reflection/Php/EnumCasesMethodReflection.php b/src/Reflection/Php/EnumCasesMethodReflection.php index ec9f1b3b9d..ff5fd5f9a5 100644 --- a/src/Reflection/Php/EnumCasesMethodReflection.php +++ b/src/Reflection/Php/EnumCasesMethodReflection.php @@ -7,6 +7,7 @@ use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ExtendedMethodReflection; use PHPStan\Reflection\FunctionVariantWithPhpDocs; +use PHPStan\Reflection\ParametersAcceptorWithPhpDocs; use PHPStan\ShouldNotHappenException; use PHPStan\TrinaryLogic; use PHPStan\Type\Generic\TemplateTypeMap; @@ -75,6 +76,11 @@ public function getVariants(): array ]; } + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs + { + return $this->getVariants()[0]; + } + public function getNamedArgumentsVariants(): ?array { return null; diff --git a/src/Reflection/Php/ExitFunctionReflection.php b/src/Reflection/Php/ExitFunctionReflection.php index e343d801b1..dc8f74ecea 100644 --- a/src/Reflection/Php/ExitFunctionReflection.php +++ b/src/Reflection/Php/ExitFunctionReflection.php @@ -69,6 +69,11 @@ public function getVariants(): array ]; } + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs + { + return $this->getVariants()[0]; + } + /** * @return ParametersAcceptorWithPhpDocs[] */ diff --git a/src/Reflection/Php/PhpFunctionFromParserNodeReflection.php b/src/Reflection/Php/PhpFunctionFromParserNodeReflection.php index 538762ba0d..96ba2f6be1 100644 --- a/src/Reflection/Php/PhpFunctionFromParserNodeReflection.php +++ b/src/Reflection/Php/PhpFunctionFromParserNodeReflection.php @@ -116,6 +116,11 @@ public function getVariants(): array return $this->variants; } + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs + { + return $this; + } + public function getNamedArgumentsVariants(): ?array { return null; diff --git a/src/Reflection/Php/PhpFunctionReflection.php b/src/Reflection/Php/PhpFunctionReflection.php index a52f3829f8..1f3ffee131 100644 --- a/src/Reflection/Php/PhpFunctionReflection.php +++ b/src/Reflection/Php/PhpFunctionReflection.php @@ -107,6 +107,11 @@ public function getVariants(): array return $this->variants; } + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs + { + return $this->getVariants()[0]; + } + public function getNamedArgumentsVariants(): ?array { return null; diff --git a/src/Reflection/Php/PhpMethodReflection.php b/src/Reflection/Php/PhpMethodReflection.php index 5a75f85a8a..390ebc886a 100644 --- a/src/Reflection/Php/PhpMethodReflection.php +++ b/src/Reflection/Php/PhpMethodReflection.php @@ -212,6 +212,11 @@ public function getVariants(): array return $this->variants; } + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs + { + return $this->getVariants()[0]; + } + public function getNamedArgumentsVariants(): ?array { return null; diff --git a/src/Reflection/Php/UniversalObjectCratesClassReflectionExtension.php b/src/Reflection/Php/UniversalObjectCratesClassReflectionExtension.php index be769e05a0..a39fe6c67b 100644 --- a/src/Reflection/Php/UniversalObjectCratesClassReflectionExtension.php +++ b/src/Reflection/Php/UniversalObjectCratesClassReflectionExtension.php @@ -4,7 +4,6 @@ use PHPStan\Reflection\Annotations\AnnotationsPropertiesClassReflectionExtension; use PHPStan\Reflection\ClassReflection; -use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Reflection\PropertiesClassReflectionExtension; use PHPStan\Reflection\PropertyReflection; use PHPStan\Reflection\ReflectionProvider; @@ -66,13 +65,13 @@ public function getProperty(ClassReflection $classReflection, string $propertyNa } if ($classReflection->hasNativeMethod('__get')) { - $readableType = ParametersAcceptorSelector::selectSingle($classReflection->getNativeMethod('__get')->getVariants())->getReturnType(); + $readableType = $classReflection->getNativeMethod('__get')->getOnlyVariant()->getReturnType(); } else { $readableType = new MixedType(); } if ($classReflection->hasNativeMethod('__set')) { - $writableType = ParametersAcceptorSelector::selectSingle($classReflection->getNativeMethod('__set')->getVariants())->getParameters()[1]->getType(); + $writableType = $classReflection->getNativeMethod('__set')->getOnlyVariant()->getParameters()[1]->getType(); } else { $writableType = new MixedType(); } diff --git a/src/Reflection/ResolvedMethodReflection.php b/src/Reflection/ResolvedMethodReflection.php index 69863e8666..477cbdea74 100644 --- a/src/Reflection/ResolvedMethodReflection.php +++ b/src/Reflection/ResolvedMethodReflection.php @@ -52,6 +52,11 @@ public function getVariants(): array return $this->variants = $this->resolveVariants($this->reflection->getVariants()); } + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs + { + return $this->getVariants()[0]; + } + public function getNamedArgumentsVariants(): ?array { $variants = $this->namedArgumentVariants; diff --git a/src/Reflection/Type/IntersectionTypeMethodReflection.php b/src/Reflection/Type/IntersectionTypeMethodReflection.php index c447875580..65c3b8b152 100644 --- a/src/Reflection/Type/IntersectionTypeMethodReflection.php +++ b/src/Reflection/Type/IntersectionTypeMethodReflection.php @@ -10,6 +10,7 @@ use PHPStan\Reflection\MethodReflection; use PHPStan\Reflection\ParametersAcceptor; use PHPStan\Reflection\ParametersAcceptorWithPhpDocs; +use PHPStan\ShouldNotHappenException; use PHPStan\TrinaryLogic; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; @@ -94,6 +95,16 @@ public function getVariants(): array ), $this->methods[0]->getVariants()); } + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs + { + $variants = $this->getVariants(); + if (count($variants) !== 1) { + throw new ShouldNotHappenException(); + } + + return $variants[0]; + } + public function getNamedArgumentsVariants(): ?array { return null; diff --git a/src/Reflection/Type/UnionTypeMethodReflection.php b/src/Reflection/Type/UnionTypeMethodReflection.php index 3b2598c368..3808304444 100644 --- a/src/Reflection/Type/UnionTypeMethodReflection.php +++ b/src/Reflection/Type/UnionTypeMethodReflection.php @@ -8,6 +8,7 @@ use PHPStan\Reflection\ExtendedMethodReflection; use PHPStan\Reflection\MethodReflection; use PHPStan\Reflection\ParametersAcceptorSelector; +use PHPStan\Reflection\ParametersAcceptorWithPhpDocs; use PHPStan\TrinaryLogic; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; @@ -82,6 +83,11 @@ public function getVariants(): array return [ParametersAcceptorSelector::combineAcceptors($variants)]; } + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs + { + return $this->getVariants()[0]; + } + public function getNamedArgumentsVariants(): ?array { return null; diff --git a/src/Reflection/WrappedExtendedMethodReflection.php b/src/Reflection/WrappedExtendedMethodReflection.php index 78f31cdce6..12d263b0e6 100644 --- a/src/Reflection/WrappedExtendedMethodReflection.php +++ b/src/Reflection/WrappedExtendedMethodReflection.php @@ -87,6 +87,11 @@ public function getVariants(): array return $variants; } + public function getOnlyVariant(): ParametersAcceptorWithPhpDocs + { + return $this->getVariants()[0]; + } + public function getNamedArgumentsVariants(): ?array { return null; diff --git a/src/Rules/Methods/MethodSignatureRule.php b/src/Rules/Methods/MethodSignatureRule.php index b0965e55bc..4cd7fd8277 100644 --- a/src/Rules/Methods/MethodSignatureRule.php +++ b/src/Rules/Methods/MethodSignatureRule.php @@ -8,7 +8,6 @@ use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ExtendedMethodReflection; use PHPStan\Reflection\ParameterReflectionWithPhpDocs; -use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Reflection\ParametersAcceptorWithPhpDocs; use PHPStan\Reflection\Php\NativeBuiltinMethodReflection; use PHPStan\Reflection\Php\PhpClassReflectionExtension; @@ -74,8 +73,8 @@ public function processNode(Node $node, Scope $scope): array if (count($parentVariants) !== 1) { continue; } - $parentParameters = ParametersAcceptorSelector::selectSingle($parentVariants); - [$returnTypeCompatibility, $returnType, $parentReturnType] = $this->checkReturnTypeCompatibility($declaringClass, $method, $parentParameters); + $parentVariant = $parentVariants[0]; + [$returnTypeCompatibility, $returnType, $parentReturnType] = $this->checkReturnTypeCompatibility($declaringClass, $method, $parentVariant); if ($returnTypeCompatibility->no() || (!$returnTypeCompatibility->yes() && $this->reportMaybes)) { $builder = RuleErrorBuilder::message(sprintf( 'Return type (%s) of method %s::%s() should be %s with return type (%s) of method %s::%s()', @@ -114,7 +113,7 @@ public function processNode(Node $node, Scope $scope): array $errors[] = $builder->build(); } - $parameterResults = $this->checkParameterTypeCompatibility($declaringClass, $method->getParameters(), $parentParameters->getParameters()); + $parameterResults = $this->checkParameterTypeCompatibility($declaringClass, $method->getParameters(), $parentVariant->getParameters()); foreach ($parameterResults as $parameterIndex => [$parameterResult, $parameterType, $parentParameterType]) { if ($parameterResult->yes()) { continue; @@ -123,7 +122,7 @@ public function processNode(Node $node, Scope $scope): array continue; } $parameter = $method->getParameters()[$parameterIndex]; - $parentParameter = $parentParameters->getParameters()[$parameterIndex]; + $parentParameter = $parentVariant->getParameters()[$parameterIndex]; $errors[] = RuleErrorBuilder::message(sprintf( 'Parameter #%d $%s (%s) of method %s::%s() should be %s with parameter $%s (%s) of method %s::%s()', $parameterIndex + 1, diff --git a/src/Type/ObjectType.php b/src/Type/ObjectType.php index b756e3f77d..31a9613e91 100644 --- a/src/Type/ObjectType.php +++ b/src/Type/ObjectType.php @@ -24,7 +24,6 @@ use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ConstantReflection; use PHPStan\Reflection\ExtendedMethodReflection; -use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Reflection\Php\UniversalObjectCratesClassReflectionExtension; use PHPStan\Reflection\PropertyReflection; use PHPStan\Reflection\ReflectionProviderStaticAccessor; @@ -601,7 +600,7 @@ public function toString(): Type } if ($classReflection->hasNativeMethod('__toString')) { - return ParametersAcceptorSelector::selectSingle($this->getMethod('__toString', new OutOfClassScope())->getVariants())->getReturnType(); + return $this->getMethod('__toString', new OutOfClassScope())->getOnlyVariant()->getReturnType(); } return new ErrorType(); @@ -872,9 +871,7 @@ public function getIterableKeyType(): Type { $isTraversable = false; if ($this->isInstanceOf(IteratorAggregate::class)->yes()) { - $keyType = RecursionGuard::run($this, fn (): Type => ParametersAcceptorSelector::selectSingle( - $this->getMethod('getIterator', new OutOfClassScope())->getVariants(), - )->getReturnType()->getIterableKeyType()); + $keyType = RecursionGuard::run($this, fn (): Type => $this->getMethod('getIterator', new OutOfClassScope())->getOnlyVariant()->getReturnType()->getIterableKeyType()); $isTraversable = true; if (!$keyType instanceof MixedType || $keyType->isExplicitMixed()) { return $keyType; @@ -893,9 +890,7 @@ public function getIterableKeyType(): Type } if ($this->isInstanceOf(Iterator::class)->yes()) { - return RecursionGuard::run($this, fn (): Type => ParametersAcceptorSelector::selectSingle( - $this->getMethod('key', new OutOfClassScope())->getVariants(), - )->getReturnType()); + return RecursionGuard::run($this, fn (): Type => $this->getMethod('key', new OutOfClassScope())->getOnlyVariant()->getReturnType()); } if ($extraOffsetAccessible) { @@ -923,9 +918,7 @@ public function getIterableValueType(): Type { $isTraversable = false; if ($this->isInstanceOf(IteratorAggregate::class)->yes()) { - $valueType = RecursionGuard::run($this, fn (): Type => ParametersAcceptorSelector::selectSingle( - $this->getMethod('getIterator', new OutOfClassScope())->getVariants(), - )->getReturnType()->getIterableValueType()); + $valueType = RecursionGuard::run($this, fn (): Type => $this->getMethod('getIterator', new OutOfClassScope())->getOnlyVariant()->getReturnType()->getIterableValueType()); $isTraversable = true; if (!$valueType instanceof MixedType || $valueType->isExplicitMixed()) { return $valueType; @@ -944,9 +937,7 @@ public function getIterableValueType(): Type } if ($this->isInstanceOf(Iterator::class)->yes()) { - return RecursionGuard::run($this, fn (): Type => ParametersAcceptorSelector::selectSingle( - $this->getMethod('current', new OutOfClassScope())->getVariants(), - )->getReturnType()); + return RecursionGuard::run($this, fn (): Type => $this->getMethod('current', new OutOfClassScope())->getOnlyVariant()->getReturnType()); } if ($extraOffsetAccessible) { @@ -1129,7 +1120,7 @@ public function hasOffsetValueType(Type $offsetType): TrinaryLogic { if ($this->isInstanceOf(ArrayAccess::class)->yes()) { $acceptedOffsetType = RecursionGuard::run($this, function (): Type { - $parameters = ParametersAcceptorSelector::selectSingle($this->getMethod('offsetSet', new OutOfClassScope())->getVariants())->getParameters(); + $parameters = $this->getMethod('offsetSet', new OutOfClassScope())->getOnlyVariant()->getParameters(); if (count($parameters) < 2) { throw new ShouldNotHappenException(sprintf( 'Method %s::%s() has less than 2 parameters.', @@ -1161,7 +1152,7 @@ public function getOffsetValueType(Type $offsetType): Type } if ($this->isInstanceOf(ArrayAccess::class)->yes()) { - return RecursionGuard::run($this, fn (): Type => ParametersAcceptorSelector::selectSingle($this->getMethod('offsetGet', new OutOfClassScope())->getVariants())->getReturnType()); + return RecursionGuard::run($this, fn (): Type => $this->getMethod('offsetGet', new OutOfClassScope())->getOnlyVariant()->getReturnType()); } return new ErrorType(); @@ -1176,7 +1167,7 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni if ($this->isInstanceOf(ArrayAccess::class)->yes()) { $acceptedValueType = new NeverType(); $acceptedOffsetType = RecursionGuard::run($this, function () use (&$acceptedValueType): Type { - $parameters = ParametersAcceptorSelector::selectSingle($this->getMethod('offsetSet', new OutOfClassScope())->getVariants())->getParameters(); + $parameters = $this->getMethod('offsetSet', new OutOfClassScope())->getOnlyVariant()->getParameters(); if (count($parameters) < 2) { throw new ShouldNotHappenException(sprintf( 'Method %s::%s() has less than 2 parameters.', diff --git a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php index c98129fc99..b237cdd33a 100644 --- a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php +++ b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php @@ -7,7 +7,6 @@ use ExtendingKnownClassWithCheck\Foo; use PHPStan\Reflection\InitializerExprContext; use PHPStan\Reflection\InitializerExprTypeResolver; -use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Reflection\SignatureMap\SignatureMapProvider; use PHPStan\Testing\PHPStanTestCase; use PHPStan\Type\Constant\ConstantIntegerType; @@ -369,7 +368,7 @@ public function testBug4713(): void $reflectionProvider = $this->createReflectionProvider(); $class = $reflectionProvider->getClass(Service::class); - $parameter = ParametersAcceptorSelector::selectSingle($class->getNativeMethod('createInstance')->getVariants())->getParameters()[0]; + $parameter = $class->getNativeMethod('createInstance')->getOnlyVariant()->getParameters()[0]; $defaultValue = $parameter->getDefaultValue(); $this->assertInstanceOf(ConstantStringType::class, $defaultValue); $this->assertSame(Service::class, $defaultValue->getValue()); @@ -382,7 +381,7 @@ public function testBug4288(): void $reflectionProvider = $this->createReflectionProvider(); $class = $reflectionProvider->getClass(MyClass::class); - $parameter = ParametersAcceptorSelector::selectSingle($class->getNativeMethod('paginate')->getVariants())->getParameters()[0]; + $parameter = $class->getNativeMethod('paginate')->getOnlyVariant()->getParameters()[0]; $defaultValue = $parameter->getDefaultValue(); $this->assertInstanceOf(ConstantIntegerType::class, $defaultValue); $this->assertSame(10, $defaultValue->getValue()); diff --git a/tests/PHPStan/Analyser/ArgumentsNormalizerLegacyTest.php b/tests/PHPStan/Analyser/ArgumentsNormalizerLegacyTest.php index 8d4f675a66..b7f4e23bf0 100644 --- a/tests/PHPStan/Analyser/ArgumentsNormalizerLegacyTest.php +++ b/tests/PHPStan/Analyser/ArgumentsNormalizerLegacyTest.php @@ -9,7 +9,6 @@ use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use PHPStan\Node\Expr\TypeExpr; -use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Reflection\SignatureMap\NativeFunctionReflectionProvider; use PHPStan\ShouldNotHappenException; use PHPStan\Testing\PHPStanTestCase; @@ -34,7 +33,7 @@ public function testArgumentReorderAllNamed(): void if ($functionReflection === null) { throw new ShouldNotHappenException(); } - $parameterAcceptor = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants()); + $parameterAcceptor = $functionReflection->getOnlyVariant(); $args = [ new Arg( @@ -84,7 +83,7 @@ public function testArgumentReorderAllNamedWithSkipped(): void if ($functionReflection === null) { throw new ShouldNotHappenException(); } - $parameterAcceptor = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants()); + $parameterAcceptor = $functionReflection->getOnlyVariant(); $args = [ new Arg( @@ -137,7 +136,7 @@ public function testMissingRequiredParameter(): void if ($functionReflection === null) { throw new ShouldNotHappenException(); } - $parameterAcceptor = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants()); + $parameterAcceptor = $functionReflection->getOnlyVariant(); $args = [ new Arg( @@ -161,7 +160,7 @@ public function testLeaveRegularCallAsIs(): void if ($functionReflection === null) { throw new ShouldNotHappenException(); } - $parameterAcceptor = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants()); + $parameterAcceptor = $functionReflection->getOnlyVariant(); $args = [ new Arg( diff --git a/tests/PHPStan/Reflection/Annotations/AnnotationsMethodsClassReflectionExtensionTest.php b/tests/PHPStan/Reflection/Annotations/AnnotationsMethodsClassReflectionExtensionTest.php index dfdb480fb8..36b4402d9e 100644 --- a/tests/PHPStan/Reflection/Annotations/AnnotationsMethodsClassReflectionExtensionTest.php +++ b/tests/PHPStan/Reflection/Annotations/AnnotationsMethodsClassReflectionExtensionTest.php @@ -8,7 +8,6 @@ use AnnotationsMethods\Foo; use AnnotationsMethods\FooInterface; use PHPStan\Analyser\Scope; -use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Reflection\PassedByReference; use PHPStan\Reflection\Php\PhpMethodReflection; use PHPStan\Testing\PHPStanTestCase; @@ -974,7 +973,7 @@ public function testMethods(string $className, array $methods): void $this->assertTrue($class->hasMethod($methodName), sprintf('Method %s() not found in class %s.', $methodName, $className)); $method = $class->getMethod($methodName, $scope); - $selectedParametersAcceptor = ParametersAcceptorSelector::selectSingle($method->getVariants()); + $selectedParametersAcceptor = $method->getOnlyVariant(); $this->assertSame( $expectedMethodData['class'], $method->getDeclaringClass()->getName(), diff --git a/tests/PHPStan/Reflection/ClassReflectionTest.php b/tests/PHPStan/Reflection/ClassReflectionTest.php index fa00afc63f..d798e65b66 100644 --- a/tests/PHPStan/Reflection/ClassReflectionTest.php +++ b/tests/PHPStan/Reflection/ClassReflectionTest.php @@ -115,7 +115,7 @@ public function testVariadicTraitMethod(): void $reflectionProvider = $this->createReflectionProvider(); $fooReflection = $reflectionProvider->getClass(Foo::class); $variadicMethod = $fooReflection->getNativeMethod('variadicMethod'); - $methodVariant = ParametersAcceptorSelector::selectSingle($variadicMethod->getVariants()); + $methodVariant = $variadicMethod->getOnlyVariant(); $this->assertTrue($methodVariant->isVariadic()); } diff --git a/tests/PHPStan/Reflection/MixedTypeTest.php b/tests/PHPStan/Reflection/MixedTypeTest.php index f6c511df33..5c5248d38c 100644 --- a/tests/PHPStan/Reflection/MixedTypeTest.php +++ b/tests/PHPStan/Reflection/MixedTypeTest.php @@ -19,7 +19,7 @@ public function testMixedType(): void $this->assertTrue($propertyType->isExplicitMixed()); $method = $class->getNativeMethod('doFoo'); - $methodVariant = ParametersAcceptorSelector::selectSingle($method->getVariants()); + $methodVariant = $method->getOnlyVariant(); $methodReturnType = $methodVariant->getReturnType(); $this->assertInstanceOf(MixedType::class, $methodReturnType); $this->assertTrue($methodReturnType->isExplicitMixed()); @@ -29,7 +29,7 @@ public function testMixedType(): void $this->assertTrue($methodParameterType->isExplicitMixed()); $function = $reflectionProvider->getFunction(new Name('NativeMixedType\doFoo'), null); - $functionVariant = ParametersAcceptorSelector::selectSingle($function->getVariants()); + $functionVariant = $function->getOnlyVariant(); $functionReturnType = $functionVariant->getReturnType(); $this->assertInstanceOf(MixedType::class, $functionReturnType); $this->assertTrue($functionReturnType->isExplicitMixed());