Skip to content

Commit

Permalink
Code style.
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed Oct 31, 2021
1 parent 13a189e commit 175423b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 55 deletions.
78 changes: 37 additions & 41 deletions src/Utils/SchemaPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@

namespace GraphQL\Utils;

use Closure;
use GraphQL\Error\Error;
use GraphQL\Language\AST\ArgumentNode;
use GraphQL\Language\AST\BooleanValueNode;
use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\EnumTypeDefinitionNode;
use GraphQL\Language\AST\EnumValueDefinitionNode;
use GraphQL\Language\AST\EnumValueNode;
use GraphQL\Language\AST\FloatValueNode;
use GraphQL\Language\AST\IntValueNode;
use GraphQL\Language\AST\ListValueNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NullValueNode;
use GraphQL\Language\AST\ObjectValueNode;
use GraphQL\Language\AST\ScalarTypeDefinitionNode;
use GraphQL\Language\AST\StringValueNode;
use GraphQL\Language\AST\ValueNode;
use GraphQL\Language\BlockString;
use GraphQL\Language\Parser;
use GraphQL\Language\Printer;
Expand Down Expand Up @@ -57,7 +51,7 @@
use function mb_strpos;
use function sprintf;
use function str_replace;
use function strlen;
use function trim;

/**
* Prints the contents of a Schema in schema definition language.
Expand Down Expand Up @@ -289,13 +283,13 @@ protected static function printArgs(array $options, array $args, string $indenta
}

// Print arguments
$length = 0;
$arguments = [];
$length = 0;
$arguments = [];
$description = false;

foreach ($args as $i => $arg) {
$value = static::printArg($arg, $options, ' ' . $indentation, $i === 0);
$length = $length + mb_strlen($value);
$value = static::printArg($arg, $options, ' ' . $indentation, $i === 0);
$length += mb_strlen($value);
$description = $description || mb_strlen($arg->description ?? '') > 0;
$arguments[] = $value;
}
Expand Down Expand Up @@ -324,9 +318,9 @@ protected static function printInputValue($arg): string
$argDecl = $arg->name . ': ' . (string) $arg->getType();
if ($arg->defaultValueExists()) {
// TODO Pass `options`.
$value = AST::astFromValue($arg->defaultValue, $arg->getType());
$value = AST::astFromValue($arg->defaultValue, $arg->getType());
$indentation = $arg instanceof InputObjectField ? ' ' : ' ';
$argDecl .= ' = ' . static::printValue($value, [], $indentation);
$argDecl .= ' = ' . static::printValue($value, [], $indentation);
}

return $argDecl;
Expand Down Expand Up @@ -442,14 +436,14 @@ protected static function printInterface(InterfaceType $type, array $options): s
*/
protected static function printUnion(UnionType $type, array $options): string
{
$types = $type->getTypes();
$types = count($types) > 0
$types = $type->getTypes();
$types = count($types) > 0
? ' = ' . implode(' | ', $types)
: '';
$directives = static::printTypeDirectives($type, $options, '');

if (static::isLineTooLong($directives)) {
$types = ltrim($types);
$types = ltrim($types);
$directives .= "\n";
}

Expand Down Expand Up @@ -536,15 +530,16 @@ protected static function printBlock(array $items): string

/**
* @param Type|EnumValueDefinition|EnumType|InterfaceType|FieldDefinition|UnionType|InputObjectType|InputObjectField|FieldArgument $type
* @param array<string, bool> $options
* @param array<string, bool> $options
* @phpstan-param Options $options
*/
protected static function printTypeDirectives($type, array $options, string $indentation = ''): string {
protected static function printTypeDirectives($type, array $options, string $indentation = ''): string
{
// Enabled?
$filter = $options['printDirectives'] ?? null;
$filter = $options['printDirectives'] ?? null;
$deprecatable = $type instanceof EnumValueDefinition || $type instanceof FieldDefinition;

if (!is_callable($filter)) {
if (! is_callable($filter)) {
if ($deprecatable) {
return static::printDeprecated($type);
}
Expand All @@ -553,44 +548,44 @@ protected static function printTypeDirectives($type, array $options, string $ind
}

// Collect directives
$node = $type->astNode;
$node = $type->astNode;
$nodeDirectives = [];

if ($node !== null) {
$nodeDirectives = $node->directives;
} elseif ($deprecatable && $type->deprecationReason !== null) {
// TODO Is there a better way to create directive node?
$name = Directive::DEPRECATED_NAME;
$reason = json_encode(static::getDeprecatedReason($type));
$name = Directive::DEPRECATED_NAME;
$reason = json_encode(static::getDeprecatedReason($type));
$nodeDirectives[] = Parser::directive("@{$name}(reason: {$reason})");
} else {
// empty
}

if (count($nodeDirectives) === 0) {
return '';
}

// Print
$length = 0;
$length = 0;
$directives = [];

foreach ($nodeDirectives as $nodeDirective) {
if (!$filter($nodeDirective)) {
if (! $filter($nodeDirective)) {
continue;
}

$directive = static::printTypeDirective($nodeDirective, $options, $indentation);
$length = $length + mb_strlen($directive);
$directive = static::printTypeDirective($nodeDirective, $options, $indentation);
$length += mb_strlen($directive);
$directives[] = $directive;
}

// Multiline?
$serialized = '';

if (count($directives) > 0) {
$delimiter = static::isLineTooLong($length) ? "\n{$indentation}" : ' ';
$serialized = $delimiter.implode($delimiter, $directives);
$delimiter = static::isLineTooLong($length)
? "\n{$indentation}"
: ' ';
$serialized = $delimiter . implode($delimiter, $directives);
}

// Return
Expand All @@ -603,12 +598,12 @@ protected static function printTypeDirectives($type, array $options, string $ind
*/
protected static function printTypeDirective(DirectiveNode $directive, array $options, string $indentation): string
{
$length = 0;
$length = 0;
$arguments = [];

foreach ($directive->arguments as $argument) {
$value = static::printArgument($argument, $options, ' '.$indentation);
$length = $length + mb_strlen($value);
$value = static::printArgument($argument, $options, ' ' . $indentation);
$length += mb_strlen($value);
$arguments[] = $value;
}

Expand All @@ -628,7 +623,7 @@ protected static function printArgument(ArgumentNode $argument, array $options,

/**
* @param ObjectValueNode|ListValueNode|BooleanValueNode|IntValueNode|FloatValueNode|EnumValueNode|StringValueNode|NullValueNode|null $value
* @param array<string, bool> $options
* @param array<string, bool> $options
* @phpstan-param Options $options
*/
protected static function printValue($value, array $options, string $indentation): string
Expand All @@ -640,8 +635,8 @@ protected static function printValue($value, array $options, string $indentation
$values = [];

foreach ($value->values as $item) {
$string = ' '.$indentation.Printer::doPrint($item);
$length = $length + mb_strlen($string);
$string = ' ' . $indentation . Printer::doPrint($item);
$length += mb_strlen($string);
$values[] = $string;
}

Expand Down Expand Up @@ -679,7 +674,7 @@ protected static function printChildrenBlock(array $lines, string $begin, string
$line = "\n{$line}";
}

$block .= "{$line}\n";
$block .= "{$line}\n";
$wrapped = $wrap;
}

Expand All @@ -688,7 +683,7 @@ protected static function printChildrenBlock(array $lines, string $begin, string
$block = implode(', ', array_map('trim', $lines));
}

$block = $begin.$block.$end;
$block = $begin . $block . $end;
}

return $block;
Expand All @@ -697,8 +692,9 @@ protected static function printChildrenBlock(array $lines, string $begin, string
/**
* @param string|int $string
*/
protected static function isLineTooLong($string): bool {
return (is_string($string) ? mb_strlen($string) : $string) > static::LINE_LENGTH;
protected static function isLineTooLong($string): bool
{
return (is_string($string) ? mb_strlen($string) : $string) > self::LINE_LENGTH;
}

/**
Expand Down
29 changes: 15 additions & 14 deletions tests/Utils/SchemaPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Generator;
use GraphQL\Language\DirectiveLocation;
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\CustomScalarType;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\EnumType;
Expand Down Expand Up @@ -1277,9 +1276,10 @@ enum __TypeKind {
self::assertEquals($expected, $output);
}

public function testPrintDirectivesAst(): void {
$text = str_pad('a', 80, 'a');
$schema = /** @lang GraphQL */ <<<GRAPHQL
public function testPrintDirectivesAst(): void
{
$text = str_pad('a', 80, 'a');
$schema = /** @lang GraphQL */ <<<GRAPHQL
directive @test(
values: [String!]
value: String @test(value: "{$text}")
Expand Down Expand Up @@ -1579,34 +1579,35 @@ interface InterfaceB implements InterfaceA
= TypeA | TypeB

GRAPHQL;
$actual = SchemaPrinter::doPrint(BuildSchema::build($schema), [
'printDirectives' => static function(): bool {
$actual = SchemaPrinter::doPrint(BuildSchema::build($schema), [
'printDirectives' => static function (): bool {
return true;
},
]);

self::assertEquals($expected, $actual);
}

public function testPrintDirectivesDeprecated(): void {
$text = str_pad('a', 80, 'a');
$enum = new EnumType([
public function testPrintDirectivesDeprecated(): void
{
$text = str_pad('a', 80, 'a');
$enum = new EnumType([
'name' => 'Aaa',
'values' => [
'A' => [
'value' => 'AAA',
'description' => 'AAAAAAAAAAAAA',
'deprecationReason' => 'deprecated for tests'
'deprecationReason' => 'deprecated for tests',
],
'B' => [
'value' => 'AAA',
'deprecationReason' => $text
'deprecationReason' => $text,
],
]
],
]);
$schema = new Schema(['types' => [$enum]]);
$schema = new Schema(['types' => [$enum]]);
$actual = SchemaPrinter::doPrint($schema, [
'printDirectives' => static function(): bool {
'printDirectives' => static function (): bool {
return true;
},
]);
Expand Down

0 comments on commit 175423b

Please sign in to comment.