Skip to content

Commit 1e5cb8e

Browse files
author
Kirill Nesmeyanov
committed
Add conditional types and generic hints
1 parent f6793ce commit 1e5cb8e

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/PrettyPrinter.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
use TypeLang\Parser\Node\Stmt\ClassConstNode;
1313
use TypeLang\Parser\Node\Stmt\Condition\Condition;
1414
use TypeLang\Parser\Node\Stmt\Condition\EqualConditionNode;
15+
use TypeLang\Parser\Node\Stmt\Condition\GreaterOrEqualThanConditionNode;
16+
use TypeLang\Parser\Node\Stmt\Condition\GreaterThanConditionNode;
17+
use TypeLang\Parser\Node\Stmt\Condition\LessOrEqualThanConditionNode;
18+
use TypeLang\Parser\Node\Stmt\Condition\LessThanConditionNode;
1519
use TypeLang\Parser\Node\Stmt\Condition\NotEqualConditionNode;
1620
use TypeLang\Parser\Node\Stmt\ConstMaskNode;
1721
use TypeLang\Parser\Node\Stmt\IntersectionTypeNode;
@@ -95,6 +99,7 @@ class PrettyPrinter extends Printer
9599

96100
/**
97101
* @return non-empty-string
102+
* @throws NonPrintableNodeException
98103
*/
99104
protected function make(Statement $stmt): string
100105
{
@@ -116,6 +121,7 @@ protected function make(Statement $stmt): string
116121

117122
/**
118123
* @return non-empty-string
124+
* @throws NonPrintableNodeException
119125
*/
120126
protected function printTypeListNode(TypesListNode $node): string
121127
{
@@ -126,6 +132,7 @@ protected function printTypeListNode(TypesListNode $node): string
126132

127133
/**
128134
* @return non-empty-string
135+
* @throws NonPrintableNodeException
129136
*/
130137
protected function printTernaryType(TernaryConditionNode $node): string
131138
{
@@ -141,18 +148,24 @@ protected function printTernaryType(TernaryConditionNode $node): string
141148

142149
/**
143150
* @return non-empty-string
151+
* @throws NonPrintableNodeException
144152
*/
145153
protected function printCondition(Condition $node): string
146154
{
147155
return match (true) {
148156
$node instanceof EqualConditionNode => 'is',
149157
$node instanceof NotEqualConditionNode => 'is not',
158+
$node instanceof GreaterOrEqualThanConditionNode => '>=',
159+
$node instanceof LessOrEqualThanConditionNode => '<=',
160+
$node instanceof GreaterThanConditionNode => '>',
161+
$node instanceof LessThanConditionNode => '<',
150162
default => throw NonPrintableNodeException::fromInvalidNode($node),
151163
};
152164
}
153165

154166
/**
155167
* @return non-empty-string
168+
* @throws NonPrintableNodeException
156169
*/
157170
protected function printNullableType(NullableTypeNode $node): string
158171
{
@@ -193,6 +206,7 @@ protected function printConstMaskNode(ConstMaskNode $node): string
193206

194207
/**
195208
* @return non-empty-string
209+
* @throws NonPrintableNodeException
196210
*/
197211
protected function printCallableTypeNode(CallableTypeNode $node): string
198212
{
@@ -253,6 +267,7 @@ class: LogicalTypeNode::class,
253267

254268
/**
255269
* @return non-empty-string
270+
* @throws NonPrintableNodeException
256271
*/
257272
protected function printCallableArgumentNode(ParameterNode $node): string
258273
{
@@ -340,6 +355,7 @@ protected function printLiteralNode(LiteralNode $node): string
340355

341356
/**
342357
* @return non-empty-string
358+
* @throws NonPrintableNodeException
343359
*/
344360
protected function printNamedTypeNode(NamedTypeNode $node): string
345361
{
@@ -357,6 +373,7 @@ protected function printNamedTypeNode(NamedTypeNode $node): string
357373

358374
/**
359375
* @return non-empty-string
376+
* @throws NonPrintableNodeException
360377
*/
361378
protected function printTemplateArgumentsNode(TemplateArgumentsListNode $params): string
362379
{
@@ -372,14 +389,22 @@ protected function printTemplateArgumentsNode(TemplateArgumentsListNode $params)
372389

373390
/**
374391
* @return non-empty-string
392+
* @throws NonPrintableNodeException
375393
*/
376394
protected function printTemplateArgumentNode(TemplateArgumentNode $param): string
377395
{
378-
return $this->make($param->value);
396+
$result = $this->make($param->value);
397+
398+
if ($param->hint !== null) {
399+
return $param->hint->toString() . ' ' . $result;
400+
}
401+
402+
return $result;
379403
}
380404

381405
/**
382406
* @return non-empty-string
407+
* @throws NonPrintableNodeException
383408
*/
384409
protected function printShapeFieldsNode(NamedTypeNode $node, FieldsListNode $shape): string
385410
{
@@ -402,6 +427,7 @@ protected function printShapeFieldsNode(NamedTypeNode $node, FieldsListNode $sha
402427

403428
/**
404429
* @return list<non-empty-string>
430+
* @throws NonPrintableNodeException
405431
*/
406432
private function getShapeFieldsNodes(NamedTypeNode $node, FieldsListNode $shape): array
407433
{
@@ -429,6 +455,7 @@ private function getShapeFieldsNodes(NamedTypeNode $node, FieldsListNode $shape)
429455

430456
/**
431457
* @return non-empty-string
458+
* @throws NonPrintableNodeException
432459
*/
433460
protected function printShapeFieldNode(FieldNode $field): string
434461
{

0 commit comments

Comments
 (0)