Skip to content

Commit d066898

Browse files
Introduce dumpNativeType
1 parent 5ba5a86 commit d066898

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
}

src/Rules/Functions/CallToFunctionStatementWithoutSideEffectsRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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',

src/dumpType.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)