44
55namespace GraphQL \Utils ;
66
7- use Closure ;
87use GraphQL \Error \Error ;
98use GraphQL \Language \AST \ArgumentNode ;
109use GraphQL \Language \AST \BooleanValueNode ;
1110use GraphQL \Language \AST \DirectiveNode ;
12- use GraphQL \Language \AST \EnumTypeDefinitionNode ;
13- use GraphQL \Language \AST \EnumValueDefinitionNode ;
1411use GraphQL \Language \AST \EnumValueNode ;
1512use GraphQL \Language \AST \FloatValueNode ;
1613use GraphQL \Language \AST \IntValueNode ;
1714use GraphQL \Language \AST \ListValueNode ;
18- use GraphQL \Language \AST \Node ;
1915use GraphQL \Language \AST \NullValueNode ;
2016use GraphQL \Language \AST \ObjectValueNode ;
21- use GraphQL \Language \AST \ScalarTypeDefinitionNode ;
2217use GraphQL \Language \AST \StringValueNode ;
23- use GraphQL \Language \AST \ValueNode ;
2418use GraphQL \Language \BlockString ;
2519use GraphQL \Language \Parser ;
2620use GraphQL \Language \Printer ;
5751use function mb_strpos ;
5852use function sprintf ;
5953use function str_replace ;
60- use function strlen ;
54+ use function trim ;
6155
6256/**
6357 * Prints the contents of a Schema in schema definition language.
@@ -289,13 +283,13 @@ protected static function printArgs(array $options, array $args, string $indenta
289283 }
290284
291285 // Print arguments
292- $ length = 0 ;
293- $ arguments = [];
286+ $ length = 0 ;
287+ $ arguments = [];
294288 $ description = false ;
295289
296290 foreach ($ args as $ i => $ arg ) {
297- $ value = static ::printArg ($ arg , $ options , ' ' . $ indentation , $ i === 0 );
298- $ length = $ length + mb_strlen ($ value );
291+ $ value = static ::printArg ($ arg , $ options , ' ' . $ indentation , $ i === 0 );
292+ $ length += mb_strlen ($ value );
299293 $ description = $ description || mb_strlen ($ arg ->description ?? '' ) > 0 ;
300294 $ arguments [] = $ value ;
301295 }
@@ -324,9 +318,9 @@ protected static function printInputValue($arg): string
324318 $ argDecl = $ arg ->name . ': ' . (string ) $ arg ->getType ();
325319 if ($ arg ->defaultValueExists ()) {
326320 // TODO Pass `options`.
327- $ value = AST ::astFromValue ($ arg ->defaultValue , $ arg ->getType ());
321+ $ value = AST ::astFromValue ($ arg ->defaultValue , $ arg ->getType ());
328322 $ indentation = $ arg instanceof InputObjectField ? ' ' : ' ' ;
329- $ argDecl .= ' = ' . static ::printValue ($ value , [], $ indentation );
323+ $ argDecl .= ' = ' . static ::printValue ($ value , [], $ indentation );
330324 }
331325
332326 return $ argDecl ;
@@ -442,14 +436,14 @@ protected static function printInterface(InterfaceType $type, array $options): s
442436 */
443437 protected static function printUnion (UnionType $ type , array $ options ): string
444438 {
445- $ types = $ type ->getTypes ();
446- $ types = count ($ types ) > 0
439+ $ types = $ type ->getTypes ();
440+ $ types = count ($ types ) > 0
447441 ? ' = ' . implode (' | ' , $ types )
448442 : '' ;
449443 $ directives = static ::printTypeDirectives ($ type , $ options , '' );
450444
451445 if (static ::isLineTooLong ($ directives )) {
452- $ types = ltrim ($ types );
446+ $ types = ltrim ($ types );
453447 $ directives .= "\n" ;
454448 }
455449
@@ -536,15 +530,16 @@ protected static function printBlock(array $items): string
536530
537531 /**
538532 * @param Type|EnumValueDefinition|EnumType|InterfaceType|FieldDefinition|UnionType|InputObjectType|InputObjectField|FieldArgument $type
539- * @param array<string, bool> $options
533+ * @param array<string, bool> $options
540534 * @phpstan-param Options $options
541535 */
542- protected static function printTypeDirectives ($ type , array $ options , string $ indentation = '' ): string {
536+ protected static function printTypeDirectives ($ type , array $ options , string $ indentation = '' ): string
537+ {
543538 // Enabled?
544- $ filter = $ options ['printDirectives ' ] ?? null ;
539+ $ filter = $ options ['printDirectives ' ] ?? null ;
545540 $ deprecatable = $ type instanceof EnumValueDefinition || $ type instanceof FieldDefinition;
546541
547- if (!is_callable ($ filter )) {
542+ if (! is_callable ($ filter )) {
548543 if ($ deprecatable ) {
549544 return static ::printDeprecated ($ type );
550545 }
@@ -553,44 +548,44 @@ protected static function printTypeDirectives($type, array $options, string $ind
553548 }
554549
555550 // Collect directives
556- $ node = $ type ->astNode ;
551+ $ node = $ type ->astNode ;
557552 $ nodeDirectives = [];
558553
559554 if ($ node !== null ) {
560555 $ nodeDirectives = $ node ->directives ;
561556 } elseif ($ deprecatable && $ type ->deprecationReason !== null ) {
562557 // TODO Is there a better way to create directive node?
563- $ name = Directive::DEPRECATED_NAME ;
564- $ reason = json_encode (static ::getDeprecatedReason ($ type ));
558+ $ name = Directive::DEPRECATED_NAME ;
559+ $ reason = json_encode (static ::getDeprecatedReason ($ type ));
565560 $ nodeDirectives [] = Parser::directive ("@ {$ name }(reason: {$ reason }) " );
566- } else {
567- // empty
568561 }
569562
570563 if (count ($ nodeDirectives ) === 0 ) {
571564 return '' ;
572565 }
573566
574567 // Print
575- $ length = 0 ;
568+ $ length = 0 ;
576569 $ directives = [];
577570
578571 foreach ($ nodeDirectives as $ nodeDirective ) {
579- if (!$ filter ($ nodeDirective )) {
572+ if (! $ filter ($ nodeDirective )) {
580573 continue ;
581574 }
582575
583- $ directive = static ::printTypeDirective ($ nodeDirective , $ options , $ indentation );
584- $ length = $ length + mb_strlen ($ directive );
576+ $ directive = static ::printTypeDirective ($ nodeDirective , $ options , $ indentation );
577+ $ length += mb_strlen ($ directive );
585578 $ directives [] = $ directive ;
586579 }
587580
588581 // Multiline?
589582 $ serialized = '' ;
590583
591584 if (count ($ directives ) > 0 ) {
592- $ delimiter = static ::isLineTooLong ($ length ) ? "\n{$ indentation }" : ' ' ;
593- $ serialized = $ delimiter .implode ($ delimiter , $ directives );
585+ $ delimiter = static ::isLineTooLong ($ length )
586+ ? "\n{$ indentation }"
587+ : ' ' ;
588+ $ serialized = $ delimiter . implode ($ delimiter , $ directives );
594589 }
595590
596591 // Return
@@ -603,12 +598,12 @@ protected static function printTypeDirectives($type, array $options, string $ind
603598 */
604599 protected static function printTypeDirective (DirectiveNode $ directive , array $ options , string $ indentation ): string
605600 {
606- $ length = 0 ;
601+ $ length = 0 ;
607602 $ arguments = [];
608603
609604 foreach ($ directive ->arguments as $ argument ) {
610- $ value = static ::printArgument ($ argument , $ options , ' ' . $ indentation );
611- $ length = $ length + mb_strlen ($ value );
605+ $ value = static ::printArgument ($ argument , $ options , ' ' . $ indentation );
606+ $ length += mb_strlen ($ value );
612607 $ arguments [] = $ value ;
613608 }
614609
@@ -628,7 +623,7 @@ protected static function printArgument(ArgumentNode $argument, array $options,
628623
629624 /**
630625 * @param ObjectValueNode|ListValueNode|BooleanValueNode|IntValueNode|FloatValueNode|EnumValueNode|StringValueNode|NullValueNode|null $value
631- * @param array<string, bool> $options
626+ * @param array<string, bool> $options
632627 * @phpstan-param Options $options
633628 */
634629 protected static function printValue ($ value , array $ options , string $ indentation ): string
@@ -640,8 +635,8 @@ protected static function printValue($value, array $options, string $indentation
640635 $ values = [];
641636
642637 foreach ($ value ->values as $ item ) {
643- $ string = ' ' . $ indentation. Printer::doPrint ($ item );
644- $ length = $ length + mb_strlen ($ string );
638+ $ string = ' ' . $ indentation . Printer::doPrint ($ item );
639+ $ length += mb_strlen ($ string );
645640 $ values [] = $ string ;
646641 }
647642
@@ -679,7 +674,7 @@ protected static function printChildrenBlock(array $lines, string $begin, string
679674 $ line = "\n{$ line }" ;
680675 }
681676
682- $ block .= "{$ line }\n" ;
677+ $ block .= "{$ line }\n" ;
683678 $ wrapped = $ wrap ;
684679 }
685680
@@ -688,7 +683,7 @@ protected static function printChildrenBlock(array $lines, string $begin, string
688683 $ block = implode (', ' , array_map ('trim ' , $ lines ));
689684 }
690685
691- $ block = $ begin. $ block. $ end ;
686+ $ block = $ begin . $ block . $ end ;
692687 }
693688
694689 return $ block ;
@@ -697,8 +692,9 @@ protected static function printChildrenBlock(array $lines, string $begin, string
697692 /**
698693 * @param string|int $string
699694 */
700- protected static function isLineTooLong ($ string ): bool {
701- return (is_string ($ string ) ? mb_strlen ($ string ) : $ string ) > static ::LINE_LENGTH ;
695+ protected static function isLineTooLong ($ string ): bool
696+ {
697+ return (is_string ($ string ) ? mb_strlen ($ string ) : $ string ) > self ::LINE_LENGTH ;
702698 }
703699
704700 /**
0 commit comments