Merge pull request #506 from spawnia/multiline-ternary

Spread ternary expressions across multiple lines
This commit is contained in:
Vladimir Razuvaev 2019-07-01 12:49:03 +07:00 committed by GitHub
commit 3b27abafca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 144 additions and 86 deletions

View File

@ -118,7 +118,9 @@ function defaultFieldResolver($source, $args, $context, \GraphQL\Type\Definition
} }
} }
return $property instanceof Closure ? $property($source, $args, $context, $info) : $property; return $property instanceof Closure
? $property($source, $args, $context, $info)
: $property;
} }
``` ```

View File

@ -182,6 +182,8 @@ class Executor
} }
} }
return $property instanceof Closure ? $property($source, $args, $context, $info) : $property; return $property instanceof Closure
? $property($source, $args, $context, $info)
: $property;
} }
} }

View File

@ -252,9 +252,9 @@ class ReferenceExecutor implements ExecutorImplementation
// //
// Similar to completeValueCatchingError. // Similar to completeValueCatchingError.
try { try {
$result = $operation->operation === 'mutation' ? $result = $operation->operation === 'mutation'
$this->executeFieldsSerially($type, $rootValue, $path, $fields) : ? $this->executeFieldsSerially($type, $rootValue, $path, $fields)
$this->executeFields($type, $rootValue, $path, $fields); : $this->executeFields($type, $rootValue, $path, $fields);
if ($this->isPromise($result)) { if ($this->isPromise($result)) {
return $result->then( return $result->then(
null, null,
@ -1324,9 +1324,9 @@ class ReferenceExecutor implements ExecutorImplementation
ResolveInfo $info, ResolveInfo $info,
&$result &$result
) { ) {
$runtimeType = is_string($runtimeTypeOrName) ? $runtimeType = is_string($runtimeTypeOrName)
$this->exeContext->schema->getType($runtimeTypeOrName) : ? $this->exeContext->schema->getType($runtimeTypeOrName)
$runtimeTypeOrName; : $runtimeTypeOrName;
if (! $runtimeType instanceof ObjectType) { if (! $runtimeType instanceof ObjectType) {
throw new InvariantViolation( throw new InvariantViolation(
sprintf( sprintf(

View File

@ -273,6 +273,7 @@ class Values
return $error->getMessage(); return $error->getMessage();
}, },
$errors $errors
) : []; )
: [];
} }
} }

View File

@ -512,15 +512,15 @@ class Parser
*/ */
private function parseVariableDefinitions() private function parseVariableDefinitions()
{ {
return $this->peek(Token::PAREN_L) ? return $this->peek(Token::PAREN_L)
$this->many( ? $this->many(
Token::PAREN_L, Token::PAREN_L,
function () { function () {
return $this->parseVariableDefinition(); return $this->parseVariableDefinition();
}, },
Token::PAREN_R Token::PAREN_R
) : )
new NodeList([]); : new NodeList([]);
} }
/** /**
@ -592,9 +592,9 @@ class Parser
*/ */
private function parseSelection() private function parseSelection()
{ {
return $this->peek(Token::SPREAD) ? return $this->peek(Token::SPREAD)
$this->parseFragment() : ? $this->parseFragment()
$this->parseField(); : $this->parseField();
} }
/** /**
@ -634,17 +634,17 @@ class Parser
*/ */
private function parseArguments($isConst) private function parseArguments($isConst)
{ {
$parseFn = $isConst ? $parseFn = $isConst
function () { ? function () {
return $this->parseConstArgument(); return $this->parseConstArgument();
} : }
function () { : function () {
return $this->parseArgument(); return $this->parseArgument();
}; };
return $this->peek(Token::PAREN_L) ? return $this->peek(Token::PAREN_L)
$this->many(Token::PAREN_L, $parseFn, Token::PAREN_R) : ? $this->many(Token::PAREN_L, $parseFn, Token::PAREN_R)
new NodeList([]); : new NodeList([]);
} }
/** /**
@ -1545,7 +1545,8 @@ class Parser
Token::BRACE_L, Token::BRACE_L,
[$this, 'parseOperationTypeDefinition'], [$this, 'parseOperationTypeDefinition'],
Token::BRACE_R Token::BRACE_R
) : []; )
: [];
if (count($directives) === 0 && count($operationTypes) === 0) { if (count($directives) === 0 && count($operationTypes) === 0) {
$this->unexpected(); $this->unexpected();
} }

View File

@ -251,8 +251,18 @@ class Visitor
$inArray = $stack['inArray']; $inArray = $stack['inArray'];
$stack = $stack['prev']; $stack = $stack['prev'];
} else { } else {
$key = $parent !== null ? ($inArray ? $index : $keys[$index]) : $UNDEFINED; $key = $parent !== null
$node = $parent !== null ? ($parent instanceof NodeList || is_array($parent) ? $parent[$key] : $parent->{$key}) : $newRoot; ? ($inArray
? $index
: $keys[$index]
)
: $UNDEFINED;
$node = $parent !== null
? ($parent instanceof NodeList || is_array($parent)
? $parent[$key]
: $parent->{$key}
)
: $newRoot;
if ($node === null || $node === $UNDEFINED) { if ($node === null || $node === $UNDEFINED) {
continue; continue;
} }

View File

@ -73,10 +73,14 @@ class Helper
} }
if (stripos($contentType, 'application/graphql') !== false) { if (stripos($contentType, 'application/graphql') !== false) {
$rawBody = $readRawBodyFn ? $readRawBodyFn() : $this->readRawBody(); $rawBody = $readRawBodyFn
? $readRawBodyFn()
: $this->readRawBody();
$bodyParams = ['query' => $rawBody ?: '']; $bodyParams = ['query' => $rawBody ?: ''];
} elseif (stripos($contentType, 'application/json') !== false) { } elseif (stripos($contentType, 'application/json') !== false) {
$rawBody = $readRawBodyFn ? $readRawBodyFn() : $this->readRawBody(); $rawBody = $readRawBodyFn ?
$readRawBodyFn()
: $this->readRawBody();
$bodyParams = json_decode($rawBody ?: '', true); $bodyParams = json_decode($rawBody ?: '', true);
if (json_last_error()) { if (json_last_error()) {
@ -272,7 +276,9 @@ class Helper
); );
} }
$doc = $op->queryId ? $this->loadPersistedQuery($config, $op) : $op->query; $doc = $op->queryId
? $this->loadPersistedQuery($config, $op)
: $op->query;
if (! $doc instanceof DocumentNode) { if (! $doc instanceof DocumentNode) {
$doc = Parser::parse($doc); $doc = Parser::parse($doc);

View File

@ -69,7 +69,9 @@ class InputObjectType extends Type implements InputType, NullableType, NamedType
if ($this->fields === null) { if ($this->fields === null) {
$this->fields = []; $this->fields = [];
$fields = $this->config['fields'] ?? []; $fields = $this->config['fields'] ?? [];
$fields = is_callable($fields) ? call_user_func($fields) : $fields; $fields = is_callable($fields)
? call_user_func($fields)
: $fields;
if (! is_array($fields)) { if (! is_array($fields)) {
throw new InvariantViolation( throw new InvariantViolation(

View File

@ -31,6 +31,8 @@ class ListOfType extends Type implements WrappingType, OutputType, NullableType,
{ {
$type = $this->ofType; $type = $this->ofType;
return $recurse && $type instanceof WrappingType ? $type->getWrappedType($recurse) : $type; return $recurse && $type instanceof WrappingType
? $type->getWrappedType($recurse)
: $type;
} }
} }

View File

@ -66,6 +66,8 @@ class NonNull extends Type implements WrappingType, OutputType, InputType
{ {
$type = $this->ofType; $type = $this->ofType;
return $recurse && $type instanceof WrappingType ? $type->getWrappedType($recurse) : $type; return $recurse && $type instanceof WrappingType
? $type->getWrappedType($recurse)
: $type;
} }
} }

View File

@ -185,7 +185,9 @@ class ObjectType extends Type implements OutputType, CompositeType, NullableType
{ {
if ($this->interfaces === null) { if ($this->interfaces === null) {
$interfaces = $this->config['interfaces'] ?? []; $interfaces = $this->config['interfaces'] ?? [];
$interfaces = is_callable($interfaces) ? call_user_func($interfaces) : $interfaces; $interfaces = is_callable($interfaces)
? call_user_func($interfaces)
: $interfaces;
if ($interfaces !== null && ! is_array($interfaces)) { if ($interfaces !== null && ! is_array($interfaces)) {
throw new InvariantViolation( throw new InvariantViolation(
@ -207,12 +209,14 @@ class ObjectType extends Type implements OutputType, CompositeType, NullableType
*/ */
public function isTypeOf($value, $context, ResolveInfo $info) public function isTypeOf($value, $context, ResolveInfo $info)
{ {
return isset($this->config['isTypeOf']) ? call_user_func( return isset($this->config['isTypeOf'])
? call_user_func(
$this->config['isTypeOf'], $this->config['isTypeOf'],
$value, $value,
$context, $context,
$info $info
) : null; )
: null;
} }
/** /**

View File

@ -345,7 +345,9 @@ abstract class Type implements JsonSerializable
*/ */
public static function getNullableType($type) public static function getNullableType($type)
{ {
return $type instanceof NonNull ? $type->getWrappedType() : $type; return $type instanceof NonNull
? $type->getWrappedType()
: $type;
} }
/** /**

View File

@ -488,7 +488,9 @@ EOD;
'type' => [ 'type' => [
'type' => Type::nonNull(self::_type()), 'type' => Type::nonNull(self::_type()),
'resolve' => static function ($value) { 'resolve' => static function ($value) {
return method_exists($value, 'getType') ? $value->getType() : $value->type; return method_exists($value, 'getType')
? $value->getType()
: $value->type;
}, },
], ],
'defaultValue' => [ 'defaultValue' => [

View File

@ -768,8 +768,9 @@ class SchemaValidationContext
); );
} }
return $union->astNode ? return $union->astNode
$union->astNode->types : null; ? $union->astNode->types
: null;
} }
private function validateEnumValues(EnumType $enumType) private function validateEnumValues(EnumType $enumType)
@ -824,8 +825,9 @@ class SchemaValidationContext
); );
} }
return $enum->astNode ? return $enum->astNode
$enum->astNode->values : null; ? $enum->astNode->values
: null;
} }
private function validateInputFields(InputObjectType $inputObj) private function validateInputFields(InputObjectType $inputObj)

View File

@ -394,8 +394,8 @@ class ASTDefinitionBuilder
function ($typeNode) { function ($typeNode) {
return $this->buildType($typeNode); return $this->buildType($typeNode);
} }
) : )
[], : [],
'astNode' => $def, 'astNode' => $def,
]); ]);
} }

View File

@ -609,7 +609,9 @@ class SchemaExtender
} }
$schemaExtensionASTNodes = count($schemaExtensions) > 0 $schemaExtensionASTNodes = count($schemaExtensions) > 0
? ($schema->extensionASTNodes ? array_merge($schema->extensionASTNodes, $schemaExtensions) : $schemaExtensions) ? ($schema->extensionASTNodes
? array_merge($schema->extensionASTNodes, $schemaExtensions)
: $schemaExtensions)
: $schema->extensionASTNodes; : $schema->extensionASTNodes;
$types = array_merge( $types = array_merge(

View File

@ -353,8 +353,8 @@ class SchemaPrinter
private static function printObject(ObjectType $type, array $options) : string private static function printObject(ObjectType $type, array $options) : string
{ {
$interfaces = $type->getInterfaces(); $interfaces = $type->getInterfaces();
$implementedInterfaces = ! empty($interfaces) ? $implementedInterfaces = ! empty($interfaces)
' implements ' . implode( ? ' implements ' . implode(
' & ', ' & ',
array_map( array_map(
static function ($i) { static function ($i) {
@ -362,7 +362,8 @@ class SchemaPrinter
}, },
$interfaces $interfaces
) )
) : ''; )
: '';
return self::printDescription($options, $type) . return self::printDescription($options, $type) .
sprintf("type %s%s {\n%s\n}", $type->name, $implementedInterfaces, self::printFields($options, $type)); sprintf("type %s%s {\n%s\n}", $type->name, $implementedInterfaces, self::printFields($options, $type));

View File

@ -302,10 +302,12 @@ class TypeInfo
case $node instanceof InlineFragmentNode: case $node instanceof InlineFragmentNode:
case $node instanceof FragmentDefinitionNode: case $node instanceof FragmentDefinitionNode:
$typeConditionNode = $node->typeCondition; $typeConditionNode = $node->typeCondition;
$outputType = $typeConditionNode ? self::typeFromAST( $outputType = $typeConditionNode
? self::typeFromAST(
$schema, $schema,
$typeConditionNode $typeConditionNode
) : Type::getNamedType($this->getType()); )
: Type::getNamedType($this->getType());
$this->typeStack[] = Type::isOutputType($outputType) ? $outputType : null; $this->typeStack[] = Type::isOutputType($outputType) ? $outputType : null;
break; break;

View File

@ -19,12 +19,14 @@ class LoneSchemaDefinition extends ValidationRule
public function getVisitor(ValidationContext $context) public function getVisitor(ValidationContext $context)
{ {
$oldSchema = $context->getSchema(); $oldSchema = $context->getSchema();
$alreadyDefined = $oldSchema !== null ? ( $alreadyDefined = $oldSchema !== null
? (
$oldSchema->getAstNode() || $oldSchema->getAstNode() ||
$oldSchema->getQueryType() || $oldSchema->getQueryType() ||
$oldSchema->getMutationType() || $oldSchema->getMutationType() ||
$oldSchema->getSubscriptionType() $oldSchema->getSubscriptionType()
) : false; )
: false;
$schemaDefinitionsCount = 0; $schemaDefinitionsCount = 0;

View File

@ -473,24 +473,24 @@ class OverlappingFieldsCanBeMerged extends ValidationRule
private function doTypesConflict(OutputType $type1, OutputType $type2) private function doTypesConflict(OutputType $type1, OutputType $type2)
{ {
if ($type1 instanceof ListOfType) { if ($type1 instanceof ListOfType) {
return $type2 instanceof ListOfType ? return $type2 instanceof ListOfType
$this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType()) : ? $this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType())
true; : true;
} }
if ($type2 instanceof ListOfType) { if ($type2 instanceof ListOfType) {
return $type1 instanceof ListOfType ? return $type1 instanceof ListOfType
$this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType()) : ? $this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType())
true; : true;
} }
if ($type1 instanceof NonNull) { if ($type1 instanceof NonNull) {
return $type2 instanceof NonNull ? return $type2 instanceof NonNull
$this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType()) : ? $this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType())
true; : true;
} }
if ($type2 instanceof NonNull) { if ($type2 instanceof NonNull) {
return $type1 instanceof NonNull ? return $type1 instanceof NonNull
$this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType()) : ? $this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType())
true; : true;
} }
if (Type::isLeafType($type1) || Type::isLeafType($type2)) { if (Type::isLeafType($type1) || Type::isLeafType($type2)) {
return $type1 !== $type2; return $type1 !== $type2;

View File

@ -66,13 +66,15 @@ class ProvidedRequiredArgumentsOnDirectives extends ValidationRule
} }
$requiredArgsMap[$def->name->value] = Utils::keyMap( $requiredArgsMap[$def->name->value] = Utils::keyMap(
$arguments ? array_filter($arguments, static function (Node $argument) : bool { $arguments
? array_filter($arguments, static function (Node $argument) : bool {
return $argument instanceof NonNullTypeNode && return $argument instanceof NonNullTypeNode &&
( (
! isset($argument->defaultValue) || ! isset($argument->defaultValue) ||
$argument->defaultValue === null $argument->defaultValue === null
); );
}) : [], })
: [],
static function (NamedTypeNode $argument) : string { static function (NamedTypeNode $argument) : string {
return $argument->name->value; return $argument->name->value;
} }

View File

@ -70,7 +70,12 @@ class VisitorTest extends ValidatorTestCase
/** @var Node $node */ /** @var Node $node */
[$node, $key, $parent, $path, $ancestors] = $args; [$node, $key, $parent, $path, $ancestors] = $args;
$parentArray = $parent && ! is_array($parent) ? ($parent instanceof NodeList ? iterator_to_array($parent) : $parent->toArray()) : $parent; $parentArray = $parent && ! is_array($parent)
? ($parent instanceof NodeList
? iterator_to_array($parent)
: $parent->toArray()
)
: $parent;
self::assertInstanceOf(Node::class, $node); self::assertInstanceOf(Node::class, $node);
self::assertContains($node->kind, array_keys(NodeKind::$classMap)); self::assertContains($node->kind, array_keys(NodeKind::$classMap));
@ -114,7 +119,9 @@ class VisitorTest extends ValidatorTestCase
{ {
$result = $ast; $result = $ast;
foreach ($path as $key) { foreach ($path as $key) {
$resultArray = $result instanceof NodeList ? iterator_to_array($result) : $result->toArray(); $resultArray = $result instanceof NodeList
? iterator_to_array($result)
: $result->toArray();
self::assertArrayHasKey($key, $resultArray); self::assertArrayHasKey($key, $resultArray);
$result = $resultArray[$key]; $result = $resultArray[$key];
} }

View File

@ -219,7 +219,9 @@ class SchemaExtenderTest extends TestCase
{ {
$ast = Parser::parse(SchemaPrinter::doPrint($extendedSchema)); $ast = Parser::parse(SchemaPrinter::doPrint($extendedSchema));
$ast->definitions = array_values(array_filter( $ast->definitions = array_values(array_filter(
$ast->definitions instanceof NodeList ? iterator_to_array($ast->definitions->getIterator()) : $ast->definitions, $ast->definitions instanceof NodeList
? iterator_to_array($ast->definitions->getIterator())
: $ast->definitions,
function (Node $node) : bool { function (Node $node) : bool {
return ! in_array(Printer::doPrint($node), $this->testSchemaDefinitions, true); return ! in_array(Printer::doPrint($node), $this->testSchemaDefinitions, true);
} }

View File

@ -40,7 +40,9 @@ function renderClassMethod(ReflectionMethod $method) {
$def = $type . '$' . $p->getName(); $def = $type . '$' . $p->getName();
if ($p->isDefaultValueAvailable()) { if ($p->isDefaultValueAvailable()) {
$val = $p->isDefaultValueConstant() ? $p->getDefaultValueConstantName() : $p->getDefaultValue(); $val = $p->isDefaultValueConstant()
? $p->getDefaultValueConstantName()
: $p->getDefaultValue();
$def .= " = " . Utils::printSafeJson($val); $def .= " = " . Utils::printSafeJson($val);
} }