Skip to content

Commit ab3576f

Browse files
committed
Avoid unnecessary @throws annotations for known correct configuration
1 parent 66b0dda commit ab3576f

3 files changed

Lines changed: 45 additions & 53 deletions

File tree

src/Type/Definition/Directive.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace GraphQL\Type\Definition;
44

5-
use GraphQL\Error\InvariantViolation;
65
use GraphQL\Language\AST\DirectiveDefinitionNode;
76
use GraphQL\Language\DirectiveLocation;
87

@@ -75,11 +74,7 @@ public function __construct(array $config)
7574
$this->config = $config;
7675
}
7776

78-
/**
79-
* @throws InvariantViolation
80-
*
81-
* @return array<string, Directive>
82-
*/
77+
/** @return array<string, Directive> */
8378
public static function getInternalDirectives(): array
8479
{
8580
return [
@@ -89,7 +84,6 @@ public static function getInternalDirectives(): array
8984
];
9085
}
9186

92-
/** @throws InvariantViolation */
9387
public static function includeDirective(): Directive
9488
{
9589
return self::$internalDirectives[self::INCLUDE_NAME] ??= new self([
@@ -109,7 +103,6 @@ public static function includeDirective(): Directive
109103
]);
110104
}
111105

112-
/** @throws InvariantViolation */
113106
public static function skipDirective(): Directive
114107
{
115108
return self::$internalDirectives[self::SKIP_NAME] ??= new self([
@@ -129,7 +122,6 @@ public static function skipDirective(): Directive
129122
]);
130123
}
131124

132-
/** @throws InvariantViolation */
133125
public static function deprecatedDirective(): Directive
134126
{
135127
return self::$internalDirectives[self::DEPRECATED_NAME] ??= new self([
@@ -151,7 +143,6 @@ public static function deprecatedDirective(): Directive
151143
]);
152144
}
153145

154-
/** @throws InvariantViolation */
155146
public static function isSpecifiedDirective(Directive $directive): bool
156147
{
157148
return array_key_exists($directive->name, self::getInternalDirectives());

src/Type/Definition/Type.php

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,56 +37,58 @@ abstract class Type implements \JsonSerializable
3737
protected static ?array $builtInTypes;
3838

3939
/**
40-
* @api
40+
* Returns the registered or default standard Int type.
4141
*
42-
* @throws InvariantViolation
42+
* @api
4343
*/
4444
public static function int(): ScalarType
4545
{
46-
return static::$standardTypes[self::INT] ??= new IntType();
46+
return static::$standardTypes[self::INT] ??= new IntType(); // @phpstan-ignore missingType.checkedException (static configuration is known to be correct)
4747
}
4848

4949
/**
50-
* @api
50+
* Returns the registered or default standard Float type.
5151
*
52-
* @throws InvariantViolation
52+
* @api
5353
*/
5454
public static function float(): ScalarType
5555
{
56-
return static::$standardTypes[self::FLOAT] ??= new FloatType();
56+
return static::$standardTypes[self::FLOAT] ??= new FloatType(); // @phpstan-ignore missingType.checkedException (static configuration is known to be correct)
5757
}
5858

5959
/**
60-
* @api
60+
* Returns the registered or default standard String type.
6161
*
62-
* @throws InvariantViolation
62+
* @api
6363
*/
6464
public static function string(): ScalarType
6565
{
66-
return static::$standardTypes[self::STRING] ??= new StringType();
66+
return static::$standardTypes[self::STRING] ??= new StringType(); // @phpstan-ignore missingType.checkedException (static configuration is known to be correct)
6767
}
6868

6969
/**
70-
* @api
70+
* Returns the registered or default standard Boolean type.
7171
*
72-
* @throws InvariantViolation
72+
* @api
7373
*/
7474
public static function boolean(): ScalarType
7575
{
76-
return static::$standardTypes[self::BOOLEAN] ??= new BooleanType();
76+
return static::$standardTypes[self::BOOLEAN] ??= new BooleanType(); // @phpstan-ignore missingType.checkedException (static configuration is known to be correct)
7777
}
7878

7979
/**
80-
* @api
80+
* Returns the registered or default standard ID type.
8181
*
82-
* @throws InvariantViolation
82+
* @api
8383
*/
8484
public static function id(): ScalarType
8585
{
86-
return static::$standardTypes[self::ID] ??= new IDType();
86+
return static::$standardTypes[self::ID] ??= new IDType(); // @phpstan-ignore missingType.checkedException (static configuration is known to be correct)
8787
}
8888

8989
/**
90+
* Wraps the given type in a list type.
91+
*
9092
* @template T of Type
9193
*
9294
* @param T|callable():T $type
@@ -101,6 +103,8 @@ public static function listOf($type): ListOfType
101103
}
102104

103105
/**
106+
* Wraps the given type in a non-null type.
107+
*
104108
* @param (NullableType&Type)|callable():(NullableType&Type) $type
105109
*
106110
* @api
@@ -113,8 +117,6 @@ public static function nonNull($type): NonNull
113117
/**
114118
* Returns all builtin in types including base scalar and introspection types.
115119
*
116-
* @throws InvariantViolation
117-
*
118120
* @return array<string, Type&NamedType>
119121
*/
120122
public static function builtInTypes(): array
@@ -128,8 +130,6 @@ public static function builtInTypes(): array
128130
/**
129131
* Returns all builtin scalar types.
130132
*
131-
* @throws InvariantViolation
132-
*
133133
* @return array<string, ScalarType>
134134
*/
135135
public static function getStandardTypes(): array
@@ -144,6 +144,8 @@ public static function getStandardTypes(): array
144144
}
145145

146146
/**
147+
* Allows partially or completely overriding the standard types.
148+
*
147149
* @param array<ScalarType> $types
148150
*
149151
* @throws InvariantViolation
@@ -174,6 +176,8 @@ public static function overrideStandardTypes(array $types): void
174176
}
175177

176178
/**
179+
* Determines if the given type is an input type.
180+
*
177181
* @param mixed $type
178182
*
179183
* @api
@@ -184,6 +188,8 @@ public static function isInputType($type): bool
184188
}
185189

186190
/**
191+
* Returns the underlying named type of the given type.
192+
*
187193
* @return (Type&NamedType)|null
188194
*
189195
* @api
@@ -200,6 +206,8 @@ public static function getNamedType(?Type $type): ?Type
200206
}
201207

202208
/**
209+
* Determines if the given type is an output type.
210+
*
203211
* @param mixed $type
204212
*
205213
* @api
@@ -210,6 +218,8 @@ public static function isOutputType($type): bool
210218
}
211219

212220
/**
221+
* Determines if the given type is a leaf type.
222+
*
213223
* @param mixed $type
214224
*
215225
* @api
@@ -220,6 +230,8 @@ public static function isLeafType($type): bool
220230
}
221231

222232
/**
233+
* Determines if the given type is a composite type.
234+
*
223235
* @param mixed $type
224236
*
225237
* @api
@@ -230,6 +242,8 @@ public static function isCompositeType($type): bool
230242
}
231243

232244
/**
245+
* Determines if the given type is an abstract type.
246+
*
233247
* @param mixed $type
234248
*
235249
* @api
@@ -240,6 +254,8 @@ public static function isAbstractType($type): bool
240254
}
241255

242256
/**
257+
* Unwraps a potentially non-null type to return the underlying nullable type.
258+
*
243259
* @return Type&NullableType
244260
*
245261
* @api

src/Type/Introspection.php

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,7 @@ public static function isIntrospectionType(NamedType $type): bool
231231
return in_array($type->name, self::TYPE_NAMES, true);
232232
}
233233

234-
/**
235-
* @throws InvariantViolation
236-
*
237-
* @return array<string, Type&NamedType>
238-
*/
234+
/** @return array<string, Type&NamedType> */
239235
public static function getTypes(): array
240236
{
241237
return [
@@ -250,10 +246,9 @@ public static function getTypes(): array
250246
];
251247
}
252248

253-
/** @throws InvariantViolation */
254249
public static function _schema(): ObjectType
255250
{
256-
return self::$cachedInstances[self::SCHEMA_OBJECT_NAME] ??= new ObjectType([
251+
return self::$cachedInstances[self::SCHEMA_OBJECT_NAME] ??= new ObjectType([ // @phpstan-ignore missingType.checkedException (static configuration is known to be correct)
257252
'name' => self::SCHEMA_OBJECT_NAME,
258253
'isIntrospection' => true,
259254
'description' => 'A GraphQL Schema defines the capabilities of a GraphQL '
@@ -290,10 +285,9 @@ public static function _schema(): ObjectType
290285
]);
291286
}
292287

293-
/** @throws InvariantViolation */
294288
public static function _type(): ObjectType
295289
{
296-
return self::$cachedInstances[self::TYPE_OBJECT_NAME] ??= new ObjectType([
290+
return self::$cachedInstances[self::TYPE_OBJECT_NAME] ??= new ObjectType([ // @phpstan-ignore missingType.checkedException (static configuration is known to be correct)
297291
'name' => self::TYPE_OBJECT_NAME,
298292
'isIntrospection' => true,
299293
'description' => 'The fundamental unit of any GraphQL Schema is the type. There are '
@@ -441,10 +435,9 @@ public static function _type(): ObjectType
441435
]);
442436
}
443437

444-
/** @throws InvariantViolation */
445438
public static function _typeKind(): EnumType
446439
{
447-
return self::$cachedInstances[self::TYPE_KIND_ENUM_NAME] ??= new EnumType([
440+
return self::$cachedInstances[self::TYPE_KIND_ENUM_NAME] ??= new EnumType([ // @phpstan-ignore missingType.checkedException (static configuration is known to be correct)
448441
'name' => self::TYPE_KIND_ENUM_NAME,
449442
'isIntrospection' => true,
450443
'description' => 'An enum describing what kind of type a given `__Type` is.',
@@ -485,10 +478,9 @@ public static function _typeKind(): EnumType
485478
]);
486479
}
487480

488-
/** @throws InvariantViolation */
489481
public static function _field(): ObjectType
490482
{
491-
return self::$cachedInstances[self::FIELD_OBJECT_NAME] ??= new ObjectType([
483+
return self::$cachedInstances[self::FIELD_OBJECT_NAME] ??= new ObjectType([ // @phpstan-ignore missingType.checkedException (static configuration is known to be correct)
492484
'name' => self::FIELD_OBJECT_NAME,
493485
'isIntrospection' => true,
494486
'description' => 'Object and Interface types are described by a list of Fields, each of '
@@ -539,10 +531,9 @@ public static function _field(): ObjectType
539531
]);
540532
}
541533

542-
/** @throws InvariantViolation */
543534
public static function _inputValue(): ObjectType
544535
{
545-
return self::$cachedInstances[self::INPUT_VALUE_OBJECT_NAME] ??= new ObjectType([
536+
return self::$cachedInstances[self::INPUT_VALUE_OBJECT_NAME] ??= new ObjectType([ // @phpstan-ignore missingType.checkedException (static configuration is known to be correct)
546537
'name' => self::INPUT_VALUE_OBJECT_NAME,
547538
'isIntrospection' => true,
548539
'description' => 'Arguments provided to Fields or Directives and the input fields of an '
@@ -597,10 +588,9 @@ public static function _inputValue(): ObjectType
597588
]);
598589
}
599590

600-
/** @throws InvariantViolation */
601591
public static function _enumValue(): ObjectType
602592
{
603-
return self::$cachedInstances[self::ENUM_VALUE_OBJECT_NAME] ??= new ObjectType([
593+
return self::$cachedInstances[self::ENUM_VALUE_OBJECT_NAME] ??= new ObjectType([ // @phpstan-ignore missingType.checkedException (static configuration is known to be correct)
604594
'name' => self::ENUM_VALUE_OBJECT_NAME,
605595
'isIntrospection' => true,
606596
'description' => 'One possible value for a given Enum. Enum values are unique values, not '
@@ -627,10 +617,9 @@ public static function _enumValue(): ObjectType
627617
]);
628618
}
629619

630-
/** @throws InvariantViolation */
631620
public static function _directive(): ObjectType
632621
{
633-
return self::$cachedInstances[self::DIRECTIVE_OBJECT_NAME] ??= new ObjectType([
622+
return self::$cachedInstances[self::DIRECTIVE_OBJECT_NAME] ??= new ObjectType([ // @phpstan-ignore missingType.checkedException (static configuration is known to be correct)
634623
'name' => self::DIRECTIVE_OBJECT_NAME,
635624
'isIntrospection' => true,
636625
'description' => 'A Directive provides a way to describe alternate runtime execution and '
@@ -666,10 +655,9 @@ public static function _directive(): ObjectType
666655
]);
667656
}
668657

669-
/** @throws InvariantViolation */
670658
public static function _directiveLocation(): EnumType
671659
{
672-
return self::$cachedInstances[self::DIRECTIVE_LOCATION_ENUM_NAME] ??= new EnumType([
660+
return self::$cachedInstances[self::DIRECTIVE_LOCATION_ENUM_NAME] ??= new EnumType([ // @phpstan-ignore missingType.checkedException (static configuration is known to be correct)
673661
'name' => self::DIRECTIVE_LOCATION_ENUM_NAME,
674662
'isIntrospection' => true,
675663
'description' => 'A Directive can be adjacent to many parts of the GraphQL language, a '
@@ -755,7 +743,6 @@ public static function _directiveLocation(): EnumType
755743
]);
756744
}
757745

758-
/** @throws InvariantViolation */
759746
public static function schemaMetaFieldDef(): FieldDefinition
760747
{
761748
return self::$cachedInstances[self::SCHEMA_FIELD_NAME] ??= new FieldDefinition([
@@ -767,7 +754,6 @@ public static function schemaMetaFieldDef(): FieldDefinition
767754
]);
768755
}
769756

770-
/** @throws InvariantViolation */
771757
public static function typeMetaFieldDef(): FieldDefinition
772758
{
773759
return self::$cachedInstances[self::TYPE_FIELD_NAME] ??= new FieldDefinition([
@@ -784,7 +770,6 @@ public static function typeMetaFieldDef(): FieldDefinition
784770
]);
785771
}
786772

787-
/** @throws InvariantViolation */
788773
public static function typeNameMetaFieldDef(): FieldDefinition
789774
{
790775
return self::$cachedInstances[self::TYPE_NAME_FIELD_NAME] ??= new FieldDefinition([

0 commit comments

Comments
 (0)