44
55namespace TypeLang \Printer ;
66
7- use TypeLang \Type \CallableTypeNode ;
8- use TypeLang \Type \ClassConstMaskNode ;
9- use TypeLang \Type \ClassConstNode ;
10- use TypeLang \Type \ConstMaskNode ;
11- use TypeLang \Type \IntersectionTypeNode ;
12- use TypeLang \Type \Literal \BoolLiteralNode ;
13- use TypeLang \Type \Literal \FloatLiteralNode ;
14- use TypeLang \Type \Literal \IntLiteralNode ;
7+ use TypeLang \Printer \NativeTypePrinter \TypeAliases ;
8+ use TypeLang \Printer \NodePrinter \Callable \NativeCallableTypeNodePrinter ;
9+ use TypeLang \Printer \NodePrinter \Condition \NativeConditionalTypeNodePrinter ;
10+ use TypeLang \Printer \NodePrinter \LogicalTypeNodePrinter \NativeIntersectionTypeNodePrinter ;
11+ use TypeLang \Printer \NodePrinter \LogicalTypeNodePrinter \NativeUnionTypeNodePrinter ;
12+ use TypeLang \Printer \NodePrinter \NativeFixedNodePrinter ;
13+ use TypeLang \Printer \NodePrinter \NativeLiteralNodePrinter ;
14+ use TypeLang \Printer \NodePrinter \NativeNamedTypeNodePrinter ;
15+ use TypeLang \Printer \NodePrinter \NativeNullableTypeNodePrinter ;
16+ use TypeLang \Printer \NodePrinter \Registry \CachedNodePrinterRegistry ;
17+ use TypeLang \Printer \NodePrinter \Registry \NodePrinterRegistry ;
18+ use TypeLang \Type \Callable \CallableTypeNode ;
19+ use TypeLang \Type \Condition \ConditionalTypeNode ;
20+ use TypeLang \Type \Constant \ClassConstMaskTypeNode ;
21+ use TypeLang \Type \Constant \ClassConstTypeNode ;
22+ use TypeLang \Type \Constant \ConstMaskTypeNode ;
1523use TypeLang \Type \Literal \LiteralNode ;
16- use TypeLang \Type \Literal \ NullLiteralNode ;
17- use TypeLang \Type \Literal \ StringLiteralNode ;
24+ use TypeLang \Type \LogicalTypeNode \ IntersectionTypeNode ;
25+ use TypeLang \Type \LogicalTypeNode \ UnionTypeNode ;
1826use TypeLang \Type \NamedTypeNode ;
19- use TypeLang \Type \TernaryExpressionNode ;
20- use TypeLang \Type \ThisNode ;
21- use TypeLang \Type \TypeOffsetAccessNode ;
22- use TypeLang \Type \TypesListNode ;
23- use TypeLang \Type \UnionTypeNode ;
24- use TypeLang \Type \WildcardNode ;
25-
26- class NativeTypePrinter extends PrettyTypePrinter
27+ use TypeLang \Type \ThisTypeNode ;
28+ use TypeLang \Type \TypeNode ;
29+ use TypeLang \Type \WrappingTypeNode \NullableTypeNode ;
30+ use TypeLang \Type \WrappingTypeNode \TypeOffsetAccessTypeNode ;
31+ use TypeLang \Type \WrappingTypeNode \TypesListTypeNode ;
32+
33+ /**
34+ * Prints the native PHP type a type stands for.
35+ */
36+ final class NativeTypePrinter implements TypePrinterInterface
2737{
28- /**
29- * @var array<non-empty-string, non-empty-string>
30- */
31- private array $ aliases = [];
38+ private readonly TypeAliases $ aliases ;
39+
40+ private readonly TypePrinter $ printer ;
3241
3342 /**
3443 * @param iterable<non-empty-string, non-empty-string> $aliases
35- * @param non-empty-string $newLine
36- * @param non-empty-string $indention
3744 */
3845 public function __construct (
3946 iterable $ aliases = [],
40- string $ newLine = self ::DEFAULT_NEW_LINE_DELIMITER ,
41- string $ indention = self ::DEFAULT_INDENTION ,
47+ PrinterOptions $ options = new PrinterOptions (),
4248 ) {
43- parent ::__construct ($ newLine , $ indention );
44-
45- // preload phan type aliases
46- // @phpstan-ignore-next-line : PHPStan false-positive, not a bug
47- $ this ->aliases += require __DIR__ . '/../resources/aliases/phan.php ' ;
48- // preload psalm type aliases
49- // @phpstan-ignore-next-line : PHPStan false-positive, not a bug
50- $ this ->aliases += require __DIR__ . '/../resources/aliases/psalm.php ' ;
51- // preload phpstan type aliases
52- // @phpstan-ignore-next-line : PHPStan false-positive, not a bug
53- $ this ->aliases += require __DIR__ . '/../resources/aliases/phpstan.php ' ;
49+ $ this ->aliases = new TypeAliases ();
5450
5551 foreach ($ aliases as $ alias => $ type ) {
56- $ this ->addTypeAlias ($ alias , $ type );
52+ $ this ->aliases -> add ($ alias , $ type );
5753 }
54+
55+ $ this ->printer = new TypePrinter (
56+ registry: new CachedNodePrinterRegistry ($ this ->createRegistry ()),
57+ options: $ options ,
58+ );
59+ }
60+
61+ private function createRegistry (): NodePrinterRegistry
62+ {
63+ return PrettyTypePrinter ::createRegistry ()
64+ ->with (LiteralNode::class, new NativeLiteralNodePrinter ())
65+ ->with (NamedTypeNode::class, new NativeNamedTypeNodePrinter ($ this ->aliases ))
66+ ->with (CallableTypeNode::class, new NativeCallableTypeNodePrinter ($ this ->aliases ))
67+ ->with (UnionTypeNode::class, new NativeUnionTypeNodePrinter ())
68+ ->with (IntersectionTypeNode::class, new NativeIntersectionTypeNodePrinter ())
69+ ->with (ConditionalTypeNode::class, new NativeConditionalTypeNodePrinter ())
70+ ->with (NullableTypeNode::class, new NativeNullableTypeNodePrinter ())
71+ ->with (ThisTypeNode::class, new NativeFixedNodePrinter ('self ' ))
72+ ->with (TypesListTypeNode::class, new NativeFixedNodePrinter ('iterable ' ))
73+ ->with (TypeOffsetAccessTypeNode::class, new NativeFixedNodePrinter ('mixed ' ))
74+ ->with (ClassConstTypeNode::class, new NativeFixedNodePrinter ('mixed ' ))
75+ ->with (ClassConstMaskTypeNode::class, new NativeFixedNodePrinter ('mixed ' ))
76+ ->with (ConstMaskTypeNode::class, new NativeFixedNodePrinter ('mixed ' ));
5877 }
5978
6079 /**
@@ -65,7 +84,7 @@ public function __construct(
6584 */
6685 public function addTypeAlias (string $ alias , string $ type ): void
6786 {
68- $ this ->aliases [ \strtolower ($ alias)] = $ type ;
87+ $ this ->aliases -> add ($ alias, $ type) ;
6988 }
7089
7190 /**
@@ -76,9 +95,7 @@ public function addTypeAlias(string $alias, string $type): void
7695 */
7796 public function addUnionTypeAlias (string $ alias , array $ types ): void
7897 {
79- \sort ($ types );
80-
81- $ this ->aliases [\strtolower ($ alias )] = \implode ('| ' , $ types );
98+ $ this ->aliases ->addUnion ($ alias , $ types );
8299 }
83100
84101 /**
@@ -89,161 +106,11 @@ public function addUnionTypeAlias(string $alias, array $types): void
89106 */
90107 public function addIntersectionTypeAlias (string $ alias , array $ types ): void
91108 {
92- \sort ($ types );
93-
94- $ this ->aliases [\strtolower ($ alias )] = \implode ('& ' , $ types );
95- }
96-
97- /**
98- * @return non-empty-string
99- */
100- #[\Override]
101- protected function printTypeListNode (TypesListNode $ node ): string
102- {
103- return 'iterable ' ;
104- }
105-
106- #[\Override]
107- protected function printTernaryType (TernaryExpressionNode $ node ): string
108- {
109- /** @var non-empty-string */
110- return $ this ->make (new UnionTypeNode ([$ node ->then , $ node ->else ]));
111- }
112-
113- #[\Override]
114- protected function printClassConstMaskNode (ClassConstMaskNode $ node ): string
115- {
116- return 'mixed ' ;
117- }
118-
119- #[\Override]
120- protected function printConstMaskNode (ConstMaskNode $ node ): string
121- {
122- return 'mixed ' ;
123- }
124-
125- #[\Override]
126- protected function printWildcardNode (WildcardNode $ node ): string
127- {
128- return 'mixed ' ;
129- }
130-
131- #[\Override]
132- protected function printThisNode (ThisNode $ node ): string
133- {
134- return 'self ' ;
135- }
136-
137- #[\Override]
138- protected function printClassConstNode (ClassConstNode $ node ): string
139- {
140- return 'mixed ' ;
141- }
142-
143- #[\Override]
144- protected function printUnionTypeNode (UnionTypeNode $ node ): string
145- {
146- $ shouldWrap = $ this ->nesting ++ > 0 ;
147-
148- $ result = $ this ->unwrapAndPrint ($ node );
149-
150- $ result = $ this ->formatUnionWithMixed ($ result );
151- $ result = $ this ->formatBoolWithTrueAndFalse ($ result );
152-
153- /** @var non-empty-string */
154- return \vsprintf ($ shouldWrap ? '(%s) ' : '%s ' , [
155- \implode ('| ' , [...\array_unique ($ result )]),
156- ]);
157- }
158-
159- /**
160- * Replace "true" + "false" pair into "bool"
161- *
162- * @param list<non-empty-string> $result
163- * @return list<non-empty-string>
164- */
165- private function formatBoolWithTrueAndFalse (array $ result ): array
166- {
167- $ containsBool = (\in_array ('true ' , $ result , true ) || \in_array ('\true ' , $ result , true ))
168- && (\in_array ('false ' , $ result , true ) || \in_array ('\false ' , $ result , true ));
169-
170- if (!$ containsBool ) {
171- return $ result ;
172- }
173-
174- $ filtered = \array_filter ($ result , static fn (string $ type ): bool
175- => !\in_array ($ type , ['true ' , 'false ' , '\true ' , '\false ' ], true ));
176- $ filtered [] = 'bool ' ;
177-
178- /** @var list<non-empty-string> */
179- return $ filtered ;
180- }
181-
182- /**
183- * Replace everything that contain "mixed" type
184- * if one of the types is "mixed".
185- *
186- * @param list<non-empty-string> $result
187- * @return list<non-empty-string>
188- */
189- private function formatUnionWithMixed (array $ result ): array
190- {
191- if (\in_array ('mixed ' , $ result , true )) {
192- return ['mixed ' ];
193- }
194-
195- return $ result ;
196- }
197-
198- #[\Override]
199- protected function printIntersectionTypeNode (IntersectionTypeNode $ node ): string
200- {
201- /** @var non-empty-string */
202- return \vsprintf ($ this ->nesting ++ > 0 ? '(%s) ' : '%s ' , [
203- \implode ('& ' , [...$ this ->unwrapAndPrint ($ node )]),
204- ]);
205- }
206-
207- #[\Override]
208- protected function printCallableTypeNode (CallableTypeNode $ node ): string
209- {
210- return $ this ->getTypeName ($ node ->name ->toString ());
211- }
212-
213- /**
214- * @param non-empty-string $name
215- * @return non-empty-string
216- */
217- protected function getTypeName (string $ name ): string
218- {
219- return $ this ->aliases [\strtolower ($ name )] ?? $ name ;
220- }
221-
222- #[\Override]
223- protected function printNamedTypeNode (NamedTypeNode $ node ): string
224- {
225- return $ this ->getTypeName ($ node ->name ->toString ());
226- }
227-
228- #[\Override]
229- protected function printLiteralNode (LiteralNode $ node ): string
230- {
231- /** @var non-empty-string */
232- return match (true ) {
233- $ node instanceof BoolLiteralNode => 'bool ' ,
234- $ node instanceof FloatLiteralNode => 'float ' ,
235- $ node instanceof IntLiteralNode => 'int ' ,
236- $ node instanceof NullLiteralNode => 'null ' ,
237- $ node instanceof StringLiteralNode => 'string ' ,
238- default => \get_debug_type ($ node ->value ),
239- };
109+ $ this ->aliases ->addIntersection ($ alias , $ types );
240110 }
241111
242- #[\Override]
243- protected function printTypeOffsetAccessNode (TypeOffsetAccessNode $ node ): string
112+ public function print (TypeNode $ stmt ): string
244113 {
245- // Getting an offset requires information about the data from which the
246- // offset is needed. Therefore, we return a mixed type.
247- return 'mixed ' ;
114+ return $ this ->printer ->print ($ stmt );
248115 }
249116}
0 commit comments