Total Complexity | 46 |
Total Lines | 407 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ExtensionContext often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ExtensionContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class ExtensionContext implements ExtensionContextInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var ExtendInfo |
||
32 | */ |
||
33 | protected $info; |
||
34 | |||
35 | /** |
||
36 | * @var DefinitionBuilderInterface |
||
37 | */ |
||
38 | protected $definitionBuilder; |
||
39 | |||
40 | /** |
||
41 | * @var TypeInterface[] |
||
42 | */ |
||
43 | protected $extendTypeCache = []; |
||
44 | |||
45 | /** |
||
46 | * ExtensionContext constructor. |
||
47 | * @param ExtendInfo $info |
||
48 | */ |
||
49 | public function __construct(ExtendInfo $info) |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @return bool |
||
56 | */ |
||
57 | public function isSchemaExtended(): bool |
||
58 | { |
||
59 | return |
||
60 | $this->info->hasTypeExtensionsMap() || |
||
61 | $this->info->hasTypeDefinitionMap() || |
||
62 | $this->info->hasDirectiveDefinitions() || |
||
63 | $this->info->hasSchemaExtensions(); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @return ObjectType[] |
||
68 | * @throws ExtensionException |
||
69 | * @throws InvariantException |
||
70 | */ |
||
71 | public function getExtendedOperationTypes(): array |
||
72 | { |
||
73 | /** @noinspection PhpUnhandledExceptionInspection */ |
||
74 | $operationTypes = [ |
||
75 | 'query' => $this->getExtendedQueryType(), |
||
76 | 'mutation' => $this->getExtendedMutationType(), |
||
77 | 'subscription' => $this->getExtendedSubscriptionType(), |
||
78 | ]; |
||
79 | |||
80 | foreach ($this->info->getSchemaExtensions() as $schemaExtension) { |
||
81 | foreach ($schemaExtension->getOperationTypes() as $operationType) { |
||
82 | $operation = $operationType->getOperation(); |
||
83 | |||
84 | if (isset($operationTypes[$operation])) { |
||
85 | throw new ExtensionException(\sprintf('Must provide only one %s type in schema.', $operation)); |
||
86 | } |
||
87 | |||
88 | $operationTypes[$operation] = $this->definitionBuilder->buildType($operationType->getType()); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | return $operationTypes; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @return TypeInterface|null |
||
97 | * @throws InvariantException |
||
98 | */ |
||
99 | protected function getExtendedQueryType(): ?TypeInterface |
||
100 | { |
||
101 | $existingQueryType = $this->info->getSchema()->getQueryType(); |
||
102 | |||
103 | return null !== $existingQueryType |
||
104 | ? $this->getExtendedType($existingQueryType) |
||
105 | : null; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @return TypeInterface|null |
||
110 | * @throws InvariantException |
||
111 | */ |
||
112 | protected function getExtendedMutationType(): ?TypeInterface |
||
113 | { |
||
114 | $existingMutationType = $this->info->getSchema()->getMutationType(); |
||
115 | |||
116 | return null !== $existingMutationType |
||
117 | ? $this->getExtendedType($existingMutationType) |
||
118 | : null; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @return TypeInterface|null |
||
123 | * @throws InvariantException |
||
124 | */ |
||
125 | protected function getExtendedSubscriptionType(): ?TypeInterface |
||
126 | { |
||
127 | $existingSubscriptionType = $this->info->getSchema()->getSubscriptionType(); |
||
128 | |||
129 | return null !== $existingSubscriptionType |
||
130 | ? $this->getExtendedType($existingSubscriptionType) |
||
131 | : null; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @return TypeInterface[] |
||
136 | */ |
||
137 | public function getExtendedTypes(): array |
||
150 | ); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @return Directive[] |
||
155 | * @throws InvariantException |
||
156 | */ |
||
157 | public function getExtendedDirectives(): array |
||
170 | ); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param DefinitionBuilderInterface $definitionBuilder |
||
175 | * @return ExtensionContext |
||
176 | */ |
||
177 | public function setDefinitionBuilder(DefinitionBuilderInterface $definitionBuilder): ExtensionContext |
||
178 | { |
||
179 | $this->definitionBuilder = $definitionBuilder; |
||
180 | return $this; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param NamedTypeNode $node |
||
185 | * @return TypeInterface|null |
||
186 | * @throws ExtensionException |
||
187 | * @throws InvariantException |
||
188 | */ |
||
189 | public function resolveType(NamedTypeNode $node): ?TypeInterface |
||
190 | { |
||
191 | $typeName = $node->getNameValue(); |
||
192 | $existingType = $this->info->getSchema()->getType($typeName); |
||
193 | |||
194 | if ($existingType instanceof NamedTypeInterface) { |
||
195 | return $this->getExtendedType($existingType); |
||
196 | } |
||
197 | |||
198 | throw new ExtensionException( |
||
199 | \sprintf( |
||
200 | 'Unknown type: "%s". Ensure that this type exists ' . |
||
201 | 'either in the original schema, or is added in a type definition.', |
||
202 | $typeName |
||
203 | ), |
||
204 | [$node] |
||
205 | ); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @param NamedTypeInterface $type |
||
210 | * @return TypeInterface |
||
211 | * @throws InvariantException |
||
212 | */ |
||
213 | protected function getExtendedType(NamedTypeInterface $type): TypeInterface |
||
214 | { |
||
215 | $typeName = $type->getName(); |
||
216 | |||
217 | if (isset($this->extendTypeCache[$typeName])) { |
||
218 | return $this->extendTypeCache[$typeName]; |
||
219 | } |
||
220 | |||
221 | return $this->extendTypeCache[$typeName] = $this->extendType($type); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @param TypeInterface $type |
||
226 | * @return TypeInterface |
||
227 | * @throws InvariantException |
||
228 | */ |
||
229 | protected function extendType(TypeInterface $type): TypeInterface |
||
230 | { |
||
231 | /** @noinspection PhpParamsInspection */ |
||
232 | if (isIntrospectionType($type)) { |
||
233 | // Introspection types are not extended. |
||
234 | return $type; |
||
235 | } |
||
236 | |||
237 | if ($type instanceof ObjectType) { |
||
238 | return $this->extendObjectType($type); |
||
239 | } |
||
240 | |||
241 | if ($type instanceof InterfaceType) { |
||
242 | return $this->extendInterfaceType($type); |
||
243 | } |
||
244 | |||
245 | if ($type instanceof UnionType) { |
||
246 | return $this->extendUnionType($type); |
||
247 | } |
||
248 | |||
249 | // This type is not yet extendable. |
||
250 | return $type; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * @param ObjectType $type |
||
255 | * @return ObjectType |
||
256 | * @throws InvariantException |
||
257 | */ |
||
258 | protected function extendObjectType(ObjectType $type): ObjectType |
||
259 | { |
||
260 | $typeName = $type->getName(); |
||
261 | $extensionASTNodes = $type->getExtensionAstNodes(); |
||
262 | |||
263 | if ($this->info->hasTypeExtensions($typeName)) { |
||
264 | $extensionASTNodes = $this->extendExtensionASTNodes($typeName, $extensionASTNodes); |
||
265 | } |
||
266 | |||
267 | return newObjectType([ |
||
268 | 'name' => $typeName, |
||
269 | 'description' => $type->getDescription(), |
||
270 | 'interfaces' => function () use ($type) { |
||
271 | return $this->extendImplementedInterfaces($type); |
||
272 | }, |
||
273 | 'fields' => function () use ($type) { |
||
274 | return $this->extendFieldMap($type); |
||
275 | }, |
||
276 | 'astNode' => $type->getAstNode(), |
||
277 | 'extensionASTNodes' => $extensionASTNodes, |
||
278 | 'isTypeOf' => $type->getIsTypeOf(), |
||
279 | ]); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @param InterfaceType $type |
||
284 | * @return InterfaceType |
||
285 | * @throws InvariantException |
||
286 | */ |
||
287 | protected function extendInterfaceType(InterfaceType $type): InterfaceType |
||
288 | { |
||
289 | $typeName = $type->getName(); |
||
290 | $extensionASTNodes = $this->info->getTypeExtensions($typeName); |
||
291 | |||
292 | if ($this->info->hasTypeExtensions($typeName)) { |
||
293 | $extensionASTNodes = $this->extendExtensionASTNodes($typeName, $extensionASTNodes); |
||
294 | } |
||
295 | |||
296 | return newInterfaceType([ |
||
297 | 'name' => $typeName, |
||
298 | 'description' => $type->getDescription(), |
||
299 | 'fields' => function () use ($type) { |
||
300 | return $this->extendFieldMap($type); |
||
301 | }, |
||
302 | 'astNode' => $type->getAstNode(), |
||
303 | 'extensionASTNodes' => $extensionASTNodes, |
||
304 | 'resolveType' => $type->getResolveTypeCallback(), |
||
305 | ]); |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @param string $typeName |
||
310 | * @param array $nodes |
||
311 | * @return array |
||
312 | */ |
||
313 | protected function extendExtensionASTNodes(string $typeName, array $nodes): array |
||
314 | { |
||
315 | $typeExtensions = $this->info->getTypeExtensions($typeName); |
||
316 | return !empty($nodes) ? \array_merge($typeExtensions, $nodes) : $typeExtensions; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @param UnionType $type |
||
321 | * @return UnionType |
||
322 | * @throws InvariantException |
||
323 | */ |
||
324 | protected function extendUnionType(UnionType $type): UnionType |
||
325 | { |
||
326 | return newUnionType([ |
||
327 | 'name' => $type->getName(), |
||
328 | 'description' => $type->getDescription(), |
||
329 | 'types' => \array_map(function ($unionType) { |
||
330 | return $this->getExtendedType($unionType); |
||
331 | }, $type->getTypes()), |
||
332 | 'astNode' => $type->getAstNode(), |
||
333 | 'resolveType' => $type->getResolveTypeCallback(), |
||
334 | ]); |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @param ObjectType $type |
||
339 | * @return array |
||
340 | * @throws InvariantException |
||
341 | */ |
||
342 | protected function extendImplementedInterfaces(ObjectType $type): array |
||
343 | { |
||
344 | $typeName = $type->getName(); |
||
345 | |||
346 | $interfaces = \array_map(function (InterfaceType $interface) { |
||
347 | return $this->getExtendedType($interface); |
||
348 | }, $type->getInterfaces()); |
||
349 | |||
350 | // If there are any extensions to the interfaces, apply those here. |
||
351 | $extensions = $this->info->getTypeExtensions($typeName); |
||
352 | |||
353 | foreach ($extensions as $extension) { |
||
354 | foreach ($extension->getInterfaces() as $namedType) { |
||
355 | // Note: While this could make early assertions to get the correctly |
||
356 | // typed values, that would throw immediately while type system |
||
357 | // validation with validateSchema() will produce more actionable results. |
||
358 | $interfaces[] = $this->definitionBuilder->buildType($namedType); |
||
359 | } |
||
360 | } |
||
361 | |||
362 | return $interfaces; |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * @param FieldsAwareInterface $type |
||
367 | * @return array |
||
368 | * @throws InvalidTypeException |
||
369 | * @throws InvariantException |
||
370 | * @throws ExtensionException |
||
371 | * @throws InvalidArgumentException |
||
372 | */ |
||
373 | protected function extendFieldMap(FieldsAwareInterface $type): array |
||
415 | } |
||
416 | |||
417 | /** |
||
418 | * @param TypeInterface $typeDefinition |
||
419 | * @return TypeInterface |
||
420 | * @throws InvalidArgumentException |
||
421 | * @throws InvalidTypeException |
||
422 | * @throws InvariantException |
||
423 | */ |
||
424 | protected function extendFieldType(TypeInterface $typeDefinition): TypeInterface |
||
425 | { |
||
426 | if ($typeDefinition instanceof ListType) { |
||
427 | return newList($this->extendFieldType($typeDefinition->getOfType())); |
||
435 | } |
||
436 | } |
||
437 |