@@ -100,7 +100,7 @@ public function listT(ListT $type): string
100100 {
101101 $ value = $ type ->value ->accept ($ this );
102102
103- $ elements = $ this -> arrayElements ( $ type ->elements );
103+ $ elements = implode ( ' , ' , array_map ( fn ( Type $ type ): string => $ type -> accept ( $ this ), $ type ->elements ) );
104104
105105 if ($ value === 'never ' ) {
106106 return \sprintf ('list{%s} ' , $ elements );
@@ -122,7 +122,7 @@ public function arrayT(ArrayT $type): string
122122 {
123123 $ value = $ type ->value ->accept ($ this );
124124
125- $ elements = $ this ->arrayElements ( $ type ->elements );
125+ $ elements = implode ( ' , ' , array_map ( $ this ->arrayElement (...), $ type ->elements ) );
126126
127127 if ($ value === 'never ' ) {
128128 return \sprintf ('array{%s} ' , $ elements );
@@ -145,27 +145,16 @@ public function arrayT(ArrayT $type): string
145145 }
146146
147147 /**
148- * @param array<ArrayElement> $elements
148+ * @return non-empty-string
149149 */
150- private function arrayElements ( array $ elements ): string
150+ protected function arrayElement ( ArrayElement $ element ): string
151151 {
152- if (array_is_list ($ elements ) && array_all ($ elements , static fn (ArrayElement $ e ) => !$ e ->isOptional )) {
153- return implode (', ' , array_map (
154- fn (ArrayElement $ element ): string => $ element ->type ->accept ($ this ),
155- $ elements ,
156- ));
157- }
158-
159- return implode (', ' , array_map (
160- fn (int |string $ key , ArrayElement $ element ): string => \sprintf (
161- '%s%s: %s ' ,
162- \is_int ($ key ) ? $ key : $ this ->stringValueT (new StringValueT ($ key )),
163- $ element ->isOptional ? '? ' : '' ,
164- $ element ->type ->accept ($ this ),
165- ),
166- array_keys ($ elements ),
167- $ elements ,
168- ));
152+ return \sprintf (
153+ '%s%s: %s ' ,
154+ \is_int ($ element ->key ) ? $ element ->key : $ this ->stringValueT (new StringValueT ($ element ->key )),
155+ $ element ->isOptional ? '? ' : '' ,
156+ $ element ->type ->accept ($ this ),
157+ );
169158 }
170159
171160 #[\Override]
@@ -208,7 +197,7 @@ public function objectT(ObjectT $type): string
208197 /**
209198 * @return non-empty-string
210199 */
211- private function property (Property $ property ): string
200+ protected function property (Property $ property ): string
212201 {
213202 return \sprintf ('%s%s: %s ' , $ property ->name , $ property ->isOptional ? '? ' : '' , $ property ->type ->accept ($ this ));
214203 }
@@ -256,7 +245,7 @@ public function callableT(CallableT $type): string
256245 'callable%s(%s): %s ' ,
257246 $ this ->templates ($ type ->templates ),
258247 implode (', ' , array_map ($ this ->parameter (...), $ type ->parameters )),
259- $ type ->returns ->accept ($ this ),
248+ $ type ->return ->accept ($ this ),
260249 );
261250 }
262251
@@ -267,31 +256,52 @@ public function closureT(ClosureT $type): string
267256 'Closure%s(%s): %s ' ,
268257 $ this ->templates ($ type ->templates ),
269258 implode (', ' , array_map ($ this ->parameter (...), $ type ->parameters )),
270- $ type ->returns ->accept ($ this ),
259+ $ type ->return ->accept ($ this ),
271260 );
272261 }
273262
274263 /**
275264 * @return non-empty-string
276265 */
277- private function parameter (Parameter $ parameter ): string
266+ protected function parameter (Parameter $ parameter ): string
278267 {
279- /** @phpstan-ignore return.type */
280- return \sprintf (
281- '%s%s%s%s ' ,
282- $ parameter ->type ->accept ($ this ),
283- $ parameter ->isPassedByReference ? '& ' : '' ,
284- $ parameter ->isVariadic ? '... ' : '' ,
285- $ parameter ->hasDefault ? '= ' : '' ,
286- );
268+ $ string = $ parameter ->type ->accept ($ this );
269+
270+ if ($ parameter ->name !== null ) {
271+ $ string .= ' ' ;
272+ }
273+
274+ if ($ parameter ->isPassedByReference ) {
275+ $ string .= '& ' ;
276+
277+ // todo $parameter->outType
278+ }
279+
280+ if ($ parameter ->isVariadic ) {
281+ $ string .= '... ' ;
282+ }
283+
284+ if ($ parameter ->name !== null ) {
285+ $ string .= '$ ' . $ parameter ->name ;
286+ }
287+
288+ if ($ parameter ->hasDefault ) {
289+ $ string .= '= ' ;
290+
291+ if ($ parameter ->defaultType !== null ) {
292+ $ string .= $ parameter ->defaultType ->accept ($ this );
293+ }
294+ }
295+
296+ return $ string ;
287297 }
288298
289299 /**
290300 * @param non-empty-string $name
291301 * @param list<Type> $templateArguments
292302 * @return non-empty-string
293303 */
294- private function constructor (string $ name , array $ templateArguments ): string
304+ protected function constructor (string $ name , array $ templateArguments ): string
295305 {
296306 if ($ templateArguments === []) {
297307 return $ name ;
@@ -384,7 +394,7 @@ public function ternaryT(TernaryT $type): string
384394 /**
385395 * @param list<Template> $templates
386396 */
387- private function templates (array $ templates ): string
397+ protected function templates (array $ templates ): string
388398 {
389399 if ($ templates === []) {
390400 return '' ;
@@ -393,13 +403,13 @@ private function templates(array $templates): string
393403 return \sprintf ('<%s> ' , implode (', ' , array_map ($ this ->template (...), $ templates )));
394404 }
395405
396- private function template (Template $ template ): string
406+ protected function template (Template $ template ): string
397407 {
398408 $ lowerBound = $ template ->lowerBound ->accept ($ this );
399409 $ upperBound = $ template ->upperBound ->accept ($ this );
400410
401411 return \sprintf (
402- '%s%s%s%s ' ,
412+ '%s%s%s%s%s ' ,
403413 match ($ template ->variance ) {
404414 Variance::Invariant => '' ,
405415 Variance::Covariant => 'out ' ,
@@ -408,6 +418,7 @@ private function template(Template $template): string
408418 $ this ->templateNames ()[$ template ->type ] ??= $ template ->name ,
409419 $ upperBound === 'mixed ' ? '' : ' of ' . $ upperBound ,
410420 $ lowerBound === 'never ' ? '' : ' super ' . $ lowerBound ,
421+ $ template ->default === null ? '' : ' = ' . $ template ->default ->accept ($ this ),
411422 );
412423 }
413424
@@ -417,7 +428,7 @@ private function template(Template $template): string
417428 /**
418429 * @return \SplObjectStorage<TemplateT, non-empty-string>
419430 */
420- private function templateNames (): \SplObjectStorage
431+ final protected function templateNames (): \SplObjectStorage
421432 {
422433 if ($ this ->templateNames !== null ) {
423434 return $ this ->templateNames ;
0 commit comments