diff --git a/src/Rules/Debug/DumpNativeTypeRule.php b/src/Rules/Debug/DumpNativeTypeRule.php new file mode 100644 index 0000000000..cfb084c4e3 --- /dev/null +++ b/src/Rules/Debug/DumpNativeTypeRule.php @@ -0,0 +1,61 @@ + + */ +#[AutowiredService] +final class DumpNativeTypeRule implements Rule +{ + + public function __construct(private ReflectionProvider $reflectionProvider) + { + } + + public function getNodeType(): string + { + return Node\Expr\FuncCall::class; + } + + public function processNode(Node $node, Scope $scope): array + { + if (!$node->name instanceof Node\Name) { + return []; + } + + $functionName = $this->reflectionProvider->resolveFunctionName($node->name, $scope); + if ($functionName === null) { + return []; + } + + if (strtolower($functionName) !== 'phpstan\dumpnativetype') { + return []; + } + + if (count($node->getArgs()) === 0) { + return []; + } + + return [ + RuleErrorBuilder::message( + sprintf( + 'Dumped type: %s', + $scope->getNativeType($node->getArgs()[0]->value)->describe(VerbosityLevel::precise()), + ), + )->nonIgnorable()->identifier('phpstan.dumpNativeType')->build(), + ]; + } + +} diff --git a/src/Rules/Functions/CallToFunctionStatementWithoutSideEffectsRule.php b/src/Rules/Functions/CallToFunctionStatementWithoutSideEffectsRule.php index acbb863ed4..e7d9c46156 100644 --- a/src/Rules/Functions/CallToFunctionStatementWithoutSideEffectsRule.php +++ b/src/Rules/Functions/CallToFunctionStatementWithoutSideEffectsRule.php @@ -30,6 +30,7 @@ final class CallToFunctionStatementWithoutSideEffectsRule implements Rule ]; public const PHPSTAN_TESTING_FUNCTIONS = [ + 'PHPStan\\dumpNativeType', 'PHPStan\\dumpType', 'PHPStan\\dumpPhpDocType', 'PHPStan\\debugScope', diff --git a/src/dumpType.php b/src/dumpType.php index 3d4cda24f7..64da7d525b 100644 --- a/src/dumpType.php +++ b/src/dumpType.php @@ -14,6 +14,18 @@ function dumpType($value) // phpcs:ignore Squiz.Functions.GlobalFunction.Found return null; } +/** + * @phpstan-pure + * @param mixed $value + * @return mixed + * + * @throws void + */ +function dumpNativeType($value) // phpcs:ignore Squiz.Functions.GlobalFunction.Found +{ + return null; +} + /** * @phpstan-pure * @param mixed $value