Skip to content

Commit 1f0ec24

Browse files
authored
Fix native return type hints part II (#656)
1 parent e1bce32 commit 1f0ec24

35 files changed

Lines changed: 438 additions & 811 deletions

phpstan-baseline.neon

Lines changed: 0 additions & 425 deletions
Large diffs are not rendered by default.

src/Validator/Rules/LoneAnonymousOperation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static function (Node $definition) : bool {
3939
NodeKind::OPERATION_DEFINITION => static function (OperationDefinitionNode $node) use (
4040
&$operationCount,
4141
$context
42-
) {
42+
) : void {
4343
if ($node->name !== null || $operationCount <= 1) {
4444
return;
4545
}

tests/Executor/AbstractPromiseTest.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForInterface() : void
4141
'name' => 'Dog',
4242
'interfaces' => [$petType],
4343
'isTypeOf' => static function ($obj) {
44-
return new Deferred(static function () use ($obj) {
44+
return new Deferred(static function () use ($obj) : bool {
4545
return $obj instanceof Dog;
4646
});
4747
},
@@ -55,7 +55,7 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForInterface() : void
5555
'name' => 'Cat',
5656
'interfaces' => [$petType],
5757
'isTypeOf' => static function ($obj) {
58-
return new Deferred(static function () use ($obj) {
58+
return new Deferred(static function () use ($obj) : bool {
5959
return $obj instanceof Cat;
6060
});
6161
},
@@ -71,7 +71,7 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForInterface() : void
7171
'fields' => [
7272
'pets' => [
7373
'type' => Type::listOf($petType),
74-
'resolve' => static function () {
74+
'resolve' => static function () : array {
7575
return [
7676
new Dog('Odie', true),
7777
new Cat('Garfield', false),
@@ -124,8 +124,8 @@ public function testIsTypeOfCanBeRejected() : void
124124
$DogType = new ObjectType([
125125
'name' => 'Dog',
126126
'interfaces' => [$PetType],
127-
'isTypeOf' => static function () {
128-
return new Deferred(static function () {
127+
'isTypeOf' => static function () : Deferred {
128+
return new Deferred(static function () : void {
129129
throw new UserError('We are testing this error');
130130
});
131131
},
@@ -139,7 +139,7 @@ public function testIsTypeOfCanBeRejected() : void
139139
'name' => 'Cat',
140140
'interfaces' => [$PetType],
141141
'isTypeOf' => static function ($obj) {
142-
return new Deferred(static function () use ($obj) {
142+
return new Deferred(static function () use ($obj) : bool {
143143
return $obj instanceof Cat;
144144
});
145145
},
@@ -155,7 +155,7 @@ public function testIsTypeOfCanBeRejected() : void
155155
'fields' => [
156156
'pets' => [
157157
'type' => Type::listOf($PetType),
158-
'resolve' => static function () {
158+
'resolve' => static function () : array {
159159
return [
160160
new Dog('Odie', true),
161161
new Cat('Garfield', false),
@@ -210,7 +210,7 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForUnion() : void
210210
$dogType = new ObjectType([
211211
'name' => 'Dog',
212212
'isTypeOf' => static function ($obj) {
213-
return new Deferred(static function () use ($obj) {
213+
return new Deferred(static function () use ($obj) : bool {
214214
return $obj instanceof Dog;
215215
});
216216
},
@@ -223,7 +223,7 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForUnion() : void
223223
$catType = new ObjectType([
224224
'name' => 'Cat',
225225
'isTypeOf' => static function ($obj) {
226-
return new Deferred(static function () use ($obj) {
226+
return new Deferred(static function () use ($obj) : bool {
227227
return $obj instanceof Cat;
228228
});
229229
},
@@ -244,7 +244,7 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForUnion() : void
244244
'fields' => [
245245
'pets' => [
246246
'type' => Type::listOf($petType),
247-
'resolve' => static function () {
247+
'resolve' => static function () : array {
248248
return [new Dog('Odie', true), new Cat('Garfield', false)];
249249
},
250250
],
@@ -286,8 +286,8 @@ public function testResolveTypeOnInterfaceYieldsUsefulError() : void
286286
{
287287
$PetType = new InterfaceType([
288288
'name' => 'Pet',
289-
'resolveType' => static function ($obj) use (&$DogType, &$CatType, &$HumanType) {
290-
return new Deferred(static function () use ($obj, $DogType, $CatType, $HumanType) {
289+
'resolveType' => static function ($obj) use (&$DogType, &$CatType, &$HumanType) : Deferred {
290+
return new Deferred(static function () use ($obj, $DogType, $CatType, $HumanType) : ?Type {
291291
if ($obj instanceof Dog) {
292292
return $DogType;
293293
}
@@ -338,7 +338,7 @@ public function testResolveTypeOnInterfaceYieldsUsefulError() : void
338338
'pets' => [
339339
'type' => Type::listOf($PetType),
340340
'resolve' => static function () {
341-
return new Deferred(static function () {
341+
return new Deferred(static function () : array {
342342
return [
343343
new Dog('Odie', true),
344344
new Cat('Garfield', false),
@@ -417,7 +417,7 @@ public function testResolveTypeOnUnionYieldsUsefulError() : void
417417
$PetType = new UnionType([
418418
'name' => 'Pet',
419419
'resolveType' => static function ($obj) use ($DogType, $CatType, $HumanType) {
420-
return new Deferred(static function () use ($obj, $DogType, $CatType, $HumanType) {
420+
return new Deferred(static function () use ($obj, $DogType, $CatType, $HumanType) : ?Type {
421421
if ($obj instanceof Dog) {
422422
return $DogType;
423423
}
@@ -440,7 +440,7 @@ public function testResolveTypeOnUnionYieldsUsefulError() : void
440440
'fields' => [
441441
'pets' => [
442442
'type' => Type::listOf($PetType),
443-
'resolve' => static function () {
443+
'resolve' => static function () : array {
444444
return [
445445
new Dog('Odie', true),
446446
new Cat('Garfield', false),
@@ -535,7 +535,7 @@ public function testResolveTypeAllowsResolvingWithTypeName() : void
535535
'fields' => [
536536
'pets' => [
537537
'type' => Type::listOf($PetType),
538-
'resolve' => static function () {
538+
'resolve' => static function () : array {
539539
return [
540540
new Dog('Odie', true),
541541
new Cat('Garfield', false),
@@ -579,8 +579,8 @@ public function testResolveTypeCanBeCaught() : void
579579
{
580580
$PetType = new InterfaceType([
581581
'name' => 'Pet',
582-
'resolveType' => static function () {
583-
return new Deferred(static function () {
582+
'resolveType' => static function () : Deferred {
583+
return new Deferred(static function () : void {
584584
throw new UserError('We are testing this error');
585585
});
586586
},
@@ -613,7 +613,7 @@ public function testResolveTypeCanBeCaught() : void
613613
'fields' => [
614614
'pets' => [
615615
'type' => Type::listOf($PetType),
616-
'resolve' => static function () {
616+
'resolve' => static function () : array {
617617
return [
618618
new Dog('Odie', true),
619619
new Cat('Garfield', false),

tests/Executor/AbstractTest.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace GraphQL\Tests\Executor;
66

7+
use GraphQL\Error\InvariantViolation;
78
use GraphQL\Executor\ExecutionResult;
89
use GraphQL\Executor\Executor;
910
use GraphQL\GraphQL;
@@ -43,7 +44,7 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForInterface() : void
4344
$dogType = new ObjectType([
4445
'name' => 'Dog',
4546
'interfaces' => [$petType],
46-
'isTypeOf' => static function ($obj) {
47+
'isTypeOf' => static function ($obj) : bool {
4748
return $obj instanceof Dog;
4849
},
4950
'fields' => [
@@ -55,7 +56,7 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForInterface() : void
5556
$catType = new ObjectType([
5657
'name' => 'Cat',
5758
'interfaces' => [$petType],
58-
'isTypeOf' => static function ($obj) {
59+
'isTypeOf' => static function ($obj) : bool {
5960
return $obj instanceof Cat;
6061
},
6162
'fields' => [
@@ -70,7 +71,7 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForInterface() : void
7071
'fields' => [
7172
'pets' => [
7273
'type' => Type::listOf($petType),
73-
'resolve' => static function () {
74+
'resolve' => static function () : array {
7475
return [new Dog('Odie', true), new Cat('Garfield', false)];
7576
},
7677
],
@@ -109,7 +110,7 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForUnion() : void
109110
{
110111
$dogType = new ObjectType([
111112
'name' => 'Dog',
112-
'isTypeOf' => static function ($obj) {
113+
'isTypeOf' => static function ($obj) : bool {
113114
return $obj instanceof Dog;
114115
},
115116
'fields' => [
@@ -120,7 +121,7 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForUnion() : void
120121

121122
$catType = new ObjectType([
122123
'name' => 'Cat',
123-
'isTypeOf' => static function ($obj) {
124+
'isTypeOf' => static function ($obj) : bool {
124125
return $obj instanceof Cat;
125126
},
126127
'fields' => [
@@ -140,7 +141,7 @@ public function testIsTypeOfUsedToResolveRuntimeTypeForUnion() : void
140141
'fields' => [
141142
'pets' => [
142143
'type' => Type::listOf($petType),
143-
'resolve' => static function () {
144+
'resolve' => static function () : array {
144145
return [new Dog('Odie', true), new Cat('Garfield', false)];
145146
},
146147
],
@@ -230,7 +231,7 @@ public function testResolveTypeOnInterfaceYieldsUsefulError() : void
230231
'fields' => [
231232
'pets' => [
232233
'type' => Type::listOf($PetType),
233-
'resolve' => static function () {
234+
'resolve' => static function () : array {
234235
return [
235236
new Dog('Odie', true),
236237
new Cat('Garfield', false),
@@ -305,7 +306,7 @@ public function testResolveTypeOnUnionYieldsUsefulError() : void
305306

306307
$PetType = new UnionType([
307308
'name' => 'Pet',
308-
'resolveType' => static function ($obj) use ($DogType, $CatType, $HumanType) {
309+
'resolveType' => static function ($obj) use ($DogType, $CatType, $HumanType) : Type {
309310
if ($obj instanceof Dog) {
310311
return $DogType;
311312
}
@@ -315,6 +316,8 @@ public function testResolveTypeOnUnionYieldsUsefulError() : void
315316
if ($obj instanceof Human) {
316317
return $HumanType;
317318
}
319+
320+
throw new InvariantViolation('Invalid type');
318321
},
319322
'types' => [$DogType, $CatType],
320323
]);
@@ -325,7 +328,7 @@ public function testResolveTypeOnUnionYieldsUsefulError() : void
325328
'fields' => [
326329
'pets' => [
327330
'type' => Type::listOf($PetType),
328-
'resolve' => static function () {
331+
'resolve' => static function () : array {
329332
return [
330333
new Dog('Odie', true),
331334
new Cat('Garfield', false),
@@ -383,7 +386,7 @@ public function testReturningInvalidValueFromResolveTypeYieldsUsefulError() : vo
383386
$fooInterface = new InterfaceType([
384387
'name' => 'FooInterface',
385388
'fields' => ['bar' => ['type' => Type::string()]],
386-
'resolveType' => static function () {
389+
'resolveType' => static function () : array {
387390
return [];
388391
},
389392
]);
@@ -400,7 +403,7 @@ public function testReturningInvalidValueFromResolveTypeYieldsUsefulError() : vo
400403
'fields' => [
401404
'foo' => [
402405
'type' => $fooInterface,
403-
'resolve' => static function () {
406+
'resolve' => static function () : string {
404407
return 'dummy';
405408
},
406409
],
@@ -476,7 +479,7 @@ public function testResolveTypeAllowsResolvingWithTypeName() : void
476479
'fields' => [
477480
'pets' => [
478481
'type' => Type::listOf($PetType),
479-
'resolve' => static function () {
482+
'resolve' => static function () : array {
480483
return [
481484
new Dog('Odie', true),
482485
new Cat('Garfield', false),
@@ -523,7 +526,7 @@ public function testHintsOnConflictingTypeInstancesInResolveType() : void
523526
'fields' => [
524527
'a' => Type::string(),
525528
],
526-
'interfaces' => static function () use ($iface) {
529+
'interfaces' => static function () use ($iface) : array {
527530
return [$iface];
528531
},
529532
]);

tests/Executor/DeferredFieldsTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function setUp() : void
144144

145145
return Utils::filter(
146146
$this->storyDataSource,
147-
static function ($story) use ($category) {
147+
static function ($story) use ($category) : bool {
148148
return in_array($category['id'], $story['categoryIds'], true);
149149
}
150150
);
@@ -192,23 +192,23 @@ static function ($story) use ($category) {
192192

193193
return Utils::filter(
194194
$this->storyDataSource,
195-
static function ($story) {
195+
static function ($story) : bool {
196196
return $story['id'] % 2 === 1;
197197
}
198198
);
199199
},
200200
],
201201
'featuredCategory' => [
202202
'type' => $this->categoryType,
203-
'resolve' => function ($rootValue, $args, $context, ResolveInfo $info) {
203+
'resolve' => function ($rootValue, $args, $context, ResolveInfo $info) : array {
204204
$this->paths[] = $info->path;
205205

206206
return $this->categoryDataSource[0];
207207
},
208208
],
209209
'categories' => [
210210
'type' => Type::listOf($this->categoryType),
211-
'resolve' => function ($rootValue, $args, $context, ResolveInfo $info) {
211+
'resolve' => function ($rootValue, $args, $context, ResolveInfo $info) : array {
212212
$this->paths[] = $info->path;
213213

214214
return $this->categoryDataSource;
@@ -403,7 +403,7 @@ public function testComplexRecursiveDeferredFields() : void
403403
return [
404404
'sync' => [
405405
'type' => Type::string(),
406-
'resolve' => function ($complexType, $args, $context, ResolveInfo $info) {
406+
'resolve' => function ($complexType, $args, $context, ResolveInfo $info) : string {
407407
$this->paths[] = $info->path;
408408

409409
return 'sync';
@@ -414,7 +414,7 @@ public function testComplexRecursiveDeferredFields() : void
414414
'resolve' => function ($complexType, $args, $context, ResolveInfo $info) {
415415
$this->paths[] = $info->path;
416416

417-
return new Deferred(function () use ($info) {
417+
return new Deferred(function () use ($info) : string {
418418
$this->paths[] = ['!dfd for: ', $info->path];
419419

420420
return 'deferred';
@@ -423,7 +423,7 @@ public function testComplexRecursiveDeferredFields() : void
423423
],
424424
'nest' => [
425425
'type' => $complexType,
426-
'resolve' => function ($complexType, $args, $context, ResolveInfo $info) {
426+
'resolve' => function ($complexType, $args, $context, ResolveInfo $info) : array {
427427
$this->paths[] = $info->path;
428428

429429
return [];
@@ -434,7 +434,7 @@ public function testComplexRecursiveDeferredFields() : void
434434
'resolve' => function ($complexType, $args, $context, ResolveInfo $info) {
435435
$this->paths[] = $info->path;
436436

437-
return new Deferred(function () use ($info) {
437+
return new Deferred(function () use ($info) : array {
438438
$this->paths[] = ['!dfd nest for: ', $info->path];
439439

440440
return [];
@@ -635,7 +635,7 @@ private function findStoryById($id)
635635
{
636636
return Utils::find(
637637
$this->storyDataSource,
638-
static function ($story) use ($id) {
638+
static function ($story) use ($id) : bool {
639639
return $story['id'] === $id;
640640
}
641641
);
@@ -645,7 +645,7 @@ private function findUserById($id)
645645
{
646646
return Utils::find(
647647
$this->userDataSource,
648-
static function ($user) use ($id) {
648+
static function ($user) use ($id) : bool {
649649
return $user['id'] === $id;
650650
}
651651
);

0 commit comments

Comments
 (0)