Skip to content

Commit 8a6f9b1

Browse files
authored
Add types to Type (#842)
1 parent 396ce47 commit 8a6f9b1

File tree

7 files changed

+61
-72
lines changed

7 files changed

+61
-72
lines changed

src/Type/Definition/CustomScalarType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function parseLiteral(Node $valueNode, ?array $variables = null)
5454
return AST::valueFromASTUntyped($valueNode, $variables);
5555
}
5656

57-
public function assertValid()
57+
public function assertValid(): void
5858
{
5959
parent::assertValid();
6060

src/Type/Definition/EnumType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public function parseLiteral(Node $valueNode, ?array $variables = null)
209209
/**
210210
* @throws InvariantViolation
211211
*/
212-
public function assertValid()
212+
public function assertValid(): void
213213
{
214214
parent::assertValid();
215215

src/Type/Definition/ListOfType.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,25 @@
1010

1111
class ListOfType extends Type implements WrappingType, OutputType, NullableType, InputType
1212
{
13-
/** @var callable():Type|Type */
13+
/** @var Type|callable():Type */
1414
public $ofType;
1515

1616
/**
17-
* @param callable():Type|Type $type
17+
* @param Type|callable():Type $type
1818
*/
1919
public function __construct($type)
2020
{
21-
$this->ofType = is_callable($type) ? $type : Type::assertType($type);
21+
$this->ofType = is_callable($type)
22+
? $type
23+
: Type::assertType($type);
2224
}
2325

2426
public function toString(): string
2527
{
2628
return '[' . $this->getOfType()->toString() . ']';
2729
}
2830

29-
public function getOfType()
31+
public function getOfType(): Type
3032
{
3133
return Schema::resolveType($this->ofType);
3234
}

src/Type/Definition/NonNull.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
class NonNull extends Type implements WrappingType, OutputType, InputType
1010
{
11-
/** @var callable():(NullableType&Type)|(NullableType&Type) */
11+
/** @var (NullableType&Type)|callable():(NullableType&Type) */
1212
private $ofType;
1313

1414
/**
1515
* code sniffer doesn't understand this syntax. Pr with a fix here: waiting on https://github.com/squizlabs/PHP_CodeSniffer/pull/2919
1616
* phpcs:disable Squiz.Commenting.FunctionComment.SpacingAfterParamType
17-
* @param callable():(NullableType&Type)|(NullableType&Type) $type
17+
* @param (NullableType&Type)|callable():(NullableType&Type) $type
1818
*/
1919
public function __construct($type)
2020
{
@@ -26,16 +26,20 @@ public function toString(): string
2626
return $this->getWrappedType()->toString() . '!';
2727
}
2828

29-
public function getOfType()
29+
/**
30+
* @return NullableType&Type
31+
*/
32+
public function getOfType(): Type
3033
{
31-
return Schema::resolveType($this->ofType);
34+
/** @var NullableType&Type $type */
35+
$type = Schema::resolveType($this->ofType);
36+
37+
return $type;
3238
}
3339

34-
/**
35-
* @return (NullableType&Type)
36-
*/
3740
public function getWrappedType(bool $recurse = false): Type
3841
{
42+
/** @var NullableType&Type $type */
3943
$type = $this->getOfType();
4044

4145
return $recurse && $type instanceof WrappingType

src/Type/Definition/Type.php

Lines changed: 35 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
use function preg_replace;
2222

2323
/**
24-
* Registry of standard GraphQL types
25-
* and a base class for all other types.
24+
* Registry of standard GraphQL types and base class for all other types.
2625
*/
2726
abstract class Type implements JsonSerializable
2827
{
@@ -35,7 +34,7 @@ abstract class Type implements JsonSerializable
3534
/** @var array<string, ScalarType> */
3635
protected static $standardTypes;
3736

38-
/** @var Type[] */
37+
/** @var array<string, Type> */
3938
private static $builtInTypes;
4039

4140
/** @var string */
@@ -47,10 +46,10 @@ abstract class Type implements JsonSerializable
4746
/** @var (Node&TypeDefinitionNode)|null */
4847
public $astNode;
4948

50-
/** @var mixed[] */
49+
/** @var array<string, mixed> */
5150
public $config;
5251

53-
/** @var TypeExtensionNode[] */
52+
/** @var array<Node&TypeExtensionNode> */
5453
public $extensionASTNodes;
5554

5655
/**
@@ -114,38 +113,45 @@ public static function float(): ScalarType
114113
}
115114

116115
/**
116+
* @param Type|callable():Type $type
117+
*
117118
* @api
118119
*/
119-
public static function listOf(Type $wrappedType): ListOfType
120+
public static function listOf($type): ListOfType
120121
{
121-
return new ListOfType($wrappedType);
122+
return new ListOfType($type);
122123
}
123124

124125
/**
125-
* @param callable|NullableType $wrappedType
126+
* code sniffer doesn't understand this syntax. Pr with a fix here: waiting on https://github.com/squizlabs/PHP_CodeSniffer/pull/2919
127+
* phpcs:disable Squiz.Commenting.FunctionComment.SpacingAfterParamType
128+
* @param (NullableType&Type)|callable():(NullableType&Type) $type
126129
*
127130
* @api
128131
*/
129-
public static function nonNull($wrappedType): NonNull
132+
public static function nonNull($type): NonNull
130133
{
131-
return new NonNull($wrappedType);
134+
return new NonNull($type);
132135
}
133136

134137
/**
135-
* Checks if the type is a builtin type
138+
* Checks if the type is a builtin type.
136139
*/
137140
public static function isBuiltInType(Type $type): bool
138141
{
139-
return in_array($type->name, array_keys(self::getAllBuiltInTypes()), true);
142+
return in_array(
143+
$type->name,
144+
array_keys(self::getAllBuiltInTypes()),
145+
true
146+
);
140147
}
141148

142149
/**
143-
* Returns all builtin in types including base scalar and
144-
* introspection types
150+
* Returns all builtin in types including base scalar and introspection types.
145151
*
146-
* @return Type[]
152+
* @return array<string, Type>
147153
*/
148-
public static function getAllBuiltInTypes()
154+
public static function getAllBuiltInTypes(): array
149155
{
150156
if (self::$builtInTypes === null) {
151157
self::$builtInTypes = array_merge(
@@ -158,11 +164,11 @@ public static function getAllBuiltInTypes()
158164
}
159165

160166
/**
161-
* Returns all builtin scalar types
167+
* Returns all builtin scalar types.
162168
*
163-
* @return ScalarType[]
169+
* @return array<string, ScalarType>
164170
*/
165-
public static function getStandardTypes()
171+
public static function getStandardTypes(): array
166172
{
167173
return [
168174
self::ID => static::id(),
@@ -176,7 +182,7 @@ public static function getStandardTypes()
176182
/**
177183
* @param array<string, ScalarType> $types
178184
*/
179-
public static function overrideStandardTypes(array $types)
185+
public static function overrideStandardTypes(array $types): void
180186
{
181187
$standardTypes = self::getStandardTypes();
182188
foreach ($types as $type) {
@@ -197,8 +203,6 @@ public static function overrideStandardTypes(array $types)
197203
}
198204

199205
/**
200-
* @param Type $type
201-
*
202206
* @api
203207
*/
204208
public static function isInputType($type): bool
@@ -207,16 +211,10 @@ public static function isInputType($type): bool
207211
}
208212

209213
/**
210-
* @param Type $type
211-
*
212214
* @api
213215
*/
214216
public static function getNamedType($type): ?Type
215217
{
216-
if ($type === null) {
217-
return null;
218-
}
219-
220218
while ($type instanceof WrappingType) {
221219
$type = $type->getWrappedType();
222220
}
@@ -225,8 +223,6 @@ public static function getNamedType($type): ?Type
225223
}
226224

227225
/**
228-
* @param Type $type
229-
*
230226
* @api
231227
*/
232228
public static function isOutputType($type): bool
@@ -235,8 +231,6 @@ public static function isOutputType($type): bool
235231
}
236232

237233
/**
238-
* @param Type $type
239-
*
240234
* @api
241235
*/
242236
public static function isLeafType($type): bool
@@ -245,8 +239,6 @@ public static function isLeafType($type): bool
245239
}
246240

247241
/**
248-
* @param Type $type
249-
*
250242
* @api
251243
*/
252244
public static function isCompositeType($type): bool
@@ -255,21 +247,19 @@ public static function isCompositeType($type): bool
255247
}
256248

257249
/**
258-
* @param Type $type
259-
*
260250
* @api
261251
*/
262252
public static function isAbstractType($type): bool
263253
{
264254
return $type instanceof AbstractType;
265255
}
266256

267-
/**
268-
* @param mixed $type
269-
*/
270257
public static function assertType($type): Type
271258
{
272-
assert($type instanceof Type, new InvariantViolation('Expected ' . Utils::printSafe($type) . ' to be a GraphQL type.'));
259+
assert(
260+
$type instanceof Type,
261+
new InvariantViolation('Expected ' . Utils::printSafe($type) . ' to be a GraphQL type.')
262+
);
273263

274264
return $type;
275265
}
@@ -287,39 +277,27 @@ public static function getNullableType(Type $type): Type
287277
/**
288278
* @throws InvariantViolation
289279
*/
290-
public function assertValid()
280+
public function assertValid(): void
291281
{
292282
Utils::assertValidName($this->name);
293283
}
294284

295-
/**
296-
* @return string
297-
*/
298-
public function jsonSerialize()
285+
public function jsonSerialize(): string
299286
{
300287
return $this->toString();
301288
}
302289

303-
/**
304-
* @return string
305-
*/
306-
public function toString()
290+
public function toString(): string
307291
{
308292
return $this->name;
309293
}
310294

311-
/**
312-
* @return string
313-
*/
314-
public function __toString()
295+
public function __toString(): string
315296
{
316297
return $this->toString();
317298
}
318299

319-
/**
320-
* @return string|null
321-
*/
322-
protected function tryInferName()
300+
protected function tryInferName(): ?string
323301
{
324302
if ($this->name) {
325303
return $this->name;

src/Type/Schema.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,16 @@ private function defaultTypeLoader(string $typeName): ?Type
410410
}
411411

412412
/**
413-
* @param Type|callable():Type $type
413+
* @param Type|callable $type
414+
*
415+
* @template T of Type
416+
* @phpstan-param T|callable():T $type
417+
* @phpstan-return T
414418
*/
415419
public static function resolveType($type): Type
416420
{
417421
if ($type instanceof Type) {
422+
/** @phpstan-var T $type */
418423
return $type;
419424
}
420425

tests/Executor/ExecutorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testExecutesArbitraryCode(): void
5454
});
5555
};
5656

57-
$data = [
57+
$data = [
5858
'a' => static function (): string {
5959
return 'Apple';
6060
},

0 commit comments

Comments
 (0)