From aab010d7cd9ad0d1d65e4f8ddda80dd45ed04d75 Mon Sep 17 00:00:00 2001 From: Kirill Nesmeyanov Date: Sat, 11 Jan 2025 03:38:06 +0300 Subject: [PATCH] Fix native printer literals --- resources/aliases/phpstan.php | 1 - resources/aliases/psalm.php | 1 - src/NativeTypePrinter.php | 23 ++++++++++++++--------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/resources/aliases/phpstan.php b/resources/aliases/phpstan.php index 266af95..b3dad8b 100644 --- a/resources/aliases/phpstan.php +++ b/resources/aliases/phpstan.php @@ -43,7 +43,6 @@ 'non-empty-list' => 'array', // object 'callable-object' => 'object', - '$this' => 'object', // callable 'pure-callable' => 'callable', 'pure-closure' => '\Closure', diff --git a/resources/aliases/psalm.php b/resources/aliases/psalm.php index 27fe332..4aef6d6 100644 --- a/resources/aliases/psalm.php +++ b/resources/aliases/psalm.php @@ -44,7 +44,6 @@ 'private-properties-of' => 'array', 'properties-of' => 'array', // object - '$this' => 'object', 'callable-object' => 'object', 'stringable-object' => '\Stringable', // callable diff --git a/src/NativeTypePrinter.php b/src/NativeTypePrinter.php index f6a90ec..d6f3df9 100644 --- a/src/NativeTypePrinter.php +++ b/src/NativeTypePrinter.php @@ -4,7 +4,12 @@ namespace TypeLang\Printer; +use TypeLang\Parser\Node\Literal\BoolLiteralNode; +use TypeLang\Parser\Node\Literal\FloatLiteralNode; +use TypeLang\Parser\Node\Literal\IntLiteralNode; use TypeLang\Parser\Node\Literal\LiteralNode; +use TypeLang\Parser\Node\Literal\NullLiteralNode; +use TypeLang\Parser\Node\Literal\StringLiteralNode; use TypeLang\Parser\Node\Literal\VariableLiteralNode; use TypeLang\Parser\Node\Stmt\CallableTypeNode; use TypeLang\Parser\Node\Stmt\ClassConstMaskNode; @@ -226,15 +231,15 @@ protected function printNamedTypeNode(NamedTypeNode $node): string #[\Override] protected function printLiteralNode(LiteralNode $node): string { - if ($node instanceof VariableLiteralNode) { - if ($node->getValue() === 'this') { - return 'self'; - } - - return 'mixed'; - } - - return \get_debug_type($node->getRawValue()); + return match (true) { + $node instanceof BoolLiteralNode => 'bool', + $node instanceof FloatLiteralNode => 'float', + $node instanceof IntLiteralNode => 'int', + $node instanceof NullLiteralNode => 'null', + $node instanceof StringLiteralNode => 'string', + $node instanceof VariableLiteralNode => $node->getValue() === 'this' ? 'self' : 'mixed', + default => \get_debug_type($node->getValue()), + }; } #[\Override]