From c4d44e79dc889423d4209af8eb2a0715e3f45286 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Fri, 18 Jul 2025 10:43:06 +0200 Subject: [PATCH 1/2] Introduce dumpNativeType --- src/Rules/Debug/DumpNativeTypeRule.php | 61 ++++++++++++++++++++++++++ src/dumpType.php | 12 +++++ 2 files changed, 73 insertions(+) create mode 100644 src/Rules/Debug/DumpNativeTypeRule.php 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/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 From 68e2495d012f858f155ab4dedfc926d4133fc5f0 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Mon, 21 Jul 2025 14:12:57 +0200 Subject: [PATCH 2/2] Update --- .../Functions/CallToFunctionStatementWithoutSideEffectsRule.php | 1 + 1 file changed, 1 insertion(+) 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',