File tree Expand file tree Collapse file tree 8 files changed +173
-1
lines changed
tests/PHPStan/Rules/Debug Expand file tree Collapse file tree 8 files changed +173
-1
lines changed Original file line number Diff line number Diff line change 6868 "PHPStan\\ " : [
6969 " src/"
7070 ]
71- }
71+ },
72+ "files" : [" src/dumpType.php" ]
7273 },
7374 "autoload-dev" : {
7475 "psr-4" : {
Original file line number Diff line number Diff line change @@ -268,6 +268,9 @@ parametersSchema:
268268 # internal - static reflection
269269 singleReflectionFile : schema (string (), nullable ())
270270
271+ rules :
272+ - PHPStan\Rules\Debug\DumpTypeRule
273+
271274services :
272275 -
273276 class : PhpParser\BuilderFactory
Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace PHPStan \Rules \Debug ;
4+
5+ use PhpParser \Node ;
6+ use PHPStan \Analyser \Scope ;
7+ use PHPStan \Reflection \ReflectionProvider ;
8+ use PHPStan \Rules \Rule ;
9+ use PHPStan \Rules \RuleErrorBuilder ;
10+ use PHPStan \Type \VerbosityLevel ;
11+
12+ /**
13+ * @implements Rule<Node\Expr\FuncCall>
14+ */
15+ class DumpTypeRule implements Rule
16+ {
17+
18+ private ReflectionProvider $ reflectionProvider ;
19+
20+ public function __construct (ReflectionProvider $ reflectionProvider )
21+ {
22+ $ this ->reflectionProvider = $ reflectionProvider ;
23+ }
24+
25+ public function getNodeType (): string
26+ {
27+ return Node \Expr \FuncCall::class;
28+ }
29+
30+ public function processNode (Node $ node , Scope $ scope ): array
31+ {
32+ if (!$ node ->name instanceof Node \Name) {
33+ return [];
34+ }
35+
36+ $ functionName = $ this ->reflectionProvider ->resolveFunctionName ($ node ->name , $ scope );
37+ if ($ functionName === null ) {
38+ return [];
39+ }
40+
41+ if (strtolower ($ functionName ) !== 'phpstan\dumptype ' ) {
42+ return [];
43+ }
44+
45+ if (count ($ node ->args ) === 0 ) {
46+ return [
47+ RuleErrorBuilder::message (sprintf ('Missing argument for %s() function call. ' , $ functionName ))
48+ ->nonIgnorable ()
49+ ->build (),
50+ ];
51+ }
52+
53+ return [
54+ RuleErrorBuilder::message (
55+ sprintf (
56+ 'Dumped type: %s ' ,
57+ $ scope ->getType ($ node ->args [0 ]->value )->describe (VerbosityLevel::precise ())
58+ )
59+ )->nonIgnorable ()->build (),
60+ ];
61+ }
62+
63+ }
Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace PHPStan ;
4+
5+ /**
6+ * @param mixed $value
7+ */
8+ function dumpType ($ value ): void // phpcs:ignore Squiz.Functions.GlobalFunction.Found
9+ {
10+ }
Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace PHPStan \Rules \Debug ;
4+
5+ use PHPStan \Rules \Rule ;
6+ use PHPStan \Testing \RuleTestCase ;
7+
8+ /**
9+ * @extends RuleTestCase<DumpTypeRule>
10+ */
11+ class DumpTypeRuleTest extends RuleTestCase
12+ {
13+
14+ protected function getRule (): Rule
15+ {
16+ return new DumpTypeRule ($ this ->createReflectionProvider ());
17+ }
18+
19+ public function testRuleInPhpStanNamespace (): void
20+ {
21+ $ this ->analyse ([__DIR__ . '/data/dump-type.php ' ], [
22+ [
23+ 'Dumped type: array&nonEmpty ' ,
24+ 10 ,
25+ ],
26+ [
27+ 'Missing argument for PHPStan\dumpType() function call. ' ,
28+ 11 ,
29+ ],
30+ ]);
31+ }
32+
33+ public function testRuleInDifferentNamespace (): void
34+ {
35+ $ this ->analyse ([__DIR__ . '/data/dump-type-ns.php ' ], [
36+ [
37+ 'Dumped type: array&nonEmpty ' ,
38+ 10 ,
39+ ],
40+ ]);
41+ }
42+
43+ public function testRuleInUse (): void
44+ {
45+ $ this ->analyse ([__DIR__ . '/data/dump-type-use.php ' ], [
46+ [
47+ 'Dumped type: array&nonEmpty ' ,
48+ 12 ,
49+ ],
50+ [
51+ 'Dumped type: array&nonEmpty ' ,
52+ 13 ,
53+ ],
54+ ]);
55+ }
56+
57+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace App \Foo ;
4+
5+ function (array $ a ) {
6+ if ($ a === []) {
7+ return ;
8+ }
9+
10+ \PHPStan \dumpType ($ a );
11+ dumpType ($ a );
12+ };
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace App \Foo ;
4+
5+ use function PHPStan \dumpType ;
6+
7+ function (array $ a ) {
8+ if ($ a === []) {
9+ return ;
10+ }
11+
12+ \PHPStan \dumpType ($ a );
13+ dumpType ($ a );
14+ };
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace PHPStan ;
4+
5+ function (array $ a ) {
6+ if ($ a === []) {
7+ return ;
8+ }
9+
10+ dumpType ($ a );
11+ dumpType ();
12+ };
You can’t perform that action at this time.
0 commit comments