From b724d3aeb5f10381c3fbeca9311ee71121deeb5c Mon Sep 17 00:00:00 2001 From: Martin Herndl Date: Mon, 9 Jan 2023 13:45:24 +0100 Subject: [PATCH] Get rid of `instanceof TypeWithClassName` in `StaticMethodCallCheck` --- src/Rules/Methods/StaticMethodCallCheck.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Rules/Methods/StaticMethodCallCheck.php b/src/Rules/Methods/StaticMethodCallCheck.php index 3e2e466a33..6ee367f31f 100644 --- a/src/Rules/Methods/StaticMethodCallCheck.php +++ b/src/Rules/Methods/StaticMethodCallCheck.php @@ -18,6 +18,7 @@ use PHPStan\Rules\RuleErrorBuilder; use PHPStan\Rules\RuleLevelHelper; use PHPStan\ShouldNotHappenException; +use PHPStan\TrinaryLogic; use PHPStan\Type\ErrorType; use PHPStan\Type\Generic\GenericClassStringType; use PHPStan\Type\ObjectWithoutClassType; @@ -25,7 +26,6 @@ use PHPStan\Type\ThisType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; -use PHPStan\Type\TypeWithClassName; use PHPStan\Type\VerbosityLevel; use function array_merge; use function in_array; @@ -213,15 +213,18 @@ public function check( $method = $classType->getMethod($methodName, $scope); if (!$method->isStatic()) { $function = $scope->getFunction(); + + $scopeIsInMethodClassOrSubClass = TrinaryLogic::createFromBoolean($scope->isInClass())->lazyAnd( + $classType->getObjectClassNames(), + static fn (string $objectClassName) => TrinaryLogic::createFromBoolean( + $scope->isInClass() + && ($scope->getClassReflection()->getName() === $objectClassName || $scope->getClassReflection()->isSubclassOf($objectClassName)), + ), + ); if ( !$function instanceof MethodReflection || $function->isStatic() - || !$scope->isInClass() - || ( - $classType instanceof TypeWithClassName - && $scope->getClassReflection()->getName() !== $classType->getClassName() - && !$scope->getClassReflection()->isSubclassOf($classType->getClassName()) - ) + || $scopeIsInMethodClassOrSubClass->no() ) { // per php-src docs, this method can be called statically, even if declared non-static if (strtolower($method->getName()) === 'loadhtml' && $method->getDeclaringClass()->getName() === DOMDocument::class) {