Total Complexity | 60 |
Total Lines | 516 |
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 |
||
42 | class ExtensionContext implements ExtensionContextInterface |
||
43 | { |
||
44 | /** |
||
45 | * @var SchemaInterface |
||
46 | */ |
||
47 | protected $schema; |
||
48 | |||
49 | /** |
||
50 | * @var DocumentNode |
||
51 | */ |
||
52 | protected $document; |
||
53 | |||
54 | /** |
||
55 | * @var DefinitionBuilderCreatorInterface |
||
56 | */ |
||
57 | protected $definitionBuilderCreator; |
||
58 | |||
59 | /** |
||
60 | * @var DefinitionBuilderInterface |
||
61 | */ |
||
62 | protected $definitionBuilder; |
||
63 | |||
64 | /** |
||
65 | * @var TypeDefinitionNodeInterface[] |
||
66 | */ |
||
67 | protected $typeDefinitionMap; |
||
68 | |||
69 | /** |
||
70 | * @var ObjectTypeExtensionNode[][]|InterfaceTypeExtensionNode[][] |
||
71 | */ |
||
72 | protected $typeExtensionsMap; |
||
73 | |||
74 | /** |
||
75 | * @var DirectiveDefinitionNode[] |
||
76 | */ |
||
77 | protected $directiveDefinitions; |
||
78 | |||
79 | /** |
||
80 | * @var NamedTypeInterface[] |
||
81 | */ |
||
82 | protected $extendTypeCache; |
||
83 | |||
84 | /** |
||
85 | * ExtensionContext constructor. |
||
86 | * @param SchemaInterface $schema |
||
87 | * @param DocumentNode $document |
||
88 | * @param DefinitionBuilderCreatorInterface $definitionBuilderCreator |
||
89 | * @throws ExtensionException |
||
90 | */ |
||
91 | public function __construct( |
||
92 | SchemaInterface $schema, |
||
93 | DocumentNode $document, |
||
94 | DefinitionBuilderCreatorInterface $definitionBuilderCreator |
||
95 | ) { |
||
96 | $this->schema = $schema; |
||
97 | $this->document = $document; |
||
98 | $this->definitionBuilderCreator = $definitionBuilderCreator; |
||
99 | $this->typeExtensionsMap = []; |
||
100 | $this->typeDefinitionMap = []; |
||
101 | $this->directiveDefinitions = []; |
||
102 | $this->extendTypeCache = []; |
||
103 | |||
104 | $typeDefinitionMap = $this->getExtendedDefinitions(); |
||
105 | |||
106 | $this->definitionBuilder = $this->createDefinitionBuilder($typeDefinitionMap); |
||
107 | |||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @throws ExtensionException |
||
112 | */ |
||
113 | public function getExtendedDefinitions(): array |
||
114 | { |
||
115 | foreach ($this->document->getDefinitions() as $definition) { |
||
116 | if ($definition instanceof TypeDefinitionNodeInterface) { |
||
117 | // Sanity check that none of the defined types conflict with the schema's existing types. |
||
118 | $typeName = $definition->getNameValue(); |
||
119 | $existingType = $this->schema->getType($typeName); |
||
120 | |||
121 | if (null !== $existingType) { |
||
122 | throw new ExtensionException( |
||
123 | \sprintf( |
||
124 | 'Type "%s" already exists in the schema. It cannot also ' . |
||
125 | 'be defined in this type definition.', |
||
126 | $typeName |
||
127 | ), |
||
128 | [$definition] |
||
129 | ); |
||
130 | } |
||
131 | |||
132 | $this->typeDefinitionMap[$typeName] = $definition; |
||
133 | |||
134 | continue; |
||
135 | } |
||
136 | |||
137 | if ($definition instanceof ObjectTypeExtensionNode || $definition instanceof InterfaceTypeExtensionNode) { |
||
138 | // Sanity check that this type extension exists within the schema's existing types. |
||
139 | $extendedTypeName = $definition->getNameValue(); |
||
140 | $existingType = $this->schema->getType($extendedTypeName); |
||
141 | |||
142 | if (null === $existingType) { |
||
143 | throw new ExtensionException( |
||
144 | \sprintf( |
||
145 | 'Cannot extend type "%s" because it does not exist in the existing schema.', |
||
146 | $extendedTypeName |
||
147 | ), |
||
148 | [$definition] |
||
149 | ); |
||
150 | } |
||
151 | |||
152 | $this->checkExtensionNode($existingType, $definition); |
||
153 | |||
154 | $existingTypeExtensions = $this->typeExtensionsMap[$extendedTypeName] ?? []; |
||
155 | |||
156 | $this->typeExtensionsMap[$extendedTypeName] = \array_merge($existingTypeExtensions, [$definition]); |
||
157 | |||
158 | continue; |
||
159 | } |
||
160 | |||
161 | if ($definition instanceof DirectiveDefinitionNode) { |
||
162 | $directiveName = $definition->getNameValue(); |
||
163 | $existingDirective = $this->schema->getDirective($directiveName); |
||
164 | |||
165 | if (null !== $existingDirective) { |
||
166 | throw new ExtensionException( |
||
167 | \sprintf( |
||
168 | 'Directive "%s" already exists in the schema. It cannot be redefined.', |
||
169 | $directiveName |
||
170 | ), |
||
171 | [$definition] |
||
172 | ); |
||
173 | } |
||
174 | |||
175 | $this->directiveDefinitions[] = $definition; |
||
176 | |||
177 | continue; |
||
178 | } |
||
179 | |||
180 | if ($definition instanceof ScalarTypeExtensionNode || |
||
181 | $definition instanceof UnionTypeExtensionNode || |
||
182 | $definition instanceof EnumTypeExtensionNode || |
||
183 | $definition instanceof InputObjectTypeExtensionNode) { |
||
184 | throw new ExtensionException( |
||
185 | \sprintf('The %s kind is not yet supported by extendSchema().', $definition->getKind()) |
||
186 | ); |
||
187 | } |
||
188 | } |
||
189 | |||
190 | return $this->typeDefinitionMap; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @return bool |
||
195 | */ |
||
196 | public function isSchemaExtended(): bool |
||
197 | { |
||
198 | return |
||
199 | !empty(\array_keys($this->typeExtensionsMap)) || |
||
200 | !empty(\array_keys($this->typeDefinitionMap)) || |
||
201 | !empty($this->directiveDefinitions); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @return TypeInterface|null |
||
206 | * @throws InvalidArgumentException |
||
207 | * @throws InvariantException |
||
208 | */ |
||
209 | public function getExtendedQueryType(): ?TypeInterface |
||
210 | { |
||
211 | $existingQueryType = $this->schema->getQueryType(); |
||
212 | |||
213 | return null !== $existingQueryType |
||
214 | ? $this->getExtendedType($existingQueryType) |
||
215 | : null; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @return TypeInterface|null |
||
220 | * @throws InvalidArgumentException |
||
221 | * @throws InvariantException |
||
222 | */ |
||
223 | public function getExtendedMutationType(): ?TypeInterface |
||
224 | { |
||
225 | $existingMutationType = $this->schema->getMutationType(); |
||
226 | |||
227 | return null !== $existingMutationType |
||
228 | ? $this->getExtendedType($existingMutationType) |
||
229 | : null; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @return TypeInterface|null |
||
234 | * @throws InvalidArgumentException |
||
235 | * @throws InvariantException |
||
236 | */ |
||
237 | public function getExtendedSubscriptionType(): ?TypeInterface |
||
238 | { |
||
239 | $existingSubscriptionType = $this->schema->getSubscriptionType(); |
||
240 | |||
241 | return null !== $existingSubscriptionType |
||
242 | ? $this->getExtendedType($existingSubscriptionType) |
||
243 | : null; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @return TypeInterface[] |
||
248 | */ |
||
249 | public function getExtendedTypes(): array |
||
250 | { |
||
251 | return \array_merge( |
||
252 | \array_map(function ($type) { |
||
253 | return $this->getExtendedType($type); |
||
254 | }, \array_values($this->schema->getTypeMap())), |
||
255 | $this->definitionBuilder->buildTypes(\array_values($this->typeDefinitionMap)) |
||
256 | ); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @return Directive[] |
||
261 | * @throws InvariantException |
||
262 | */ |
||
263 | public function getExtendedDirectives(): array |
||
264 | { |
||
265 | $existingDirectives = $this->schema->getDirectives(); |
||
266 | |||
267 | invariant(!empty($existingDirectives), 'schema must have default directives'); |
||
268 | |||
269 | return \array_merge( |
||
270 | $existingDirectives, |
||
271 | \array_map(function (DirectiveDefinitionNode $node) { |
||
272 | return $this->definitionBuilder->buildDirective($node); |
||
273 | }, $this->directiveDefinitions) |
||
274 | ); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @param NamedTypeNode $node |
||
279 | * @return TypeInterface|null |
||
280 | * @throws ExtensionException |
||
281 | * @throws InvalidArgumentException |
||
282 | * @throws InvariantException |
||
283 | */ |
||
284 | public function resolveType(NamedTypeNode $node): ?TypeInterface |
||
285 | { |
||
286 | $typeName = $node->getNameValue(); |
||
287 | $existingType = $this->schema->getType($typeName); |
||
288 | |||
289 | if (null !== $existingType) { |
||
290 | return $this->getExtendedType($existingType); |
||
291 | } |
||
292 | |||
293 | throw new ExtensionException( |
||
294 | \sprintf( |
||
295 | 'Unknown type: "%s". Ensure that this type exists ' . |
||
296 | 'either in the original schema, or is added in a type definition.', |
||
297 | $typeName |
||
298 | ), |
||
299 | [$node] |
||
300 | ); |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @param array $typeDefinitionMap |
||
305 | * @return DefinitionBuilderInterface |
||
306 | */ |
||
307 | protected function createDefinitionBuilder(array $typeDefinitionMap): DefinitionBuilderInterface |
||
308 | { |
||
309 | return $this->definitionBuilderCreator->create($typeDefinitionMap, [$this, 'resolveType']); |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * @param TypeInterface $type |
||
314 | * @param NodeInterface $node |
||
315 | * @throws ExtensionException |
||
316 | */ |
||
317 | protected function checkExtensionNode(TypeInterface $type, NodeInterface $node): void |
||
318 | { |
||
319 | if ($node instanceof ObjectTypeExtensionNode && !($type instanceof ObjectType)) { |
||
320 | throw new ExtensionException( |
||
321 | \sprintf('Cannot extend non-object type "%s".', toString($type)), |
||
322 | [$node] |
||
323 | ); |
||
324 | } |
||
325 | |||
326 | if ($node instanceof InterfaceTypeExtensionNode && !($type instanceof InterfaceType)) { |
||
327 | throw new ExtensionException( |
||
328 | \sprintf('Cannot extend non-interface type "%s".', toString($type)), |
||
329 | [$node] |
||
330 | ); |
||
331 | } |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * @param TypeInterface $type |
||
336 | * @return TypeInterface |
||
337 | * @throws InvalidArgumentException |
||
338 | * @throws InvariantException |
||
339 | */ |
||
340 | protected function getExtendedType(TypeInterface $type): TypeInterface |
||
341 | { |
||
342 | $typeName = $type->getName(); |
||
|
|||
343 | |||
344 | if (!isset($this->extendTypeCache[$typeName])) { |
||
345 | $this->extendTypeCache[$typeName] = $this->extendType($type); |
||
346 | } |
||
347 | |||
348 | return $this->extendTypeCache[$typeName]; |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * @param TypeInterface $type |
||
353 | * @return TypeInterface |
||
354 | * @throws InvariantException |
||
355 | */ |
||
356 | protected function extendType(TypeInterface $type): TypeInterface |
||
357 | { |
||
358 | /** @noinspection PhpParamsInspection */ |
||
359 | if (isIntrospectionType($type)) { |
||
360 | // Introspection types are not extended. |
||
361 | return $type; |
||
362 | } |
||
363 | |||
364 | if ($type instanceof ObjectType) { |
||
365 | return $this->extendObjectType($type); |
||
366 | } |
||
367 | |||
368 | if ($type instanceof InterfaceType) { |
||
369 | return $this->extendInterfaceType($type); |
||
370 | } |
||
371 | |||
372 | if ($type instanceof UnionType) { |
||
373 | return $this->extendUnionType($type); |
||
374 | } |
||
375 | |||
376 | // This type is not yet extendable. |
||
377 | return $type; |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * @param ObjectType $type |
||
382 | * @return ObjectType |
||
383 | */ |
||
384 | protected function extendObjectType(ObjectType $type): ObjectType |
||
385 | { |
||
386 | $typeName = $type->getName(); |
||
387 | $extensionASTNodes = $type->getExtensionAstNodes(); |
||
388 | |||
389 | if (isset($this->typeExtensionsMap[$typeName])) { |
||
390 | $extensionASTNodes = !empty($extensionASTNodes) |
||
391 | ? \array_merge($this->typeExtensionsMap[$typeName], $extensionASTNodes) |
||
392 | : $this->typeExtensionsMap[$typeName]; |
||
393 | } |
||
394 | |||
395 | return GraphQLObjectType([ |
||
396 | 'name' => $typeName, |
||
397 | 'description' => $type->getDescription(), |
||
398 | 'interfaces' => function () use ($type) { |
||
399 | return $this->extendImplementedInterfaces($type); |
||
400 | }, |
||
401 | 'fields' => function () use ($type) { |
||
402 | return $this->extendFieldMap($type); |
||
403 | }, |
||
404 | 'astNode' => $type->getAstNode(), |
||
405 | 'extensionASTNodes' => $extensionASTNodes, |
||
406 | 'isTypeOf' => $type->getIsTypeOf(), |
||
407 | ]); |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * @param InterfaceType $type |
||
412 | * @return InterfaceType |
||
413 | */ |
||
414 | protected function extendInterfaceType(InterfaceType $type): InterfaceType |
||
415 | { |
||
416 | $typeName = $type->getName(); |
||
417 | $extensionASTNodes = $this->typeExtensionsMap[$typeName] ?? []; |
||
418 | |||
419 | if (isset($this->typeExtensionsMap[$typeName])) { |
||
420 | $extensionASTNodes = !empty($extensionASTNodes) |
||
421 | ? \array_merge($this->typeExtensionsMap[$typeName], $extensionASTNodes) |
||
422 | : $this->typeExtensionsMap[$typeName]; |
||
423 | } |
||
424 | |||
425 | return GraphQLInterfaceType([ |
||
426 | 'name' => $typeName, |
||
427 | 'description' => $type->getDescription(), |
||
428 | 'fields' => function () use ($type) { |
||
429 | return $this->extendFieldMap($type); |
||
430 | }, |
||
431 | 'astNode' => $type->getAstNode(), |
||
432 | 'extensionASTNodes' => $extensionASTNodes, |
||
433 | 'resolveType' => $type->getResolveType(), |
||
434 | ]); |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * @param UnionType $type |
||
439 | * @return UnionType |
||
440 | * @throws InvariantException |
||
441 | */ |
||
442 | protected function extendUnionType(UnionType $type): UnionType |
||
443 | { |
||
444 | return GraphQLUnionType([ |
||
445 | 'name' => $type->getName(), |
||
446 | 'description' => $type->getDescription(), |
||
447 | 'types' => \array_map(function ($unionType) { |
||
448 | return $this->getExtendedType($unionType); |
||
449 | }, $type->getTypes()), |
||
450 | 'astNode' => $type->getAstNode(), |
||
451 | 'resolveType' => $type->getResolveType(), |
||
452 | ]); |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * @param ObjectType $type |
||
457 | * @return array |
||
458 | * @throws InvariantException |
||
459 | */ |
||
460 | protected function extendImplementedInterfaces(ObjectType $type): array |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * @param TypeInterface|ObjectType|InterfaceType $type |
||
485 | * @return array |
||
486 | * @throws InvalidTypeException |
||
487 | * @throws InvariantException |
||
488 | * @throws ExtensionException |
||
489 | * @throws InvalidArgumentException |
||
490 | */ |
||
491 | protected function extendFieldMap(TypeInterface $type): array |
||
538 | } |
||
539 | |||
540 | /** |
||
541 | * @param TypeInterface $typeDefinition |
||
542 | * @return TypeInterface |
||
543 | * @throws InvalidArgumentException |
||
544 | * @throws InvalidTypeException |
||
545 | * @throws InvariantException |
||
546 | */ |
||
547 | protected function extendFieldType(TypeInterface $typeDefinition): TypeInterface |
||
558 | } |
||
559 | } |
||
560 |