|
27 | 27 | use PHPStan\TrinaryLogic; |
28 | 28 | use PHPStan\Type\ErrorType; |
29 | 29 | use PHPStan\Type\Generic\GenericClassStringType; |
| 30 | +use PHPStan\Type\ObjectType; |
30 | 31 | use PHPStan\Type\StaticType; |
31 | 32 | use PHPStan\Type\StringType; |
32 | 33 | use PHPStan\Type\Type; |
@@ -281,24 +282,28 @@ public function check( |
281 | 282 | || $function->isStatic() |
282 | 283 | || $scopeIsInMethodClassOrSubClass->no() |
283 | 284 | ) { |
284 | | - // per php-src docs, this method can be called statically, even if declared non-static |
285 | | - if (strtolower($method->getName()) === 'loadhtml' && $method->getDeclaringClass()->getName() === DOMDocument::class) { |
286 | | - return [[], null]; |
287 | | - } |
| 285 | + // a closure bound with a $this of the method's class calls its instance |
| 286 | + // methods through self::/static:: as well |
| 287 | + if (!self::isCalledOnBoundThis($scope, $classType)) { |
| 288 | + // per php-src docs, this method can be called statically, even if declared non-static |
| 289 | + if (strtolower($method->getName()) === 'loadhtml' && $method->getDeclaringClass()->getName() === DOMDocument::class) { |
| 290 | + return [[], null]; |
| 291 | + } |
288 | 292 |
|
289 | | - return [ |
290 | | - array_merge($errors, [ |
291 | | - RuleErrorBuilder::message(sprintf( |
292 | | - 'Static call to instance method %s::%s().', |
293 | | - $method->getDeclaringClass()->getDisplayName(), |
294 | | - $method->getName(), |
295 | | - )) |
296 | | - ->line($astName->getStartLine()) |
297 | | - ->identifier('method.staticCall') |
298 | | - ->build(), |
299 | | - ]), |
300 | | - $method, |
301 | | - ]; |
| 293 | + return [ |
| 294 | + array_merge($errors, [ |
| 295 | + RuleErrorBuilder::message(sprintf( |
| 296 | + 'Static call to instance method %s::%s().', |
| 297 | + $method->getDeclaringClass()->getDisplayName(), |
| 298 | + $method->getName(), |
| 299 | + )) |
| 300 | + ->line($astName->getStartLine()) |
| 301 | + ->identifier('method.staticCall') |
| 302 | + ->build(), |
| 303 | + ]), |
| 304 | + $method, |
| 305 | + ]; |
| 306 | + } |
302 | 307 | } |
303 | 308 | } |
304 | 309 |
|
@@ -359,4 +364,26 @@ public function check( |
359 | 364 | return [$errors, $method]; |
360 | 365 | } |
361 | 366 |
|
| 367 | + /** |
| 368 | + * Whether the scope is a closure bound with a `$this` that is certainly an instance of |
| 369 | + * the classes of the static call. An undefined `$this` reads as an error type and a |
| 370 | + * possibly defined one as mixed, neither of which is certainly such an instance. |
| 371 | + */ |
| 372 | + private static function isCalledOnBoundThis(Scope $scope, Type $classType): bool |
| 373 | + { |
| 374 | + $objectClassNames = $classType->getObjectClassNames(); |
| 375 | + if ($objectClassNames === [] || !$scope->isInClosureBind()) { |
| 376 | + return false; |
| 377 | + } |
| 378 | + |
| 379 | + $boundThisType = $scope->getType(new Expr\Variable('this')); |
| 380 | + foreach ($objectClassNames as $objectClassName) { |
| 381 | + if (!(new ObjectType($objectClassName))->isSuperTypeOf($boundThisType)->yes()) { |
| 382 | + return false; |
| 383 | + } |
| 384 | + } |
| 385 | + |
| 386 | + return true; |
| 387 | + } |
| 388 | + |
362 | 389 | } |
0 commit comments