@@ -292,7 +292,7 @@ private function addPropertyToClass(ClassLike $class, Node\Stmt\Property $node):
292
292
$ prop ->setVisibility ($ this ->toVisibility ($ node ->flags ));
293
293
$ prop ->setType ($ node ->type ? $ this ->toPhp ($ node ->type ) : null );
294
294
if ($ item ->default ) {
295
- $ prop ->setValue ($ this ->formatValue ($ item ->default , 1 ));
295
+ $ prop ->setValue ($ this ->toValue ($ item ->default ));
296
296
}
297
297
298
298
$ prop ->setReadOnly (method_exists ($ node , 'isReadonly ' ) && $ node ->isReadonly ());
@@ -315,7 +315,7 @@ private function addMethodToClass(ClassLike $class, Node\Stmt\ClassMethod $node)
315
315
private function addConstantToClass (ClassLike $ class , Node \Stmt \ClassConst $ node ): void
316
316
{
317
317
foreach ($ node ->consts as $ item ) {
318
- $ const = $ class ->addConstant ($ item ->name ->toString (), $ this ->formatValue ($ item ->value , 1 ));
318
+ $ const = $ class ->addConstant ($ item ->name ->toString (), $ this ->toValue ($ item ->value ));
319
319
$ const ->setVisibility ($ this ->toVisibility ($ node ->flags ));
320
320
$ const ->setFinal (method_exists ($ node , 'isFinal ' ) && $ node ->isFinal ());
321
321
$ this ->addCommentAndAttributes ($ const , $ node );
@@ -328,7 +328,7 @@ private function addEnumCaseToClass(EnumType $class, Node\Stmt\EnumCase $node):
328
328
$ value = match (true ) {
329
329
$ node ->expr === null => null ,
330
330
$ node ->expr instanceof Node \Scalar \LNumber, $ node ->expr instanceof Node \Scalar \String_ => $ node ->expr ->value ,
331
- default => $ this ->formatValue ($ node ->expr , 1 ),
331
+ default => $ this ->toValue ($ node ->expr ),
332
332
};
333
333
$ case = $ class ->addCase ($ node ->name ->toString (), $ value );
334
334
$ this ->addCommentAndAttributes ($ case , $ node );
@@ -358,11 +358,10 @@ private function addCommentAndAttributes(
358
358
foreach ($ group ->attrs as $ attribute ) {
359
359
$ args = [];
360
360
foreach ($ attribute ->args as $ arg ) {
361
- $ value = $ this ->formatValue ($ arg ->value , 0 );
362
361
if ($ arg ->name ) {
363
- $ args [$ arg ->name ->toString ()] = $ value ;
362
+ $ args [$ arg ->name ->toString ()] = $ this -> toValue ( $ arg -> value ) ;
364
363
} else {
365
- $ args [] = $ value ;
364
+ $ args [] = $ this -> toValue ( $ arg -> value ) ;
366
365
}
367
366
}
368
367
@@ -386,7 +385,7 @@ private function setupFunction(GlobalFunction|Method $function, Node\FunctionLik
386
385
$ param ->setReference ($ item ->byRef );
387
386
$ function ->setVariadic ($ item ->variadic );
388
387
if ($ item ->default ) {
389
- $ param ->setDefaultValue ($ this ->formatValue ($ item ->default , 2 ));
388
+ $ param ->setDefaultValue ($ this ->toValue ($ item ->default ));
390
389
}
391
390
392
391
$ this ->addCommentAndAttributes ($ param , $ item );
@@ -400,10 +399,42 @@ private function setupFunction(GlobalFunction|Method $function, Node\FunctionLik
400
399
}
401
400
402
401
403
- private function formatValue (Node \Expr $ value , int $ level ): Literal
402
+ private function toValue (Node \Expr $ node ): mixed
404
403
{
405
- $ value = $ this ->getReformattedContents ([$ value ], $ level );
406
- return new Literal ($ value );
404
+ if ($ node instanceof Node \Expr \ConstFetch) {
405
+ return match ($ node ->name ->toLowerString ()) {
406
+ 'null ' => null ,
407
+ 'true ' => true ,
408
+ 'false ' => false ,
409
+ default => new Literal ($ this ->getReformattedContents ([$ node ], 0 )),
410
+ };
411
+ } elseif ($ node instanceof Node \Scalar \LNumber
412
+ || $ node instanceof Node \Scalar \DNumber
413
+ || $ node instanceof Node \Scalar \String_
414
+ ) {
415
+ return $ node ->value ;
416
+
417
+ } elseif ($ node instanceof Node \Expr \Array_) {
418
+ $ res = [];
419
+ foreach ($ node ->items as $ item ) {
420
+ if ($ item ->unpack ) {
421
+ $ res [] = new Literal ($ this ->getReformattedContents ([$ item ], 0 ));
422
+
423
+ } elseif ($ item ->key ) {
424
+ $ key = $ item ->key instanceof Node \Identifier
425
+ ? $ item ->key ->name
426
+ : $ this ->toValue ($ item ->key );
427
+ $ res [$ key ] = $ this ->toValue ($ item ->value );
428
+
429
+ } else {
430
+ $ res [] = $ this ->toValue ($ item ->value );
431
+ }
432
+ }
433
+ return $ res ;
434
+
435
+ } else {
436
+ return new Literal ($ this ->getReformattedContents ([$ node ], 0 ));
437
+ }
407
438
}
408
439
409
440
0 commit comments