Skip to content

Commit 3d4ceaa

Browse files
committed
Code style.
1 parent c046687 commit 3d4ceaa

File tree

2 files changed

+52
-55
lines changed

2 files changed

+52
-55
lines changed

src/Utils/SchemaPrinter.php

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,17 @@
44

55
namespace GraphQL\Utils;
66

7-
use Closure;
87
use GraphQL\Error\Error;
98
use GraphQL\Language\AST\ArgumentNode;
109
use GraphQL\Language\AST\BooleanValueNode;
1110
use GraphQL\Language\AST\DirectiveNode;
12-
use GraphQL\Language\AST\EnumTypeDefinitionNode;
13-
use GraphQL\Language\AST\EnumValueDefinitionNode;
1411
use GraphQL\Language\AST\EnumValueNode;
1512
use GraphQL\Language\AST\FloatValueNode;
1613
use GraphQL\Language\AST\IntValueNode;
1714
use GraphQL\Language\AST\ListValueNode;
18-
use GraphQL\Language\AST\Node;
1915
use GraphQL\Language\AST\NullValueNode;
2016
use GraphQL\Language\AST\ObjectValueNode;
21-
use GraphQL\Language\AST\ScalarTypeDefinitionNode;
2217
use GraphQL\Language\AST\StringValueNode;
23-
use GraphQL\Language\AST\ValueNode;
2418
use GraphQL\Language\BlockString;
2519
use GraphQL\Language\Parser;
2620
use GraphQL\Language\Printer;
@@ -57,7 +51,7 @@
5751
use function mb_strpos;
5852
use function sprintf;
5953
use 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
/**

tests/Utils/SchemaPrinterTest.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Generator;
88
use GraphQL\Language\DirectiveLocation;
9-
use GraphQL\Language\Parser;
109
use GraphQL\Type\Definition\CustomScalarType;
1110
use GraphQL\Type\Definition\Directive;
1211
use GraphQL\Type\Definition\EnumType;
@@ -1277,9 +1276,10 @@ enum __TypeKind {
12771276
self::assertEquals($expected, $output);
12781277
}
12791278

1280-
public function testPrintDirectivesAst(): void {
1281-
$text = str_pad('a', 80, 'a');
1282-
$schema = /** @lang GraphQL */ <<<GRAPHQL
1279+
public function testPrintDirectivesAst(): void
1280+
{
1281+
$text = str_pad('a', 80, 'a');
1282+
$schema = /** @lang GraphQL */ <<<GRAPHQL
12831283
directive @test(
12841284
values: [String!]
12851285
value: String @test(value: "{$text}")
@@ -1579,34 +1579,35 @@ interface InterfaceB implements InterfaceA
15791579
= TypeA | TypeB
15801580

15811581
GRAPHQL;
1582-
$actual = SchemaPrinter::doPrint(BuildSchema::build($schema), [
1583-
'printDirectives' => static function(): bool {
1582+
$actual = SchemaPrinter::doPrint(BuildSchema::build($schema), [
1583+
'printDirectives' => static function (): bool {
15841584
return true;
15851585
},
15861586
]);
15871587

15881588
self::assertEquals($expected, $actual);
15891589
}
15901590

1591-
public function testPrintDirectivesDeprecated(): void {
1592-
$text = str_pad('a', 80, 'a');
1593-
$enum = new EnumType([
1591+
public function testPrintDirectivesDeprecated(): void
1592+
{
1593+
$text = str_pad('a', 80, 'a');
1594+
$enum = new EnumType([
15941595
'name' => 'Aaa',
15951596
'values' => [
15961597
'A' => [
15971598
'value' => 'AAA',
15981599
'description' => 'AAAAAAAAAAAAA',
1599-
'deprecationReason' => 'deprecated for tests'
1600+
'deprecationReason' => 'deprecated for tests',
16001601
],
16011602
'B' => [
16021603
'value' => 'AAA',
1603-
'deprecationReason' => $text
1604+
'deprecationReason' => $text,
16041605
],
1605-
]
1606+
],
16061607
]);
1607-
$schema = new Schema(['types' => [$enum]]);
1608+
$schema = new Schema(['types' => [$enum]]);
16081609
$actual = SchemaPrinter::doPrint($schema, [
1609-
'printDirectives' => static function(): bool {
1610+
'printDirectives' => static function (): bool {
16101611
return true;
16111612
},
16121613
]);

0 commit comments

Comments
 (0)