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