1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Rules\RestrictedUsage;
4:
5: use PhpParser\Node;
6: use PhpParser\Node\Identifier;
7: use PHPStan\Analyser\Scope;
8: use PHPStan\DependencyInjection\AutowiredService;
9: use PHPStan\DependencyInjection\Container;
10: use PHPStan\Node\MethodCallableNode;
11: use PHPStan\Reflection\ReflectionProvider;
12: use PHPStan\Rules\Rule;
13: use PHPStan\Rules\RuleErrorBuilder;
14:
15: /**
16: * @implements Rule<MethodCallableNode>
17: */
18: #[AutowiredService]
19: final class RestrictedMethodCallableUsageRule implements Rule
20: {
21:
22: /** @var RestrictedMethodUsageExtension[] $extensions */
23: private ?array $extensions = null;
24:
25: public function __construct(
26: private Container $container,
27: private ReflectionProvider $reflectionProvider,
28: )
29: {
30: }
31:
32: public function getNodeType(): string
33: {
34: return MethodCallableNode::class;
35: }
36:
37: /**
38: * @api
39: */
40: public function processNode(Node $node, Scope $scope): array
41: {
42: if (!$node->getName() instanceof Identifier) {
43: return [];
44: }
45:
46: /** @var RestrictedMethodUsageExtension[] $extensions */
47: $extensions = $this->extensions ??= $this->container->getServicesByTag(RestrictedMethodUsageExtension::METHOD_EXTENSION_TAG);
48: if ($extensions === []) {
49: return [];
50: }
51:
52: $methodName = $node->getName()->name;
53: $methodCalledOnType = $scope->getType($node->getVar());
54: $referencedClasses = $methodCalledOnType->getObjectClassNames();
55:
56: $errors = [];
57:
58: foreach ($referencedClasses as $referencedClass) {
59: if (!$this->reflectionProvider->hasClass($referencedClass)) {
60: continue;
61: }
62:
63: $classReflection = $this->reflectionProvider->getClass($referencedClass);
64: if (!$classReflection->hasMethod($methodName)) {
65: continue;
66: }
67:
68: $methodReflection = $classReflection->getMethod($methodName, $scope);
69: foreach ($extensions as $extension) {
70: $restrictedUsage = $extension->isRestrictedMethodUsage($methodReflection, $scope);
71: if ($restrictedUsage === null) {
72: continue;
73: }
74:
75: $errors[] = RuleErrorBuilder::message($restrictedUsage->errorMessage)
76: ->identifier($restrictedUsage->identifier)
77: ->build();
78: }
79: }
80:
81: return $errors;
82: }
83:
84: }
85: