5
5
6
6
use PhpParser \Node ;
7
7
use PhpParser \Node \Expr \FuncCall ;
8
- use PhpParser \Node \Expr \Variable ;
9
- use PhpParser \Node \Name ;
10
- use PhpParser \Node \Scalar \String_ ;
11
8
use PHPStan \Analyser \Scope ;
12
- use PHPStan \Reflection \ReflectionProvider ;
13
9
use PHPStan \Rules \IdentifierRuleError ;
14
10
use PHPStan \Rules \Rule ;
15
11
use PHPStan \ShouldNotHappenException ;
16
12
use Spaze \PHPStan \Rules \Disallowed \DisallowedCall ;
17
13
use Spaze \PHPStan \Rules \Disallowed \DisallowedCallFactory ;
18
- use Spaze \PHPStan \Rules \Disallowed \Normalizer \Normalizer ;
19
- use Spaze \PHPStan \Rules \Disallowed \RuleErrors \DisallowedCallsRuleErrors ;
20
- use Spaze \PHPStan \Rules \Disallowed \RuleErrors \ErrorIdentifiers ;
21
- use Spaze \PHPStan \Rules \Disallowed \Type \TypeResolver ;
14
+ use Spaze \PHPStan \Rules \Disallowed \RuleErrors \DisallowedFunctionRuleErrors ;
22
15
23
16
/**
24
17
* Reports on dynamically calling a disallowed function.
29
22
class FunctionCalls implements Rule
30
23
{
31
24
32
- private DisallowedCallsRuleErrors $ disallowedCallsRuleErrors ;
25
+ private DisallowedFunctionRuleErrors $ disallowedFunctionRuleErrors ;
33
26
34
27
/** @var list<DisallowedCall> */
35
28
private array $ disallowedCalls ;
36
29
37
- private ReflectionProvider $ reflectionProvider ;
38
-
39
- private Normalizer $ normalizer ;
40
-
41
- private TypeResolver $ typeResolver ;
42
-
43
30
44
31
/**
45
- * @param DisallowedCallsRuleErrors $disallowedCallsRuleErrors
32
+ * @param DisallowedFunctionRuleErrors $disallowedFunctionRuleErrors
46
33
* @param DisallowedCallFactory $disallowedCallFactory
47
- * @param ReflectionProvider $reflectionProvider
48
- * @param Normalizer $normalizer
49
- * @param TypeResolver $typeResolver
50
34
* @param array $forbiddenCalls
51
35
* @phpstan-param ForbiddenCallsConfig $forbiddenCalls
52
36
* @noinspection PhpUndefinedClassInspection ForbiddenCallsConfig is a type alias defined in PHPStan config
53
37
* @throws ShouldNotHappenException
54
38
*/
55
39
public function __construct (
56
- DisallowedCallsRuleErrors $ disallowedCallsRuleErrors ,
40
+ DisallowedFunctionRuleErrors $ disallowedFunctionRuleErrors ,
57
41
DisallowedCallFactory $ disallowedCallFactory ,
58
- ReflectionProvider $ reflectionProvider ,
59
- Normalizer $ normalizer ,
60
- TypeResolver $ typeResolver ,
61
42
array $ forbiddenCalls
62
43
) {
63
- $ this ->disallowedCallsRuleErrors = $ disallowedCallsRuleErrors ;
44
+ $ this ->disallowedFunctionRuleErrors = $ disallowedFunctionRuleErrors ;
64
45
$ this ->disallowedCalls = $ disallowedCallFactory ->createFromConfig ($ forbiddenCalls );
65
- $ this ->reflectionProvider = $ reflectionProvider ;
66
- $ this ->normalizer = $ normalizer ;
67
- $ this ->typeResolver = $ typeResolver ;
68
46
}
69
47
70
48
@@ -82,40 +60,7 @@ public function getNodeType(): string
82
60
*/
83
61
public function processNode (Node $ node , Scope $ scope ): array
84
62
{
85
- if ($ node ->name instanceof Name) {
86
- $ namespacedName = $ node ->name ->getAttribute ('namespacedName ' );
87
- if ($ namespacedName !== null && !($ namespacedName instanceof Name)) {
88
- throw new ShouldNotHappenException ();
89
- }
90
- $ names = [$ namespacedName , $ node ->name ];
91
- } elseif ($ node ->name instanceof String_) {
92
- $ names = [new Name ($ this ->normalizer ->normalizeNamespace ($ node ->name ->value ))];
93
- } elseif ($ node ->name instanceof Variable) {
94
- $ value = $ this ->typeResolver ->getVariableStringValue ($ node ->name , $ scope );
95
- if (!is_string ($ value )) {
96
- return [];
97
- }
98
- $ names = [new Name ($ this ->normalizer ->normalizeNamespace ($ value ))];
99
- } else {
100
- return [];
101
- }
102
- $ displayName = $ node ->name ->getAttribute ('originalName ' );
103
- if ($ displayName !== null && !($ displayName instanceof Name)) {
104
- throw new ShouldNotHappenException ();
105
- }
106
- foreach ($ names as $ name ) {
107
- if ($ name && $ this ->reflectionProvider ->hasFunction ($ name , $ scope )) {
108
- $ functionReflection = $ this ->reflectionProvider ->getFunction ($ name , $ scope );
109
- $ definedIn = $ functionReflection ->isBuiltin () ? null : $ functionReflection ->getFileName ();
110
- } else {
111
- $ definedIn = null ;
112
- }
113
- $ message = $ this ->disallowedCallsRuleErrors ->get ($ node , $ scope , (string )$ name , (string )($ displayName ?? $ name ), $ definedIn , $ this ->disallowedCalls , ErrorIdentifiers::DISALLOWED_FUNCTION );
114
- if ($ message ) {
115
- return $ message ;
116
- }
117
- }
118
- return [];
63
+ return $ this ->disallowedFunctionRuleErrors ->get ($ node , $ scope , $ this ->disallowedCalls );
119
64
}
120
65
121
66
}
0 commit comments