Skip to content

Commit 661078a

Browse files
authored
Use short closures when possible (#1103)
1 parent 985f3af commit 661078a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+750
-1219
lines changed

src/Type/Definition/ResolveInfo.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace GraphQL\Type\Definition;
44

55
use function array_merge_recursive;
6+
use ArrayObject;
67
use GraphQL\Language\AST\FieldNode;
78
use GraphQL\Language\AST\FragmentDefinitionNode;
89
use GraphQL\Language\AST\FragmentSpreadNode;
@@ -47,9 +48,9 @@ class ResolveInfo
4748
*
4849
* @api
4950
*
50-
* @var iterable<int, FieldNode>
51+
* @var ArrayObject<int, FieldNode>
5152
*/
52-
public iterable $fieldNodes = [];
53+
public ArrayObject $fieldNodes;
5354

5455
/**
5556
* Parent type of the field being resolved.
@@ -115,17 +116,17 @@ class ResolveInfo
115116
private QueryPlan $queryPlan;
116117

117118
/**
118-
* @param iterable<int, FieldNode> $fieldNodes
119-
* @param array<int, string|int> $path
120-
* @phpstan-param Path $path
119+
* @param ArrayObject<int, FieldNode> $fieldNodes
120+
* @param array<int, string|int> $path
121+
* @phpstan-param Path $path
121122
*
122123
* @param array<string, FragmentDefinitionNode> $fragments
123-
* @param mixed|null $rootValue
124-
* @param array<string, mixed> $variableValues
124+
* @param mixed|null $rootValue
125+
* @param array<string, mixed> $variableValues
125126
*/
126127
public function __construct(
127128
FieldDefinition $fieldDefinition,
128-
iterable $fieldNodes,
129+
ArrayObject $fieldNodes,
129130
ObjectType $parentType,
130131
array $path,
131132
Schema $schema,

src/Validator/Rules/NoFragmentCycles.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ public function getVisitor(QueryValidationContext $context): array
3838
$this->spreadPathIndexByName = [];
3939

4040
return [
41-
NodeKind::OPERATION_DEFINITION => static function (): VisitorOperation {
42-
return Visitor::skipNode();
43-
},
41+
NodeKind::OPERATION_DEFINITION => static fn (): VisitorOperation => Visitor::skipNode(),
4442
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) use ($context): VisitorOperation {
4543
$this->detectCycleRecursive($node, $context);
4644

src/Validator/Rules/UniqueFragmentNames.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ public function getVisitor(QueryValidationContext $context): array
2020
$this->knownFragmentNames = [];
2121

2222
return [
23-
NodeKind::OPERATION_DEFINITION => static function (): VisitorOperation {
24-
return Visitor::skipNode();
25-
},
23+
NodeKind::OPERATION_DEFINITION => static fn (): VisitorOperation => Visitor::skipNode(),
2624
NodeKind::FRAGMENT_DEFINITION => function (FragmentDefinitionNode $node) use ($context): VisitorOperation {
2725
$fragmentName = $node->name->value;
2826
if (! isset($this->knownFragmentNames[$fragmentName])) {

tests/Executor/AbstractPromiseTest.php

Lines changed: 55 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,10 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForInterface(): void
6262
'fields' => [
6363
'pets' => [
6464
'type' => Type::listOf($petType),
65-
'resolve' => static function (): array {
66-
return [
67-
new Dog('Odie', true),
68-
new Cat('Garfield', false),
69-
];
70-
},
65+
'resolve' => static fn (): array => [
66+
new Dog('Odie', true),
67+
new Cat('Garfield', false),
68+
],
7169
],
7270
],
7371
]),
@@ -115,11 +113,9 @@ public function testIsTypeOfCanBeRejected(): void
115113
$DogType = new ObjectType([
116114
'name' => 'Dog',
117115
'interfaces' => [$PetType],
118-
'isTypeOf' => static function (): Deferred {
119-
return new Deferred(static function (): void {
120-
throw new UserError('We are testing this error');
121-
});
122-
},
116+
'isTypeOf' => static fn (): Deferred => new Deferred(static function (): void {
117+
throw new UserError('We are testing this error');
118+
}),
123119
'fields' => [
124120
'name' => ['type' => Type::string()],
125121
'woofs' => ['type' => Type::boolean()],
@@ -129,11 +125,9 @@ public function testIsTypeOfCanBeRejected(): void
129125
$CatType = new ObjectType([
130126
'name' => 'Cat',
131127
'interfaces' => [$PetType],
132-
'isTypeOf' => static function ($obj): Deferred {
133-
return new Deferred(static function () use ($obj): bool {
134-
return $obj instanceof Cat;
135-
});
136-
},
128+
'isTypeOf' => static fn ($obj): Deferred => new Deferred(
129+
static fn (): bool => $obj instanceof Cat
130+
),
137131
'fields' => [
138132
'name' => ['type' => Type::string()],
139133
'meows' => ['type' => Type::boolean()],
@@ -146,12 +140,10 @@ public function testIsTypeOfCanBeRejected(): void
146140
'fields' => [
147141
'pets' => [
148142
'type' => Type::listOf($PetType),
149-
'resolve' => static function (): array {
150-
return [
151-
new Dog('Odie', true),
152-
new Cat('Garfield', false),
153-
];
154-
},
143+
'resolve' => static fn (): array => [
144+
new Dog('Odie', true),
145+
new Cat('Garfield', false),
146+
],
155147
],
156148
],
157149
]),
@@ -200,11 +192,9 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForUnion(): void
200192
{
201193
$dogType = new ObjectType([
202194
'name' => 'Dog',
203-
'isTypeOf' => static function ($obj): Deferred {
204-
return new Deferred(static function () use ($obj): bool {
205-
return $obj instanceof Dog;
206-
});
207-
},
195+
'isTypeOf' => static fn ($obj): Deferred => new Deferred(
196+
static fn (): bool => $obj instanceof Dog
197+
),
208198
'fields' => [
209199
'name' => ['type' => Type::string()],
210200
'woofs' => ['type' => Type::boolean()],
@@ -213,11 +203,9 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForUnion(): void
213203

214204
$catType = new ObjectType([
215205
'name' => 'Cat',
216-
'isTypeOf' => static function ($obj): Deferred {
217-
return new Deferred(static function () use ($obj): bool {
218-
return $obj instanceof Cat;
219-
});
220-
},
206+
'isTypeOf' => static fn ($obj): Deferred => new Deferred(
207+
static fn (): bool => $obj instanceof Cat
208+
),
221209
'fields' => [
222210
'name' => ['type' => Type::string()],
223211
'meows' => ['type' => Type::boolean()],
@@ -235,9 +223,10 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForUnion(): void
235223
'fields' => [
236224
'pets' => [
237225
'type' => Type::listOf($petType),
238-
'resolve' => static function (): array {
239-
return [new Dog('Odie', true), new Cat('Garfield', false)];
240-
},
226+
'resolve' => static fn (): array => [
227+
new Dog('Odie', true),
228+
new Cat('Garfield', false),
229+
],
241230
],
242231
],
243232
]),
@@ -330,15 +319,13 @@ public function testResolveTypeOnInterfaceYieldsUsefulError(): void
330319
'fields' => [
331320
'pets' => [
332321
'type' => Type::listOf($PetType),
333-
'resolve' => static function (): Deferred {
334-
return new Deferred(static function (): array {
335-
return [
336-
new Dog('Odie', true),
337-
new Cat('Garfield', false),
338-
new Human('Jon'),
339-
];
340-
});
341-
},
322+
'resolve' => static fn (): Deferred => new Deferred(
323+
static fn (): array => [
324+
new Dog('Odie', true),
325+
new Cat('Garfield', false),
326+
new Human('Jon'),
327+
]
328+
),
342329
],
343330
],
344331
]),
@@ -409,8 +396,8 @@ public function testResolveTypeOnUnionYieldsUsefulError(): void
409396

410397
$PetType = new UnionType([
411398
'name' => 'Pet',
412-
'resolveType' => static function ($obj) use ($DogType, $CatType, $HumanType): Deferred {
413-
return new Deferred(static function () use ($obj, $DogType, $CatType, $HumanType): ?Type {
399+
'resolveType' => static fn ($obj): Deferred => new Deferred(
400+
static function () use ($obj, $DogType, $CatType, $HumanType): ?Type {
414401
if ($obj instanceof Dog) {
415402
return $DogType;
416403
}
@@ -424,8 +411,8 @@ public function testResolveTypeOnUnionYieldsUsefulError(): void
424411
}
425412

426413
return null;
427-
});
428-
},
414+
}
415+
),
429416
'types' => [$DogType, $CatType],
430417
]);
431418

@@ -435,13 +422,11 @@ public function testResolveTypeOnUnionYieldsUsefulError(): void
435422
'fields' => [
436423
'pets' => [
437424
'type' => Type::listOf($PetType),
438-
'resolve' => static function (): array {
439-
return [
440-
new Dog('Odie', true),
441-
new Cat('Garfield', false),
442-
new Human('Jon'),
443-
];
444-
},
425+
'resolve' => static fn (): array => [
426+
new Dog('Odie', true),
427+
new Cat('Garfield', false),
428+
new Human('Jon'),
429+
],
445430
],
446431
],
447432
]),
@@ -489,8 +474,8 @@ public function testResolveTypeAllowsResolvingWithTypeName(): void
489474
{
490475
$PetType = new InterfaceType([
491476
'name' => 'Pet',
492-
'resolveType' => static function ($obj): Deferred {
493-
return new Deferred(static function () use ($obj) {
477+
'resolveType' => static fn ($obj): Deferred => new Deferred(
478+
static function () use ($obj) {
494479
if ($obj instanceof Dog) {
495480
return 'Dog';
496481
}
@@ -500,8 +485,8 @@ public function testResolveTypeAllowsResolvingWithTypeName(): void
500485
}
501486

502487
return null;
503-
});
504-
},
488+
}
489+
),
505490
'fields' => [
506491
'name' => ['type' => Type::string()],
507492
],
@@ -531,12 +516,10 @@ public function testResolveTypeAllowsResolvingWithTypeName(): void
531516
'fields' => [
532517
'pets' => [
533518
'type' => Type::listOf($PetType),
534-
'resolve' => static function (): array {
535-
return [
536-
new Dog('Odie', true),
537-
new Cat('Garfield', false),
538-
];
539-
},
519+
'resolve' => static fn (): array => [
520+
new Dog('Odie', true),
521+
new Cat('Garfield', false),
522+
],
540523
],
541524
],
542525
]),
@@ -575,11 +558,9 @@ public function testResolveTypeCanBeCaught(): void
575558
{
576559
$PetType = new InterfaceType([
577560
'name' => 'Pet',
578-
'resolveType' => static function (): Deferred {
579-
return new Deferred(static function (): void {
580-
throw new UserError('We are testing this error');
581-
});
582-
},
561+
'resolveType' => static fn (): Deferred => new Deferred(static function (): void {
562+
throw new UserError('We are testing this error');
563+
}),
583564
'fields' => [
584565
'name' => ['type' => Type::string()],
585566
],
@@ -609,12 +590,10 @@ public function testResolveTypeCanBeCaught(): void
609590
'fields' => [
610591
'pets' => [
611592
'type' => Type::listOf($PetType),
612-
'resolve' => static function (): array {
613-
return [
614-
new Dog('Odie', true),
615-
new Cat('Garfield', false),
616-
];
617-
},
593+
'resolve' => static fn (): array => [
594+
new Dog('Odie', true),
595+
new Cat('Garfield', false),
596+
],
618597
],
619598
],
620599
]),

0 commit comments

Comments
 (0)