36
36
use function explode ;
37
37
use function implode ;
38
38
use function is_callable ;
39
+ use function is_string ;
39
40
use function ksort ;
40
41
use function ltrim ;
41
42
use function mb_strlen ;
@@ -238,7 +239,7 @@ protected static function printDescription(array $options, $def, string $indenta
238
239
return static ::printDescriptionWithComments ($ description , $ indentation , $ firstInBlock );
239
240
}
240
241
241
- $ preferMultipleLines = mb_strlen ($ description ) > static :: LINE_LENGTH ;
242
+ $ preferMultipleLines = static :: isMultiline ($ description );
242
243
$ blockString = BlockString::print ($ description , '' , $ preferMultipleLines );
243
244
$ prefix = $ indentation !== '' && ! $ firstInBlock
244
245
? "\n" . $ indentation
@@ -288,7 +289,7 @@ protected static function printArgs(array $options, array $args, string $indenta
288
289
// Multiline?
289
290
$ serialized = '' ;
290
291
291
- if ($ length > static ::LINE_LENGTH || $ description ) {
292
+ if ($ description || static ::isMultiline ( $ length ) ) {
292
293
$ serialized = "( \n" . implode ("\n" , $ arguments ) . "\n{$ indentation }) " ;
293
294
} else {
294
295
$ serialized = '( ' . implode (', ' , array_map ('trim ' , $ arguments )) . ') ' ;
@@ -307,10 +308,7 @@ protected static function printArg(FieldArgument $type, array $options, string $
307
308
$ indentation .
308
309
static ::printInputValue ($ type ) .
309
310
static ::printTypeDirectives ($ type , $ options , $ indentation );
310
-
311
- if (!$ firstInBlock && mb_strlen ($ field ) > static ::LINE_LENGTH ) {
312
- $ field = "\n" .ltrim ($ field , "\n" );
313
- }
311
+ $ field = static ::printLine ($ field , $ firstInBlock );
314
312
315
313
return $ field ;
316
314
}
@@ -384,10 +382,7 @@ protected static function printField(FieldDefinition $type, array $options, stri
384
382
': ' .
385
383
(string ) $ type ->getType () .
386
384
static ::printTypeDirectives ($ type , $ options , $ indentation );
387
-
388
- if (!$ firstInBlock && mb_strlen ($ field ) > static ::LINE_LENGTH ) {
389
- $ field = "\n" .ltrim ($ field , "\n" );
390
- }
385
+ $ field = static ::printLine ($ field , $ firstInBlock );
391
386
392
387
return $ field ;
393
388
}
@@ -492,10 +487,7 @@ protected static function printEnumValue(EnumValueDefinition $type, array $optio
492
487
' ' .
493
488
$ type ->name .
494
489
static ::printTypeDirectives ($ type , $ options , $ indentation );
495
-
496
- if (!$ firstInBlock && mb_strlen ($ value ) > static ::LINE_LENGTH ) {
497
- $ value = "\n" .ltrim ($ value , "\n" );
498
- }
490
+ $ value = static ::printLine ($ value , $ firstInBlock );
499
491
500
492
return $ value ;
501
493
}
@@ -531,10 +523,7 @@ protected static function printInputObjectField(InputObjectField $type, array $o
531
523
' ' .
532
524
static ::printInputValue ($ type ) .
533
525
static ::printTypeDirectives ($ type , $ options , $ indentation );
534
-
535
- if (!$ firstInBlock && mb_strlen ($ field ) > static ::LINE_LENGTH ) {
536
- $ field = "\n" .ltrim ($ field , "\n" );
537
- }
526
+ $ field = static ::printLine ($ field , $ firstInBlock );
538
527
539
528
return $ field ;
540
529
}
@@ -599,7 +588,7 @@ protected static function printTypeDirectives($type, array $options, string $ind
599
588
$ serialized = '' ;
600
589
601
590
if ($ directives ) {
602
- $ delimiter = $ length > static ::LINE_LENGTH ? "\n{$ indentation }" : ' ' ;
591
+ $ delimiter = static ::isMultiline ( $ length ) ? "\n{$ indentation }" : ' ' ;
603
592
$ serialized = $ delimiter .implode ($ delimiter , $ directives );
604
593
}
605
594
@@ -615,4 +604,19 @@ protected static function printTypeDirective(DirectiveNode $directive, array $op
615
604
{
616
605
return Printer::doPrint ($ directive );
617
606
}
607
+
608
+ protected static function printLine (string $ string , bool $ firstInBlock = true ): string {
609
+ if (!$ firstInBlock && static ::isMultiline ($ string )) {
610
+ $ string = "\n" .ltrim ($ string , "\n" );
611
+ }
612
+
613
+ return $ string ;
614
+ }
615
+
616
+ /**
617
+ * @param string|int $string
618
+ */
619
+ protected static function isMultiline ($ string ): bool {
620
+ return (is_string ($ string ) ? mb_strlen ($ string ) : $ string ) > static ::LINE_LENGTH ;
621
+ }
618
622
}
0 commit comments