1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Rules\RestrictedUsage;
4:
5: use PhpParser\Node;
6: use PhpParser\Node\Identifier;
7: use PhpParser\Node\Name;
8: use PHPStan\Analyser\Scope;
9: use PHPStan\DependencyInjection\AutowiredExtensions;
10: use PHPStan\DependencyInjection\AutowiredService;
11: use PHPStan\DependencyInjection\ExtensionsCollection;
12: use PHPStan\Node\StaticMethodCallableNode;
13: use PHPStan\Reflection\ReflectionProvider;
14: use PHPStan\Rules\Rule;
15: use PHPStan\Rules\RuleErrorBuilder;
16: use PHPStan\Rules\RuleLevelHelper;
17: use PHPStan\Type\ErrorType;
18: use PHPStan\Type\Type;
19: use PHPStan\Type\TypeCombinator;
20:
21: /**
22: * @implements Rule<StaticMethodCallableNode>
23: */
24: #[AutowiredService]
25: final class RestrictedStaticMethodCallableUsageRule implements Rule
26: {
27:
28: /**
29: * @param ExtensionsCollection<RestrictedMethodUsageExtension> $extensions
30: */
31: public function __construct(
32: #[AutowiredExtensions(of: RestrictedMethodUsageExtension::class)]
33: private ExtensionsCollection $extensions,
34: private ReflectionProvider $reflectionProvider,
35: private RuleLevelHelper $ruleLevelHelper,
36: )
37: {
38: }
39:
40: public function getNodeType(): string
41: {
42: return StaticMethodCallableNode::class;
43: }
44:
45: /**
46: * @api
47: */
48: public function processNode(Node $node, Scope $scope): array
49: {
50: if (!$node->getName() instanceof Identifier) {
51: return [];
52: }
53:
54: $extensions = $this->extensions->getAll();
55: if ($extensions === []) {
56: return [];
57: }
58:
59: $methodName = $node->getName()->name;
60: $referencedClasses = [];
61:
62: if ($node->getClass() instanceof Name) {
63: $referencedClasses[] = $scope->resolveName($node->getClass());
64: } else {
65: $classTypeResult = $this->ruleLevelHelper->findTypeToCheck(
66: $scope,
67: $node->getClass(),
68: '', // We don't care about the error message
69: static fn (Type $type): bool => $type->canCallMethods()->yes() && $type->hasMethod($methodName)->yes(),
70: );
71:
72: if ($classTypeResult->getType() instanceof ErrorType) {
73: return [];
74: }
75:
76: $referencedClasses = TypeCombinator::removeNull($classTypeResult->getType())->getObjectTypeOrClassStringObjectType()->getObjectClassNames();
77: }
78:
79: $errors = [];
80: foreach ($referencedClasses as $referencedClass) {
81: if (!$this->reflectionProvider->hasClass($referencedClass)) {
82: continue;
83: }
84:
85: $classReflection = $this->reflectionProvider->getClass($referencedClass);
86: if (!$classReflection->hasMethod($methodName)) {
87: continue;
88: }
89:
90: $methodReflection = $classReflection->getMethod($methodName, $scope);
91: foreach ($extensions as $extension) {
92: $restrictedUsage = $extension->isRestrictedMethodUsage($methodReflection, $scope);
93: if ($restrictedUsage === null) {
94: continue;
95: }
96:
97: if ($classReflection->getName() !== $methodReflection->getDeclaringClass()->getName()) {
98: $rewrittenMethodReflection = new RewrittenDeclaringClassMethodReflection($classReflection, $methodReflection);
99: $rewrittenRestrictedUsage = $extension->isRestrictedMethodUsage($rewrittenMethodReflection, $scope);
100: if ($rewrittenRestrictedUsage === null) {
101: continue;
102: }
103: }
104:
105: $errorBuilder = RuleErrorBuilder::message($restrictedUsage->errorMessage)
106: ->identifier($restrictedUsage->identifier);
107: if ($restrictedUsage->tip !== null) {
108: $errorBuilder->tip($restrictedUsage->tip);
109: }
110:
111: $errors[] = $errorBuilder->build();
112: }
113: }
114:
115: return $errors;
116: }
117:
118: }
119: