File tree Expand file tree Collapse file tree 3 files changed +74
-0
lines changed Expand file tree Collapse file tree 3 files changed +74
-0
lines changed 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 \DependencyInjection \AutowiredService ;
8+ use PHPStan \Reflection \ReflectionProvider ;
9+ use PHPStan \Rules \Rule ;
10+ use PHPStan \Rules \RuleErrorBuilder ;
11+ use PHPStan \Type \VerbosityLevel ;
12+ use function count ;
13+ use function sprintf ;
14+ use function strtolower ;
15+
16+ /**
17+ * @implements Rule<Node\Expr\FuncCall>
18+ */
19+ #[AutowiredService]
20+ final class DumpNativeTypeRule implements Rule
21+ {
22+
23+ public function __construct (private ReflectionProvider $ reflectionProvider )
24+ {
25+ }
26+
27+ public function getNodeType (): string
28+ {
29+ return Node \Expr \FuncCall::class;
30+ }
31+
32+ public function processNode (Node $ node , Scope $ scope ): array
33+ {
34+ if (!$ node ->name instanceof Node \Name) {
35+ return [];
36+ }
37+
38+ $ functionName = $ this ->reflectionProvider ->resolveFunctionName ($ node ->name , $ scope );
39+ if ($ functionName === null ) {
40+ return [];
41+ }
42+
43+ if (strtolower ($ functionName ) !== 'phpstan\dumpnativetype ' ) {
44+ return [];
45+ }
46+
47+ if (count ($ node ->getArgs ()) === 0 ) {
48+ return [];
49+ }
50+
51+ return [
52+ RuleErrorBuilder::message (
53+ sprintf (
54+ 'Dumped type: %s ' ,
55+ $ scope ->getNativeType ($ node ->getArgs ()[0 ]->value )->describe (VerbosityLevel::precise ()),
56+ ),
57+ )->nonIgnorable ()->identifier ('phpstan.dumpNativeType ' )->build (),
58+ ];
59+ }
60+
61+ }
Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ final class CallToFunctionStatementWithoutSideEffectsRule implements Rule
3030 ];
3131
3232 public const PHPSTAN_TESTING_FUNCTIONS = [
33+ 'PHPStan \\dumpNativeType ' ,
3334 'PHPStan \\dumpType ' ,
3435 'PHPStan \\dumpPhpDocType ' ,
3536 'PHPStan \\debugScope ' ,
Original file line number Diff line number Diff line change @@ -14,6 +14,18 @@ function dumpType($value) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
1414 return null ;
1515}
1616
17+ /**
18+ * @phpstan-pure
19+ * @param mixed $value
20+ * @return mixed
21+ *
22+ * @throws void
23+ */
24+ function dumpNativeType ($ value ) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
25+ {
26+ return null ;
27+ }
28+
1729/**
1830 * @phpstan-pure
1931 * @param mixed $value
You can’t perform that action at this time.
0 commit comments