| Total Complexity | 41 |
| Total Lines | 373 |
| 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 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return TypeInterface|null |
||
| 67 | * @throws InvariantException |
||
| 68 | */ |
||
| 69 | public function getExtendedQueryType(): ?TypeInterface |
||
| 70 | { |
||
| 71 | $existingQueryType = $this->info->getSchema()->getQueryType(); |
||
| 72 | |||
| 73 | return null !== $existingQueryType |
||
| 74 | ? $this->getExtendedType($existingQueryType) |
||
| 75 | : null; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return TypeInterface|null |
||
| 80 | * @throws InvariantException |
||
| 81 | */ |
||
| 82 | public function getExtendedMutationType(): ?TypeInterface |
||
| 83 | { |
||
| 84 | $existingMutationType = $this->info->getSchema()->getMutationType(); |
||
| 85 | |||
| 86 | return null !== $existingMutationType |
||
| 87 | ? $this->getExtendedType($existingMutationType) |
||
| 88 | : null; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return TypeInterface|null |
||
| 93 | * @throws InvariantException |
||
| 94 | */ |
||
| 95 | public function getExtendedSubscriptionType(): ?TypeInterface |
||
| 96 | { |
||
| 97 | $existingSubscriptionType = $this->info->getSchema()->getSubscriptionType(); |
||
| 98 | |||
| 99 | return null !== $existingSubscriptionType |
||
| 100 | ? $this->getExtendedType($existingSubscriptionType) |
||
| 101 | : null; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return TypeInterface[] |
||
| 106 | */ |
||
| 107 | public function getExtendedTypes(): array |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return Directive[] |
||
| 121 | * @throws InvariantException |
||
| 122 | */ |
||
| 123 | public function getExtendedDirectives(): array |
||
| 136 | ); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param DefinitionBuilderInterface $definitionBuilder |
||
| 141 | * @return ExtensionContext |
||
| 142 | */ |
||
| 143 | public function setDefinitionBuilder(DefinitionBuilderInterface $definitionBuilder): ExtensionContext |
||
| 144 | { |
||
| 145 | $this->definitionBuilder = $definitionBuilder; |
||
| 146 | return $this; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param NamedTypeNode $node |
||
| 151 | * @return TypeInterface|null |
||
| 152 | * @throws ExtensionException |
||
| 153 | * @throws InvariantException |
||
| 154 | */ |
||
| 155 | public function resolveType(NamedTypeNode $node): ?TypeInterface |
||
| 171 | ); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param NamedTypeInterface $type |
||
| 176 | * @return TypeInterface |
||
| 177 | * @throws InvariantException |
||
| 178 | */ |
||
| 179 | protected function getExtendedType(NamedTypeInterface $type): TypeInterface |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param TypeInterface $type |
||
| 192 | * @return TypeInterface |
||
| 193 | * @throws InvariantException |
||
| 194 | */ |
||
| 195 | protected function extendType(TypeInterface $type): TypeInterface |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param ObjectType $type |
||
| 221 | * @return ObjectType |
||
| 222 | * @throws InvariantException |
||
| 223 | */ |
||
| 224 | protected function extendObjectType(ObjectType $type): ObjectType |
||
| 245 | ]); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param InterfaceType $type |
||
| 250 | * @return InterfaceType |
||
| 251 | * @throws InvariantException |
||
| 252 | */ |
||
| 253 | protected function extendInterfaceType(InterfaceType $type): InterfaceType |
||
| 254 | { |
||
| 255 | $typeName = $type->getName(); |
||
| 256 | $extensionASTNodes = $this->info->getTypeExtensions($typeName); |
||
| 257 | |||
| 258 | if ($this->info->hasTypeExtensions($typeName)) { |
||
| 259 | $extensionASTNodes = $this->extendExtensionASTNodes($typeName, $extensionASTNodes); |
||
| 260 | } |
||
| 261 | |||
| 262 | return newInterfaceType([ |
||
| 263 | 'name' => $typeName, |
||
| 264 | 'description' => $type->getDescription(), |
||
| 265 | 'fields' => function () use ($type) { |
||
| 266 | return $this->extendFieldMap($type); |
||
| 267 | }, |
||
| 268 | 'astNode' => $type->getAstNode(), |
||
| 269 | 'extensionASTNodes' => $extensionASTNodes, |
||
| 270 | 'resolveType' => $type->getResolveTypeCallback(), |
||
| 271 | ]); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $typeName |
||
| 276 | * @param array $nodes |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | protected function extendExtensionASTNodes(string $typeName, array $nodes): array |
||
| 280 | { |
||
| 281 | $typeExtensions = $this->info->getTypeExtensions($typeName); |
||
| 282 | return !empty($nodes) ? \array_merge($typeExtensions, $nodes) : $typeExtensions; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param UnionType $type |
||
| 287 | * @return UnionType |
||
| 288 | * @throws InvariantException |
||
| 289 | */ |
||
| 290 | protected function extendUnionType(UnionType $type): UnionType |
||
| 300 | ]); |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param ObjectType $type |
||
| 305 | * @return array |
||
| 306 | * @throws InvariantException |
||
| 307 | */ |
||
| 308 | protected function extendImplementedInterfaces(ObjectType $type): array |
||
| 309 | { |
||
| 310 | $typeName = $type->getName(); |
||
| 311 | |||
| 312 | $interfaces = \array_map(function (InterfaceType $interface) { |
||
| 313 | return $this->getExtendedType($interface); |
||
| 314 | }, $type->getInterfaces()); |
||
| 315 | |||
| 316 | // If there are any extensions to the interfaces, apply those here. |
||
| 317 | $extensions = $this->info->getTypeExtensions($typeName); |
||
| 318 | |||
| 319 | foreach ($extensions as $extension) { |
||
| 320 | foreach ($extension->getInterfaces() as $namedType) { |
||
| 321 | // Note: While this could make early assertions to get the correctly |
||
| 322 | // typed values, that would throw immediately while type system |
||
| 323 | // validation with validateSchema() will produce more actionable results. |
||
| 324 | $interfaces[] = $this->definitionBuilder->buildType($namedType); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | return $interfaces; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param FieldsAwareInterface $type |
||
| 333 | * @return array |
||
| 334 | * @throws InvalidTypeException |
||
| 335 | * @throws InvariantException |
||
| 336 | * @throws ExtensionException |
||
| 337 | * @throws InvalidArgumentException |
||
| 338 | */ |
||
| 339 | protected function extendFieldMap(FieldsAwareInterface $type): array |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param TypeInterface $typeDefinition |
||
| 385 | * @return TypeInterface |
||
| 386 | * @throws InvalidArgumentException |
||
| 387 | * @throws InvalidTypeException |
||
| 388 | * @throws InvariantException |
||
| 389 | */ |
||
| 390 | protected function extendFieldType(TypeInterface $typeDefinition): TypeInterface |
||
| 403 |