|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\StrictCalls; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Node\MethodCallableNode; |
| 8 | +use PHPStan\Rules\RuleLevelHelper; |
| 9 | +use PHPStan\Type\ErrorType; |
| 10 | +use PHPStan\Type\Type; |
| 11 | + |
| 12 | +/** |
| 13 | + * @implements \PHPStan\Rules\Rule<MethodCallableNode> |
| 14 | + */ |
| 15 | +class DynamicCallOnStaticMethodsCallableRule implements \PHPStan\Rules\Rule |
| 16 | +{ |
| 17 | + |
| 18 | + /** @var \PHPStan\Rules\RuleLevelHelper */ |
| 19 | + private $ruleLevelHelper; |
| 20 | + |
| 21 | + public function __construct(RuleLevelHelper $ruleLevelHelper) |
| 22 | + { |
| 23 | + $this->ruleLevelHelper = $ruleLevelHelper; |
| 24 | + } |
| 25 | + |
| 26 | + public function getNodeType(): string |
| 27 | + { |
| 28 | + return MethodCallableNode::class; |
| 29 | + } |
| 30 | + |
| 31 | + public function processNode(Node $node, Scope $scope): array |
| 32 | + { |
| 33 | + if (!$node->getName() instanceof Node\Identifier) { |
| 34 | + return []; |
| 35 | + } |
| 36 | + |
| 37 | + $name = $node->getName()->name; |
| 38 | + $type = $this->ruleLevelHelper->findTypeToCheck( |
| 39 | + $scope, |
| 40 | + $node->getVar(), |
| 41 | + '', |
| 42 | + function (Type $type) use ($name): bool { |
| 43 | + return $type->canCallMethods()->yes() && $type->hasMethod($name)->yes(); |
| 44 | + } |
| 45 | + )->getType(); |
| 46 | + |
| 47 | + if ($type instanceof ErrorType || !$type->canCallMethods()->yes() || !$type->hasMethod($name)->yes()) { |
| 48 | + return []; |
| 49 | + } |
| 50 | + |
| 51 | + $methodReflection = $type->getMethod($name, $scope); |
| 52 | + if ($methodReflection->isStatic()) { |
| 53 | + return [sprintf( |
| 54 | + 'Dynamic call to static method %s::%s().', |
| 55 | + $methodReflection->getDeclaringClass()->getDisplayName(), |
| 56 | + $methodReflection->getName() |
| 57 | + )]; |
| 58 | + } |
| 59 | + |
| 60 | + return []; |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments