Skip to content

Commit

Permalink
Fixes after PHPStan update
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 26, 2024
1 parent c903386 commit 5a256b6
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 16 deletions.
3 changes: 1 addition & 2 deletions src/Reflection/Nette/NetteObjectClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion src/Type/Nette/ComponentLookupDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions src/Type/Nette/ComponentModelDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions tests/Reflection/Nette/HtmlClassReflectionExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 5a256b6

Please sign in to comment.