From 5a256b668121368e18dce9cc2b2e1af16031799f Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 26 Sep 2024 20:08:21 +0200 Subject: [PATCH] Fixes after PHPStan update --- .../Nette/NetteObjectClassReflectionExtension.php | 3 +-- ...mponentGetPresenterDynamicReturnTypeExtension.php | 4 +++- .../ComponentLookupDynamicReturnTypeExtension.php | 4 +++- ...entModelArrayAccessDynamicReturnTypeExtension.php | 12 +++++++++--- .../ComponentModelDynamicReturnTypeExtension.php | 10 ++++++++-- ...ntainerUnsafeValuesDynamicReturnTypeExtension.php | 7 +++---- .../FormsBaseControlDynamicReturnTypeExtension.php | 6 +++++- .../Nette/HtmlClassReflectionExtensionTest.php | 3 +-- 8 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/Reflection/Nette/NetteObjectClassReflectionExtension.php b/src/Reflection/Nette/NetteObjectClassReflectionExtension.php index 2e6465a..516af62 100644 --- a/src/Reflection/Nette/NetteObjectClassReflectionExtension.php +++ b/src/Reflection/Nette/NetteObjectClassReflectionExtension.php @@ -7,7 +7,6 @@ use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\MethodReflection; use PHPStan\Reflection\MethodsClassReflectionExtension; -use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Reflection\PropertiesClassReflectionExtension; use PHPStan\Reflection\PropertyReflection; use function array_merge; @@ -53,7 +52,7 @@ public function getProperty(ClassReflection $classReflection, string $propertyNa { /** @var MethodReflection $getterMethod */ $getterMethod = $this->getMethodByProperty($classReflection, $propertyName); - return new NetteObjectPropertyReflection($classReflection, ParametersAcceptorSelector::selectSingle($getterMethod->getVariants())->getReturnType()); + return new NetteObjectPropertyReflection($classReflection, $getterMethod->getVariants()[0]->getReturnType()); } public function hasMethod(ClassReflection $classReflection, string $methodName): bool diff --git a/src/Type/Nette/ComponentGetPresenterDynamicReturnTypeExtension.php b/src/Type/Nette/ComponentGetPresenterDynamicReturnTypeExtension.php index baaeaec..09860e5 100644 --- a/src/Type/Nette/ComponentGetPresenterDynamicReturnTypeExtension.php +++ b/src/Type/Nette/ComponentGetPresenterDynamicReturnTypeExtension.php @@ -26,7 +26,9 @@ public function isMethodSupported(MethodReflection $methodReflection): bool public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type { - $methodDefinition = ParametersAcceptorSelector::selectSingle( + $methodDefinition = ParametersAcceptorSelector::selectFromArgs( + $scope, + $methodCall->getArgs(), $methodReflection->getVariants(), ); $defaultReturnType = $methodDefinition->getReturnType(); diff --git a/src/Type/Nette/ComponentLookupDynamicReturnTypeExtension.php b/src/Type/Nette/ComponentLookupDynamicReturnTypeExtension.php index 46d5771..934670f 100644 --- a/src/Type/Nette/ComponentLookupDynamicReturnTypeExtension.php +++ b/src/Type/Nette/ComponentLookupDynamicReturnTypeExtension.php @@ -26,7 +26,9 @@ public function isMethodSupported(MethodReflection $methodReflection): bool public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type { - $defaultReturnType = ParametersAcceptorSelector::selectSingle( + $defaultReturnType = ParametersAcceptorSelector::selectFromArgs( + $scope, + $methodCall->getArgs(), $methodReflection->getVariants(), )->getReturnType(); if (count($methodCall->getArgs()) < 2) { diff --git a/src/Type/Nette/ComponentModelArrayAccessDynamicReturnTypeExtension.php b/src/Type/Nette/ComponentModelArrayAccessDynamicReturnTypeExtension.php index c18eb47..e4adb49 100644 --- a/src/Type/Nette/ComponentModelArrayAccessDynamicReturnTypeExtension.php +++ b/src/Type/Nette/ComponentModelArrayAccessDynamicReturnTypeExtension.php @@ -2,7 +2,9 @@ namespace PHPStan\Type\Nette; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Scalar\String_; use PHPStan\Analyser\Scope; use PHPStan\Reflection\MethodReflection; use PHPStan\Reflection\ParametersAcceptorSelector; @@ -38,13 +40,13 @@ public function getTypeFromMethodCall( ): Type { $calledOnType = $scope->getType($methodCall->var); - $defaultType = ParametersAcceptorSelector::selectSingle($calledOnType->getMethod('createComponent', $scope)->getVariants())->getReturnType(); + $defaultType = $calledOnType->getMethod('createComponent', $scope)->getVariants()[0]->getReturnType(); $defaultType = TypeCombinator::remove($defaultType, new NullType()); if ($defaultType->isSuperTypeOf(new ObjectType('Nette\ComponentModel\IComponent'))->yes()) { $defaultType = new MixedType(false, new NullType()); } $args = $methodCall->getArgs(); - if (count($args) !== 1) { + if (count($args) < 1) { return $defaultType; } @@ -64,7 +66,11 @@ public function getTypeFromMethodCall( $method = $calledOnType->getMethod($methodName, $scope); - $types[] = ParametersAcceptorSelector::selectSingle($method->getVariants())->getReturnType(); + $types[] = ParametersAcceptorSelector::selectFromArgs( + $scope, + [new Arg(new String_($componentName))], + $method->getVariants(), + )->getReturnType(); } return TypeCombinator::union(...$types); diff --git a/src/Type/Nette/ComponentModelDynamicReturnTypeExtension.php b/src/Type/Nette/ComponentModelDynamicReturnTypeExtension.php index a8170d0..526e1dd 100644 --- a/src/Type/Nette/ComponentModelDynamicReturnTypeExtension.php +++ b/src/Type/Nette/ComponentModelDynamicReturnTypeExtension.php @@ -2,7 +2,9 @@ namespace PHPStan\Type\Nette; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Scalar\String_; use PHPStan\Analyser\Scope; use PHPStan\Reflection\MethodReflection; use PHPStan\Reflection\ParametersAcceptorSelector; @@ -38,7 +40,7 @@ public function getTypeFromMethodCall( ): Type { $calledOnType = $scope->getType($methodCall->var); - $defaultType = ParametersAcceptorSelector::selectSingle($calledOnType->getMethod('createComponent', $scope)->getVariants())->getReturnType(); + $defaultType = $calledOnType->getMethod('createComponent', $scope)->getVariants()[0]->getReturnType(); if ($defaultType->isSuperTypeOf(new ObjectType('Nette\ComponentModel\IComponent'))->yes()) { $defaultType = new MixedType(); } @@ -75,7 +77,11 @@ public function getTypeFromMethodCall( $method = $calledOnType->getMethod($methodName, $scope); - $types[] = ParametersAcceptorSelector::selectSingle($method->getVariants())->getReturnType(); + $types[] = ParametersAcceptorSelector::selectFromArgs( + $scope, + [new Arg(new String_($componentName))], + $method->getVariants(), + )->getReturnType(); } return TypeCombinator::union(...$types); diff --git a/src/Type/Nette/FormContainerUnsafeValuesDynamicReturnTypeExtension.php b/src/Type/Nette/FormContainerUnsafeValuesDynamicReturnTypeExtension.php index 6ddb557..d4d3f2b 100644 --- a/src/Type/Nette/FormContainerUnsafeValuesDynamicReturnTypeExtension.php +++ b/src/Type/Nette/FormContainerUnsafeValuesDynamicReturnTypeExtension.php @@ -5,7 +5,6 @@ use PhpParser\Node\Expr\MethodCall; use PHPStan\Analyser\Scope; use PHPStan\Reflection\MethodReflection; -use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Type\ArrayType; use PHPStan\Type\DynamicMethodReturnTypeExtension; use PHPStan\Type\MixedType; @@ -27,10 +26,10 @@ public function isMethodSupported(MethodReflection $methodReflection): bool return $methodReflection->getName() === 'getUnsafeValues'; } - public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type { if (count($methodCall->getArgs()) === 0) { - return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); + return null; } $arg = $methodCall->getArgs()[0]->value; @@ -43,7 +42,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method return new ArrayType(new StringType(), new MixedType()); } - return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); + return null; } } diff --git a/src/Type/Nette/FormsBaseControlDynamicReturnTypeExtension.php b/src/Type/Nette/FormsBaseControlDynamicReturnTypeExtension.php index cc6458d..2d6c929 100644 --- a/src/Type/Nette/FormsBaseControlDynamicReturnTypeExtension.php +++ b/src/Type/Nette/FormsBaseControlDynamicReturnTypeExtension.php @@ -31,7 +31,11 @@ public function getTypeFromMethodCall( Scope $scope ): Type { - $returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); + $returnType = ParametersAcceptorSelector::selectFromArgs( + $scope, + $methodCall->getArgs(), + $methodReflection->getVariants(), + )->getReturnType(); $referencedClasses = $returnType->getReferencedClasses(); if ( count($referencedClasses) === 1 diff --git a/tests/Reflection/Nette/HtmlClassReflectionExtensionTest.php b/tests/Reflection/Nette/HtmlClassReflectionExtensionTest.php index af0bd96..0c78873 100644 --- a/tests/Reflection/Nette/HtmlClassReflectionExtensionTest.php +++ b/tests/Reflection/Nette/HtmlClassReflectionExtensionTest.php @@ -4,7 +4,6 @@ use Nette\Utils\Html; use PHPStan\Broker\Broker; -use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Testing\PHPStanTestCase; use PHPStan\Type\VerbosityLevel; use stdClass; @@ -52,7 +51,7 @@ public function testGetMethod(): void { $classReflection = $this->broker->getClass(Html::class); $methodReflection = $this->extension->getMethod($classReflection, 'href'); - $parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants()); + $parametersAcceptor = $methodReflection->getVariants()[0]; self::assertSame('href', $methodReflection->getName()); self::assertSame($classReflection, $methodReflection->getDeclaringClass()); self::assertFalse($methodReflection->isStatic());