Skip to content

Commit

Permalink
Get rid of instanceof TypeWithClassName in StaticMethodCallCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm authored and ondrejmirtes committed Jan 16, 2023
1 parent 6b61e95 commit b724d3a
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/Rules/Methods/StaticMethodCallCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
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;
use PHPStan\Type\StringType;
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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b724d3a

Please sign in to comment.