forked from phpstan/phpstan-deprecation-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallToDeprecatedFunctionRule.php
More file actions
80 lines (65 loc) · 1.95 KB
/
CallToDeprecatedFunctionRule.php
File metadata and controls
80 lines (65 loc) · 1.95 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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Deprecations;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\FunctionNotFoundException;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;
/**
* @implements Rule<FuncCall>
*/
class CallToDeprecatedFunctionRule implements Rule
{
private ReflectionProvider $reflectionProvider;
private DeprecatedScopeHelper $deprecatedScopeHelper;
private DeprecationHelper $deprecationHelper;
public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper, DeprecationHelper $deprecationHelper)
{
$this->reflectionProvider = $reflectionProvider;
$this->deprecatedScopeHelper = $deprecatedScopeHelper;
$this->deprecationHelper = $deprecationHelper;
}
public function getNodeType(): string
{
return FuncCall::class;
}
public function processNode(Node $node, Scope $scope): array
{
if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) {
return [];
}
if (!($node->name instanceof Name)) {
return [];
}
try {
$function = $this->reflectionProvider->getFunction($node->name, $scope);
} catch (FunctionNotFoundException $e) {
// Other rules will notify if the function is not found
return [];
}
$deprecation = $this->deprecationHelper->getDeprecation($function);
if ($deprecation !== null) {
$description = $deprecation->getDescription();
if ($description === null) {
return [
RuleErrorBuilder::message(sprintf(
'Call to deprecated function %s().',
$function->getName(),
))->identifier('function.deprecated')->build(),
];
}
return [
RuleErrorBuilder::message(sprintf(
"Call to deprecated function %s():\n%s",
$function->getName(),
$description,
))->identifier('function.deprecated')->build(),
];
}
return [];
}
}