31
31
use function array_filter ;
32
32
use function array_keys ;
33
33
use function array_map ;
34
+ use function array_sum ;
34
35
use function array_values ;
35
36
use function count ;
36
37
use function explode ;
40
41
use function ksort ;
41
42
use function ltrim ;
42
43
use function mb_strlen ;
44
+ use function mb_strpos ;
43
45
use function self ;
44
46
use function sprintf ;
45
47
use function str_replace ;
@@ -239,7 +241,7 @@ protected static function printDescription(array $options, $def, string $indenta
239
241
return static ::printDescriptionWithComments ($ description , $ indentation , $ firstInBlock );
240
242
}
241
243
242
- $ preferMultipleLines = static ::isMultiline ($ description );
244
+ $ preferMultipleLines = static ::isLineTooLong ($ description );
243
245
$ blockString = BlockString::print ($ description , '' , $ preferMultipleLines );
244
246
$ prefix = $ indentation !== '' && ! $ firstInBlock
245
247
? "\n" . $ indentation
@@ -286,16 +288,8 @@ protected static function printArgs(array $options, array $args, string $indenta
286
288
$ arguments [] = $ value ;
287
289
}
288
290
289
- // Multiline?
290
- $ serialized = '' ;
291
-
292
- if ($ description || static ::isMultiline ($ length )) {
293
- $ serialized = "( \n" . implode ("\n" , $ arguments ) . "\n{$ indentation }) " ;
294
- } else {
295
- $ serialized = '( ' . implode (', ' , array_map ('trim ' , $ arguments )) . ') ' ;
296
- }
297
-
298
- return $ serialized ;
291
+ // Return
292
+ return static ::printLines ($ arguments , '( ' , ') ' , $ description || static ::isLineTooLong ($ length ), $ indentation );
299
293
}
300
294
301
295
/**
@@ -304,13 +298,10 @@ protected static function printArgs(array $options, array $args, string $indenta
304
298
*/
305
299
protected static function printArg (FieldArgument $ type , array $ options , string $ indentation = '' , bool $ firstInBlock = true ): string
306
300
{
307
- $ field = static ::printDescription ($ options , $ type , $ indentation , $ firstInBlock ) .
301
+ return static ::printDescription ($ options , $ type , $ indentation , $ firstInBlock ) .
308
302
$ indentation .
309
303
static ::printInputValue ($ type ) .
310
304
static ::printTypeDirectives ($ type , $ options , $ indentation );
311
- $ field = static ::printLine ($ field , $ firstInBlock );
312
-
313
- return $ field ;
314
305
}
315
306
316
307
/**
@@ -375,16 +366,13 @@ static function (FieldDefinition $f, int $i) use ($options): string {
375
366
*/
376
367
protected static function printField (FieldDefinition $ type , array $ options , string $ indentation = '' , bool $ firstInBlock = true ): string
377
368
{
378
- $ field = static ::printDescription ($ options , $ type , $ indentation , $ firstInBlock ) .
369
+ return static ::printDescription ($ options , $ type , $ indentation , $ firstInBlock ) .
379
370
' ' .
380
371
$ type ->name .
381
372
static ::printArgs ($ options , $ type ->args , ' ' ) .
382
373
': ' .
383
374
(string ) $ type ->getType () .
384
375
static ::printTypeDirectives ($ type , $ options , $ indentation );
385
- $ field = static ::printLine ($ field , $ firstInBlock );
386
-
387
- return $ field ;
388
376
}
389
377
390
378
/**
@@ -483,13 +471,10 @@ static function (EnumValueDefinition $value, int $i) use ($options): string {
483
471
*/
484
472
protected static function printEnumValue (EnumValueDefinition $ type , array $ options , string $ indentation = '' , bool $ firstInBlock = false ): string
485
473
{
486
- $ value = static ::printDescription ($ options , $ type , $ indentation , $ firstInBlock ) .
474
+ return static ::printDescription ($ options , $ type , $ indentation , $ firstInBlock ) .
487
475
' ' .
488
476
$ type ->name .
489
477
static ::printTypeDirectives ($ type , $ options , $ indentation );
490
- $ value = static ::printLine ($ value , $ firstInBlock );
491
-
492
- return $ value ;
493
478
}
494
479
495
480
/**
@@ -519,23 +504,18 @@ static function (InputObjectField $f, $i) use ($options): string {
519
504
*/
520
505
protected static function printInputObjectField (InputObjectField $ type , array $ options , string $ indentation = '' , bool $ firstInBlock = true ): string
521
506
{
522
- $ field = static ::printDescription ($ options , $ type , $ indentation , $ firstInBlock ) .
507
+ return static ::printDescription ($ options , $ type , $ indentation , $ firstInBlock ) .
523
508
' ' .
524
509
static ::printInputValue ($ type ) .
525
510
static ::printTypeDirectives ($ type , $ options , $ indentation );
526
- $ field = static ::printLine ($ field , $ firstInBlock );
527
-
528
- return $ field ;
529
511
}
530
512
531
513
/**
532
514
* @param array<string> $items
533
515
*/
534
516
protected static function printBlock (array $ items ): string
535
517
{
536
- return count ($ items ) > 0
537
- ? " { \n" . implode ("\n" , $ items ) . "\n} "
538
- : '' ;
518
+ return static ::printLines ($ items , ' { ' , '} ' , true );
539
519
}
540
520
541
521
/**
@@ -588,7 +568,7 @@ protected static function printTypeDirectives($type, array $options, string $ind
588
568
$ serialized = '' ;
589
569
590
570
if ($ directives ) {
591
- $ delimiter = static ::isMultiline ($ length ) ? "\n{$ indentation }" : ' ' ;
571
+ $ delimiter = static ::isLineTooLong ($ length ) ? "\n{$ indentation }" : ' ' ;
592
572
$ serialized = $ delimiter .implode ($ delimiter , $ directives );
593
573
}
594
574
@@ -605,18 +585,51 @@ protected static function printTypeDirective(DirectiveNode $directive, array $op
605
585
return Printer::doPrint ($ directive );
606
586
}
607
587
608
- protected static function printLine (string $ string , bool $ firstInBlock = true ): string {
609
- if (!$ firstInBlock && static ::isMultiline ($ string )) {
610
- $ string = "\n" .ltrim ($ string , "\n" );
588
+ /**
589
+ * @param array<string> $lines
590
+ */
591
+ protected static function printLines (array $ lines , string $ begin , string $ end , bool $ multiline , string $ indentation = '' ): string
592
+ {
593
+ $ block = '' ;
594
+
595
+ if ($ lines ) {
596
+ $ multiline = $ multiline || static ::isLineTooLong (array_sum (array_map ('mb_strlen ' , $ lines )));
597
+
598
+ if ($ multiline ) {
599
+ $ wrapped = false ;
600
+
601
+ for ($ i = 0 , $ c = count ($ lines ); $ i < $ c ; $ i ++) {
602
+ // If line too long and contains LF we wrap it by empty lines
603
+ $ line = trim ($ lines [$ i ], "\n" );
604
+ $ wrap = static ::isLineTooLong ($ line ) || mb_strpos ($ line , "\n" ) !== false ;
605
+
606
+ if ($ i === 0 ) {
607
+ $ line = "\n{$ line }" ;
608
+ }
609
+
610
+ if (($ wrap && $ i > 0 ) || $ wrapped ) {
611
+ $ line = "\n{$ line }" ;
612
+ }
613
+
614
+ $ block .= "{$ line }\n" ;
615
+ $ wrapped = $ wrap ;
616
+ }
617
+
618
+ $ block .= $ indentation ;
619
+ } else {
620
+ $ block = implode (', ' , array_map ('trim ' , $ lines ));
621
+ }
622
+
623
+ $ block = $ begin .$ block .$ end ;
611
624
}
612
625
613
- return $ string ;
626
+ return $ block ;
614
627
}
615
628
616
629
/**
617
630
* @param string|int $string
618
631
*/
619
- protected static function isMultiline ($ string ): bool {
632
+ protected static function isLineTooLong ($ string ): bool {
620
633
return (is_string ($ string ) ? mb_strlen ($ string ) : $ string ) > static ::LINE_LENGTH ;
621
634
}
622
635
}
0 commit comments