forked from phpstan/phpstan-deprecation-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallToDeprecatedStaticMethodRule.php
More file actions
138 lines (116 loc) · 4.4 KB
/
CallToDeprecatedStaticMethodRule.php
File metadata and controls
138 lines (116 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Deprecations;
use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\ClassNotFoundException;
use PHPStan\Reflection\MissingMethodFromReflectionException;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use function sprintf;
use function strtolower;
/**
* @implements Rule<StaticCall>
*/
class CallToDeprecatedStaticMethodRule implements Rule
{
private ReflectionProvider $reflectionProvider;
private RuleLevelHelper $ruleLevelHelper;
private DeprecatedScopeHelper $deprecatedScopeHelper;
private DeprecationHelper $deprecationHelper;
public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper, DeprecatedScopeHelper $deprecatedScopeHelper, DeprecationHelper $deprecationHelper)
{
$this->reflectionProvider = $reflectionProvider;
$this->ruleLevelHelper = $ruleLevelHelper;
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
$this->deprecationHelper = $deprecationHelper;
}
public function getNodeType(): string
{
return StaticCall::class;
}
public function processNode(Node $node, Scope $scope): array
{
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
return [];
}
if (!$node->name instanceof Identifier) {
return [];
}
$methodName = $node->name->name;
$referencedClasses = [];
if ($node->class instanceof Name) {
$referencedClasses[] = $scope->resolveName($node->class);
} else {
$classTypeResult = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$node->class,
'', // We don't care about the error message
static fn (Type $type): bool => $type->canCallMethods()->yes() && $type->hasMethod($methodName)->yes(),
);
if ($classTypeResult->getType() instanceof ErrorType) {
return [];
}
$referencedClasses = $classTypeResult->getReferencedClasses();
}
$errors = [];
foreach ($referencedClasses as $referencedClass) {
try {
$class = $this->reflectionProvider->getClass($referencedClass);
$methodReflection = $class->getMethod($methodName, $scope);
} catch (ClassNotFoundException $e) {
continue;
} catch (MissingMethodFromReflectionException $e) {
continue;
}
$classDeprecation = $this->deprecationHelper->getDeprecation($class);
if ($classDeprecation !== null) {
$classDescription = $classDeprecation->getDescription();
if ($classDescription === null) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Call to method %s() of deprecated %s %s.',
$methodReflection->getName(),
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
$methodReflection->getDeclaringClass()->getName(),
))->identifier(sprintf('staticMethod.deprecated%s', $methodReflection->getDeclaringClass()->getClassTypeDescription()))->build();
} else {
$errors[] = RuleErrorBuilder::message(sprintf(
"Call to method %s() of deprecated %s %s:\n%s",
$methodReflection->getName(),
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
$methodReflection->getDeclaringClass()->getName(),
$classDescription,
))->identifier(sprintf('staticMethod.deprecated%s', $methodReflection->getDeclaringClass()->getClassTypeDescription()))->build();
}
}
$methodDeprecation = $this->deprecationHelper->getDeprecation($methodReflection);
if ($methodDeprecation === null) {
continue;
}
$description = $methodDeprecation->getDescription();
if ($description === null) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Call to deprecated method %s() of %s %s.',
$methodReflection->getName(),
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
$methodReflection->getDeclaringClass()->getName(),
))->identifier('staticMethod.deprecated')->build();
} else {
$errors[] = RuleErrorBuilder::message(sprintf(
"Call to deprecated method %s() of %s %s:\n%s",
$methodReflection->getName(),
strtolower($methodReflection->getDeclaringClass()->getClassTypeDescription()),
$methodReflection->getDeclaringClass()->getName(),
$description,
))->identifier('staticMethod.deprecated')->build();
}
}
return $errors;
}
}