Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/Rules/Debug/DumpNativeTypeRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Debug;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use function count;
use function sprintf;
use function strtolower;

/**
* @implements Rule<Node\Expr\FuncCall>
*/
#[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(),
];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ final class CallToFunctionStatementWithoutSideEffectsRule implements Rule
];

public const PHPSTAN_TESTING_FUNCTIONS = [
'PHPStan\\dumpNativeType',
'PHPStan\\dumpType',
'PHPStan\\dumpPhpDocType',
'PHPStan\\debugScope',
Expand Down
12 changes: 12 additions & 0 deletions src/dumpType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading