55
66use PhpParser \Node ;
77use PhpParser \Node \Expr \FuncCall ;
8- use PhpParser \Node \Expr \Variable ;
9- use PhpParser \Node \Name ;
10- use PhpParser \Node \Scalar \String_ ;
118use PHPStan \Analyser \Scope ;
12- use PHPStan \Reflection \ReflectionProvider ;
139use PHPStan \Rules \IdentifierRuleError ;
1410use PHPStan \Rules \Rule ;
1511use PHPStan \ShouldNotHappenException ;
1612use Spaze \PHPStan \Rules \Disallowed \DisallowedCall ;
1713use 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 ;
2215
2316/**
2417 * Reports on dynamically calling a disallowed function.
2922class FunctionCalls implements Rule
3023{
3124
32- private DisallowedCallsRuleErrors $ disallowedCallsRuleErrors ;
25+ private DisallowedFunctionRuleErrors $ disallowedFunctionRuleErrors ;
3326
3427 /** @var list<DisallowedCall> */
3528 private array $ disallowedCalls ;
3629
37- private ReflectionProvider $ reflectionProvider ;
38-
39- private Normalizer $ normalizer ;
40-
41- private TypeResolver $ typeResolver ;
42-
4330
4431 /**
45- * @param DisallowedCallsRuleErrors $disallowedCallsRuleErrors
32+ * @param DisallowedFunctionRuleErrors $disallowedFunctionRuleErrors
4633 * @param DisallowedCallFactory $disallowedCallFactory
47- * @param ReflectionProvider $reflectionProvider
48- * @param Normalizer $normalizer
49- * @param TypeResolver $typeResolver
5034 * @param array $forbiddenCalls
5135 * @phpstan-param ForbiddenCallsConfig $forbiddenCalls
5236 * @noinspection PhpUndefinedClassInspection ForbiddenCallsConfig is a type alias defined in PHPStan config
5337 * @throws ShouldNotHappenException
5438 */
5539 public function __construct (
56- DisallowedCallsRuleErrors $ disallowedCallsRuleErrors ,
40+ DisallowedFunctionRuleErrors $ disallowedFunctionRuleErrors ,
5741 DisallowedCallFactory $ disallowedCallFactory ,
58- ReflectionProvider $ reflectionProvider ,
59- Normalizer $ normalizer ,
60- TypeResolver $ typeResolver ,
6142 array $ forbiddenCalls
6243 ) {
63- $ this ->disallowedCallsRuleErrors = $ disallowedCallsRuleErrors ;
44+ $ this ->disallowedFunctionRuleErrors = $ disallowedFunctionRuleErrors ;
6445 $ this ->disallowedCalls = $ disallowedCallFactory ->createFromConfig ($ forbiddenCalls );
65- $ this ->reflectionProvider = $ reflectionProvider ;
66- $ this ->normalizer = $ normalizer ;
67- $ this ->typeResolver = $ typeResolver ;
6846 }
6947
7048
@@ -82,40 +60,7 @@ public function getNodeType(): string
8260 */
8361 public function processNode (Node $ node , Scope $ scope ): array
8462 {
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 );
11964 }
12065
12166}
0 commit comments