4
4
5
5
namespace GraphQL \Utils ;
6
6
7
- use Closure ;
8
7
use GraphQL \Error \Error ;
9
8
use GraphQL \Language \AST \ArgumentNode ;
10
9
use GraphQL \Language \AST \BooleanValueNode ;
11
10
use GraphQL \Language \AST \DirectiveNode ;
12
- use GraphQL \Language \AST \EnumTypeDefinitionNode ;
13
- use GraphQL \Language \AST \EnumValueDefinitionNode ;
14
11
use GraphQL \Language \AST \EnumValueNode ;
15
12
use GraphQL \Language \AST \FloatValueNode ;
16
13
use GraphQL \Language \AST \IntValueNode ;
17
14
use GraphQL \Language \AST \ListValueNode ;
18
- use GraphQL \Language \AST \Node ;
19
15
use GraphQL \Language \AST \NullValueNode ;
20
16
use GraphQL \Language \AST \ObjectValueNode ;
21
- use GraphQL \Language \AST \ScalarTypeDefinitionNode ;
22
17
use GraphQL \Language \AST \StringValueNode ;
23
- use GraphQL \Language \AST \ValueNode ;
24
18
use GraphQL \Language \BlockString ;
25
19
use GraphQL \Language \Parser ;
26
20
use GraphQL \Language \Printer ;
57
51
use function mb_strpos ;
58
52
use function sprintf ;
59
53
use function str_replace ;
60
- use function strlen ;
54
+ use function trim ;
61
55
62
56
/**
63
57
* Prints the contents of a Schema in schema definition language.
@@ -289,13 +283,13 @@ protected static function printArgs(array $options, array $args, string $indenta
289
283
}
290
284
291
285
// Print arguments
292
- $ length = 0 ;
293
- $ arguments = [];
286
+ $ length = 0 ;
287
+ $ arguments = [];
294
288
$ description = false ;
295
289
296
290
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 );
299
293
$ description = $ description || mb_strlen ($ arg ->description ?? '' ) > 0 ;
300
294
$ arguments [] = $ value ;
301
295
}
@@ -324,9 +318,9 @@ protected static function printInputValue($arg): string
324
318
$ argDecl = $ arg ->name . ': ' . (string ) $ arg ->getType ();
325
319
if ($ arg ->defaultValueExists ()) {
326
320
// TODO Pass `options`.
327
- $ value = AST ::astFromValue ($ arg ->defaultValue , $ arg ->getType ());
321
+ $ value = AST ::astFromValue ($ arg ->defaultValue , $ arg ->getType ());
328
322
$ indentation = $ arg instanceof InputObjectField ? ' ' : ' ' ;
329
- $ argDecl .= ' = ' . static ::printValue ($ value , [], $ indentation );
323
+ $ argDecl .= ' = ' . static ::printValue ($ value , [], $ indentation );
330
324
}
331
325
332
326
return $ argDecl ;
@@ -442,14 +436,14 @@ protected static function printInterface(InterfaceType $type, array $options): s
442
436
*/
443
437
protected static function printUnion (UnionType $ type , array $ options ): string
444
438
{
445
- $ types = $ type ->getTypes ();
446
- $ types = count ($ types ) > 0
439
+ $ types = $ type ->getTypes ();
440
+ $ types = count ($ types ) > 0
447
441
? ' = ' . implode (' | ' , $ types )
448
442
: '' ;
449
443
$ directives = static ::printTypeDirectives ($ type , $ options , '' );
450
444
451
445
if (static ::isLineTooLong ($ directives )) {
452
- $ types = ltrim ($ types );
446
+ $ types = ltrim ($ types );
453
447
$ directives .= "\n" ;
454
448
}
455
449
@@ -536,15 +530,16 @@ protected static function printBlock(array $items): string
536
530
537
531
/**
538
532
* @param Type|EnumValueDefinition|EnumType|InterfaceType|FieldDefinition|UnionType|InputObjectType|InputObjectField|FieldArgument $type
539
- * @param array<string, bool> $options
533
+ * @param array<string, bool> $options
540
534
* @phpstan-param Options $options
541
535
*/
542
- protected static function printTypeDirectives ($ type , array $ options , string $ indentation = '' ): string {
536
+ protected static function printTypeDirectives ($ type , array $ options , string $ indentation = '' ): string
537
+ {
543
538
// Enabled?
544
- $ filter = $ options ['printDirectives ' ] ?? null ;
539
+ $ filter = $ options ['printDirectives ' ] ?? null ;
545
540
$ deprecatable = $ type instanceof EnumValueDefinition || $ type instanceof FieldDefinition;
546
541
547
- if (!is_callable ($ filter )) {
542
+ if (! is_callable ($ filter )) {
548
543
if ($ deprecatable ) {
549
544
return static ::printDeprecated ($ type );
550
545
}
@@ -553,44 +548,44 @@ protected static function printTypeDirectives($type, array $options, string $ind
553
548
}
554
549
555
550
// Collect directives
556
- $ node = $ type ->astNode ;
551
+ $ node = $ type ->astNode ;
557
552
$ nodeDirectives = [];
558
553
559
554
if ($ node !== null ) {
560
555
$ nodeDirectives = $ node ->directives ;
561
556
} elseif ($ deprecatable && $ type ->deprecationReason !== null ) {
562
557
// 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 ));
565
560
$ nodeDirectives [] = Parser::directive ("@ {$ name }(reason: {$ reason }) " );
566
- } else {
567
- // empty
568
561
}
569
562
570
563
if (count ($ nodeDirectives ) === 0 ) {
571
564
return '' ;
572
565
}
573
566
574
567
// Print
575
- $ length = 0 ;
568
+ $ length = 0 ;
576
569
$ directives = [];
577
570
578
571
foreach ($ nodeDirectives as $ nodeDirective ) {
579
- if (!$ filter ($ nodeDirective )) {
572
+ if (! $ filter ($ nodeDirective )) {
580
573
continue ;
581
574
}
582
575
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 );
585
578
$ directives [] = $ directive ;
586
579
}
587
580
588
581
// Multiline?
589
582
$ serialized = '' ;
590
583
591
584
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 );
594
589
}
595
590
596
591
// Return
@@ -603,12 +598,12 @@ protected static function printTypeDirectives($type, array $options, string $ind
603
598
*/
604
599
protected static function printTypeDirective (DirectiveNode $ directive , array $ options , string $ indentation ): string
605
600
{
606
- $ length = 0 ;
601
+ $ length = 0 ;
607
602
$ arguments = [];
608
603
609
604
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 );
612
607
$ arguments [] = $ value ;
613
608
}
614
609
@@ -628,7 +623,7 @@ protected static function printArgument(ArgumentNode $argument, array $options,
628
623
629
624
/**
630
625
* @param ObjectValueNode|ListValueNode|BooleanValueNode|IntValueNode|FloatValueNode|EnumValueNode|StringValueNode|NullValueNode|null $value
631
- * @param array<string, bool> $options
626
+ * @param array<string, bool> $options
632
627
* @phpstan-param Options $options
633
628
*/
634
629
protected static function printValue ($ value , array $ options , string $ indentation ): string
@@ -640,8 +635,8 @@ protected static function printValue($value, array $options, string $indentation
640
635
$ values = [];
641
636
642
637
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 );
645
640
$ values [] = $ string ;
646
641
}
647
642
@@ -679,7 +674,7 @@ protected static function printChildrenBlock(array $lines, string $begin, string
679
674
$ line = "\n{$ line }" ;
680
675
}
681
676
682
- $ block .= "{$ line }\n" ;
677
+ $ block .= "{$ line }\n" ;
683
678
$ wrapped = $ wrap ;
684
679
}
685
680
@@ -688,7 +683,7 @@ protected static function printChildrenBlock(array $lines, string $begin, string
688
683
$ block = implode (', ' , array_map ('trim ' , $ lines ));
689
684
}
690
685
691
- $ block = $ begin. $ block. $ end ;
686
+ $ block = $ begin . $ block . $ end ;
692
687
}
693
688
694
689
return $ block ;
@@ -697,8 +692,9 @@ protected static function printChildrenBlock(array $lines, string $begin, string
697
692
/**
698
693
* @param string|int $string
699
694
*/
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 ;
702
698
}
703
699
704
700
/**
0 commit comments