File tree Expand file tree Collapse file tree 4 files changed +94
-0
lines changed
src/Rules/VariableVariables
tests/Rules/VariableVariables Expand file tree Collapse file tree 4 files changed +94
-0
lines changed Original file line number Diff line number Diff line change @@ -130,6 +130,8 @@ conditionalTags:
130
130
phpstan.rules.rule : %strictRules.strictFunctionCalls%
131
131
PHPStan\Rules\SwitchConditions\MatchingTypeInSwitchCaseConditionRule :
132
132
phpstan.rules.rule : %strictRules.switchConditionsMatchingType%
133
+ PHPStan\Rules\VariableVariables\VariableFunctionCallRule :
134
+ phpstan.rules.rule : %strictRules.noVariableVariables%
133
135
PHPStan\Rules\VariableVariables\VariableMethodCallRule :
134
136
phpstan.rules.rule : %strictRules.noVariableVariables%
135
137
PHPStan\Rules\VariableVariables\VariableMethodCallableRule :
@@ -282,6 +284,9 @@ services:
282
284
-
283
285
class : PHPStan\Rules\SwitchConditions\MatchingTypeInSwitchCaseConditionRule
284
286
287
+ -
288
+ class : PHPStan\Rules\VariableVariables\VariableFunctionCallRule
289
+
285
290
-
286
291
class : PHPStan\Rules\VariableVariables\VariableMethodCallRule
287
292
Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types = 1 );
2
+
3
+ namespace PHPStan \Rules \VariableVariables ;
4
+
5
+ use PhpParser \Node ;
6
+ use PhpParser \Node \Expr \FuncCall ;
7
+ use PHPStan \Analyser \Scope ;
8
+ use PHPStan \Rules \Rule ;
9
+ use PHPStan \Rules \RuleErrorBuilder ;
10
+
11
+ /**
12
+ * @implements Rule<FuncCall>
13
+ */
14
+ class VariableFunctionCallRule implements Rule
15
+ {
16
+
17
+ public function getNodeType (): string
18
+ {
19
+ return FuncCall::class;
20
+ }
21
+
22
+ public function processNode (Node $ node , Scope $ scope ): array
23
+ {
24
+ if (!$ node ->name instanceof Node \Expr) {
25
+ return [];
26
+ }
27
+
28
+ if ($ scope ->getType ($ node ->name )->isCallable ()->yes ()) {
29
+ return [];
30
+ }
31
+
32
+ return [
33
+ RuleErrorBuilder::message ('Variable function call. ' )->identifier ('function.dynamicName ' )->build (),
34
+ ];
35
+ }
36
+
37
+ }
Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types = 1 );
2
+
3
+ namespace PHPStan \Rules \VariableVariables ;
4
+
5
+ use PHPStan \Rules \Rule ;
6
+ use PHPStan \Testing \RuleTestCase ;
7
+
8
+ /**
9
+ * @extends RuleTestCase<VariableFunctionCallRule>
10
+ */
11
+ class VariableFunctionCallRuleTest extends RuleTestCase
12
+ {
13
+
14
+ protected function getRule (): Rule
15
+ {
16
+ return new VariableFunctionCallRule ();
17
+ }
18
+
19
+ public function testRule (): void
20
+ {
21
+ $ this ->analyse ([__DIR__ . '/data/functions.php ' ], [
22
+ [
23
+ 'Variable function call. ' ,
24
+ 6 ,
25
+ ],
26
+ [
27
+ 'Variable function call. ' ,
28
+ 18 ,
29
+ ],
30
+ ]);
31
+ }
32
+
33
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ function (callable $ a , string $ b ) {
4
+ time ();
5
+ $ a ();
6
+ $ b ();
7
+ $ c = function () {};
8
+ $ c ();
9
+ $ d = fn ($ a ) => $ a ;
10
+ $ d ();
11
+ $ e = time (...);
12
+ $ e ();
13
+ $ f = 'time ' ;
14
+ $ f ();
15
+ $ g = ['PDO ' , 'connect ' ];
16
+ $ g ();
17
+ $ h = ['PDO ' , $ b ];
18
+ $ h ();
19
+ };
You can’t perform that action at this time.
0 commit comments