Total Complexity | 46 |
Total Lines | 401 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
26 | class ExtensionContext implements ExtensionContextInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var ExtendInfo |
||
30 | */ |
||
31 | protected $info; |
||
32 | |||
33 | /** |
||
34 | * @var DefinitionBuilderInterface |
||
35 | */ |
||
36 | protected $definitionBuilder; |
||
37 | |||
38 | /** |
||
39 | * @var TypeInterface[] |
||
40 | */ |
||
41 | protected $extendTypeCache = []; |
||
42 | |||
43 | /** |
||
44 | * ExtensionContext constructor. |
||
45 | * @param ExtendInfo $info |
||
46 | */ |
||
47 | public function __construct(ExtendInfo $info) |
||
48 | { |
||
49 | $this->info = $info; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @return bool |
||
54 | */ |
||
55 | public function isSchemaExtended(): bool |
||
56 | { |
||
57 | return |
||
58 | $this->info->hasTypeExtensionsMap() || |
||
59 | $this->info->hasTypeDefinitionMap() || |
||
60 | $this->info->hasDirectiveDefinitions() || |
||
61 | $this->info->hasSchemaExtensions(); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @return ObjectType[] |
||
66 | * @throws SchemaExtensionException |
||
67 | * @throws InvariantException |
||
68 | */ |
||
69 | public function getExtendedOperationTypes(): array |
||
70 | { |
||
71 | /** @noinspection PhpUnhandledExceptionInspection */ |
||
72 | $operationTypes = [ |
||
73 | 'query' => $this->getExtendedQueryType(), |
||
74 | 'mutation' => $this->getExtendedMutationType(), |
||
75 | 'subscription' => $this->getExtendedSubscriptionType(), |
||
76 | ]; |
||
77 | |||
78 | foreach ($this->info->getSchemaExtensions() as $schemaExtension) { |
||
79 | foreach ($schemaExtension->getOperationTypes() as $operationType) { |
||
80 | $operation = $operationType->getOperation(); |
||
81 | |||
82 | if (isset($operationTypes[$operation])) { |
||
83 | throw new SchemaExtensionException(\sprintf('Must provide only one %s type in schema.', $operation)); |
||
84 | } |
||
85 | |||
86 | $operationTypes[$operation] = $this->definitionBuilder->buildType($operationType->getType()); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | return $operationTypes; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @return TypeInterface|null |
||
95 | * @throws InvariantException |
||
96 | */ |
||
97 | protected function getExtendedQueryType(): ?TypeInterface |
||
98 | { |
||
99 | $existingQueryType = $this->info->getSchema()->getQueryType(); |
||
100 | |||
101 | return null !== $existingQueryType |
||
102 | ? $this->getExtendedType($existingQueryType) |
||
103 | : null; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return TypeInterface|null |
||
108 | * @throws InvariantException |
||
109 | */ |
||
110 | protected function getExtendedMutationType(): ?TypeInterface |
||
111 | { |
||
112 | $existingMutationType = $this->info->getSchema()->getMutationType(); |
||
113 | |||
114 | return null !== $existingMutationType |
||
115 | ? $this->getExtendedType($existingMutationType) |
||
116 | : null; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @return TypeInterface|null |
||
121 | * @throws InvariantException |
||
122 | */ |
||
123 | protected function getExtendedSubscriptionType(): ?TypeInterface |
||
124 | { |
||
125 | $existingSubscriptionType = $this->info->getSchema()->getSubscriptionType(); |
||
126 | |||
127 | return null !== $existingSubscriptionType |
||
128 | ? $this->getExtendedType($existingSubscriptionType) |
||
129 | : null; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @return TypeInterface[] |
||
134 | */ |
||
135 | public function getExtendedTypes(): array |
||
144 | ); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @return Directive[] |
||
149 | * @throws InvariantException |
||
150 | */ |
||
151 | public function getExtendedDirectives(): array |
||
164 | ); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param DefinitionBuilderInterface $definitionBuilder |
||
169 | * @return ExtensionContext |
||
170 | */ |
||
171 | public function setDefinitionBuilder(DefinitionBuilderInterface $definitionBuilder): ExtensionContext |
||
172 | { |
||
173 | $this->definitionBuilder = $definitionBuilder; |
||
174 | return $this; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @param NamedTypeNode $node |
||
179 | * @return TypeInterface|null |
||
180 | * @throws SchemaExtensionException |
||
181 | * @throws InvariantException |
||
182 | */ |
||
183 | public function resolveType(NamedTypeNode $node): ?TypeInterface |
||
184 | { |
||
185 | $typeName = $node->getNameValue(); |
||
186 | $existingType = $this->info->getSchema()->getType($typeName); |
||
|
|||
187 | |||
188 | if ($existingType instanceof NamedTypeInterface) { |
||
189 | return $this->getExtendedType($existingType); |
||
190 | } |
||
191 | |||
192 | throw new SchemaExtensionException( |
||
193 | \sprintf( |
||
194 | 'Unknown type: "%s". Ensure that this type exists ' . |
||
195 | 'either in the original schema, or is added in a type definition.', |
||
196 | $typeName |
||
197 | ), |
||
198 | [$node] |
||
199 | ); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param NamedTypeInterface $type |
||
204 | * @return TypeInterface |
||
205 | * @throws InvariantException |
||
206 | */ |
||
207 | protected function getExtendedType(NamedTypeInterface $type): TypeInterface |
||
208 | { |
||
209 | $typeName = $type->getName(); |
||
210 | |||
211 | if (isset($this->extendTypeCache[$typeName])) { |
||
212 | return $this->extendTypeCache[$typeName]; |
||
213 | } |
||
214 | |||
215 | return $this->extendTypeCache[$typeName] = $this->extendType($type); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @param TypeInterface $type |
||
220 | * @return TypeInterface |
||
221 | * @throws InvariantException |
||
222 | */ |
||
223 | protected function extendType(TypeInterface $type): TypeInterface |
||
224 | { |
||
225 | /** @noinspection PhpParamsInspection */ |
||
226 | if (isIntrospectionType($type)) { |
||
227 | // Introspection types are not extended. |
||
228 | return $type; |
||
229 | } |
||
230 | |||
231 | if ($type instanceof ObjectType) { |
||
232 | return $this->extendObjectType($type); |
||
233 | } |
||
234 | |||
235 | if ($type instanceof InterfaceType) { |
||
236 | return $this->extendInterfaceType($type); |
||
237 | } |
||
238 | |||
239 | if ($type instanceof UnionType) { |
||
240 | return $this->extendUnionType($type); |
||
241 | } |
||
242 | |||
243 | // This type is not yet extendable. |
||
244 | return $type; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @param ObjectType $type |
||
249 | * @return ObjectType |
||
250 | * @throws InvariantException |
||
251 | */ |
||
252 | protected function extendObjectType(ObjectType $type): ObjectType |
||
253 | { |
||
254 | $typeName = $type->getName(); |
||
255 | $extensionASTNodes = $type->getExtensionAstNodes(); |
||
256 | |||
257 | if ($this->info->hasTypeExtensions($typeName)) { |
||
258 | $extensionASTNodes = $this->extendExtensionASTNodes($typeName, $extensionASTNodes); |
||
259 | } |
||
260 | |||
261 | return newObjectType([ |
||
262 | 'name' => $typeName, |
||
263 | 'description' => $type->getDescription(), |
||
264 | 'interfaces' => function () use ($type) { |
||
265 | return $this->extendImplementedInterfaces($type); |
||
266 | }, |
||
267 | 'fields' => function () use ($type) { |
||
268 | return $this->extendFieldMap($type); |
||
269 | }, |
||
270 | 'astNode' => $type->getAstNode(), |
||
271 | 'extensionASTNodes' => $extensionASTNodes, |
||
272 | 'isTypeOf' => $type->getIsTypeOfCallback(), |
||
273 | ]); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @param InterfaceType $type |
||
278 | * @return InterfaceType |
||
279 | * @throws InvariantException |
||
280 | */ |
||
281 | protected function extendInterfaceType(InterfaceType $type): InterfaceType |
||
282 | { |
||
283 | $typeName = $type->getName(); |
||
284 | $extensionASTNodes = $this->info->getTypeExtensions($typeName); |
||
285 | |||
286 | if ($this->info->hasTypeExtensions($typeName)) { |
||
287 | $extensionASTNodes = $this->extendExtensionASTNodes($typeName, $extensionASTNodes); |
||
288 | } |
||
289 | |||
290 | return newInterfaceType([ |
||
291 | 'name' => $typeName, |
||
292 | 'description' => $type->getDescription(), |
||
293 | 'fields' => function () use ($type) { |
||
294 | return $this->extendFieldMap($type); |
||
295 | }, |
||
296 | 'astNode' => $type->getAstNode(), |
||
297 | 'extensionASTNodes' => $extensionASTNodes, |
||
298 | 'resolveType' => $type->getResolveTypeCallback(), |
||
299 | ]); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @param string $typeName |
||
304 | * @param array $nodes |
||
305 | * @return array |
||
306 | */ |
||
307 | protected function extendExtensionASTNodes(string $typeName, array $nodes): array |
||
308 | { |
||
309 | $typeExtensions = $this->info->getTypeExtensions($typeName); |
||
310 | return !empty($nodes) ? \array_merge($typeExtensions, $nodes) : $typeExtensions; |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * @param UnionType $type |
||
315 | * @return UnionType |
||
316 | * @throws InvariantException |
||
317 | */ |
||
318 | protected function extendUnionType(UnionType $type): UnionType |
||
319 | { |
||
320 | return newUnionType([ |
||
321 | 'name' => $type->getName(), |
||
322 | 'description' => $type->getDescription(), |
||
323 | 'types' => \array_map(function ($unionType) { |
||
324 | return $this->getExtendedType($unionType); |
||
325 | }, $type->getTypes()), |
||
326 | 'astNode' => $type->getAstNode(), |
||
327 | 'resolveType' => $type->getResolveTypeCallback(), |
||
328 | ]); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * @param ObjectType $type |
||
333 | * @return array |
||
334 | * @throws InvariantException |
||
335 | */ |
||
336 | protected function extendImplementedInterfaces(ObjectType $type): array |
||
337 | { |
||
338 | $typeName = $type->getName(); |
||
339 | |||
340 | $interfaces = \array_map(function (InterfaceType $interface) { |
||
341 | return $this->getExtendedType($interface); |
||
342 | }, $type->getInterfaces()); |
||
343 | |||
344 | // If there are any extensions to the interfaces, apply those here. |
||
345 | $extensions = $this->info->getTypeExtensions($typeName); |
||
346 | |||
347 | foreach ($extensions as $extension) { |
||
348 | foreach ($extension->getInterfaces() as $namedType) { |
||
349 | // Note: While this could make early assertions to get the correctly |
||
350 | // typed values, that would throw immediately while type system |
||
351 | // validation with validateSchema() will produce more actionable results. |
||
352 | $interfaces[] = $this->definitionBuilder->buildType($namedType); |
||
353 | } |
||
354 | } |
||
355 | |||
356 | return $interfaces; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * @param FieldsAwareInterface $type |
||
361 | * @return array |
||
362 | * @throws InvalidTypeException |
||
363 | * @throws InvariantException |
||
364 | * @throws SchemaExtensionException |
||
365 | */ |
||
366 | protected function extendFieldMap(FieldsAwareInterface $type): array |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * @param TypeInterface $typeDefinition |
||
412 | * @return TypeInterface |
||
413 | * @throws InvalidTypeException |
||
414 | * @throws InvariantException |
||
415 | */ |
||
416 | protected function extendFieldType(TypeInterface $typeDefinition): TypeInterface |
||
417 | { |
||
427 | } |
||
428 | } |
||
429 |