| Total Complexity | 46 |
| Total Lines | 403 |
| 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 |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return TypeInterface|null |
||
| 110 | * @throws InvariantException |
||
| 111 | */ |
||
| 112 | protected function getExtendedMutationType(): ?TypeInterface |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return TypeInterface|null |
||
| 123 | * @throws InvariantException |
||
| 124 | */ |
||
| 125 | protected function getExtendedSubscriptionType(): ?TypeInterface |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return TypeInterface[] |
||
| 136 | */ |
||
| 137 | public function getExtendedTypes(): array |
||
| 146 | ); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return Directive[] |
||
| 151 | * @throws InvariantException |
||
| 152 | */ |
||
| 153 | public function getExtendedDirectives(): array |
||
| 166 | ); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param DefinitionBuilderInterface $definitionBuilder |
||
| 171 | * @return ExtensionContext |
||
| 172 | */ |
||
| 173 | public function setDefinitionBuilder(DefinitionBuilderInterface $definitionBuilder): ExtensionContext |
||
| 174 | { |
||
| 175 | $this->definitionBuilder = $definitionBuilder; |
||
| 176 | return $this; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param NamedTypeNode $node |
||
| 181 | * @return TypeInterface|null |
||
| 182 | * @throws ExtensionException |
||
| 183 | * @throws InvariantException |
||
| 184 | */ |
||
| 185 | public function resolveType(NamedTypeNode $node): ?TypeInterface |
||
| 186 | { |
||
| 187 | $typeName = $node->getNameValue(); |
||
| 188 | $existingType = $this->info->getSchema()->getType($typeName); |
||
| 189 | |||
| 190 | if ($existingType instanceof NamedTypeInterface) { |
||
| 191 | return $this->getExtendedType($existingType); |
||
| 192 | } |
||
| 193 | |||
| 194 | throw new ExtensionException( |
||
| 195 | \sprintf( |
||
| 196 | 'Unknown type: "%s". Ensure that this type exists ' . |
||
| 197 | 'either in the original schema, or is added in a type definition.', |
||
| 198 | $typeName |
||
| 199 | ), |
||
| 200 | [$node] |
||
| 201 | ); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param NamedTypeInterface $type |
||
| 206 | * @return TypeInterface |
||
| 207 | * @throws InvariantException |
||
| 208 | */ |
||
| 209 | protected function getExtendedType(NamedTypeInterface $type): TypeInterface |
||
| 210 | { |
||
| 211 | $typeName = $type->getName(); |
||
| 212 | |||
| 213 | if (isset($this->extendTypeCache[$typeName])) { |
||
| 214 | return $this->extendTypeCache[$typeName]; |
||
| 215 | } |
||
| 216 | |||
| 217 | return $this->extendTypeCache[$typeName] = $this->extendType($type); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param TypeInterface $type |
||
| 222 | * @return TypeInterface |
||
| 223 | * @throws InvariantException |
||
| 224 | */ |
||
| 225 | protected function extendType(TypeInterface $type): TypeInterface |
||
| 226 | { |
||
| 227 | /** @noinspection PhpParamsInspection */ |
||
| 228 | if (isIntrospectionType($type)) { |
||
| 229 | // Introspection types are not extended. |
||
| 230 | return $type; |
||
| 231 | } |
||
| 232 | |||
| 233 | if ($type instanceof ObjectType) { |
||
| 234 | return $this->extendObjectType($type); |
||
| 235 | } |
||
| 236 | |||
| 237 | if ($type instanceof InterfaceType) { |
||
| 238 | return $this->extendInterfaceType($type); |
||
| 239 | } |
||
| 240 | |||
| 241 | if ($type instanceof UnionType) { |
||
| 242 | return $this->extendUnionType($type); |
||
| 243 | } |
||
| 244 | |||
| 245 | // This type is not yet extendable. |
||
| 246 | return $type; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param ObjectType $type |
||
| 251 | * @return ObjectType |
||
| 252 | * @throws InvariantException |
||
| 253 | */ |
||
| 254 | protected function extendObjectType(ObjectType $type): ObjectType |
||
| 255 | { |
||
| 256 | $typeName = $type->getName(); |
||
| 257 | $extensionASTNodes = $type->getExtensionAstNodes(); |
||
| 258 | |||
| 259 | if ($this->info->hasTypeExtensions($typeName)) { |
||
| 260 | $extensionASTNodes = $this->extendExtensionASTNodes($typeName, $extensionASTNodes); |
||
| 261 | } |
||
| 262 | |||
| 263 | return newObjectType([ |
||
| 264 | 'name' => $typeName, |
||
| 265 | 'description' => $type->getDescription(), |
||
| 266 | 'interfaces' => function () use ($type) { |
||
| 267 | return $this->extendImplementedInterfaces($type); |
||
| 268 | }, |
||
| 269 | 'fields' => function () use ($type) { |
||
| 270 | return $this->extendFieldMap($type); |
||
| 271 | }, |
||
| 272 | 'astNode' => $type->getAstNode(), |
||
| 273 | 'extensionASTNodes' => $extensionASTNodes, |
||
| 274 | 'isTypeOf' => $type->getIsTypeOf(), |
||
| 275 | ]); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param InterfaceType $type |
||
| 280 | * @return InterfaceType |
||
| 281 | * @throws InvariantException |
||
| 282 | */ |
||
| 283 | protected function extendInterfaceType(InterfaceType $type): InterfaceType |
||
| 284 | { |
||
| 285 | $typeName = $type->getName(); |
||
| 286 | $extensionASTNodes = $this->info->getTypeExtensions($typeName); |
||
| 287 | |||
| 288 | if ($this->info->hasTypeExtensions($typeName)) { |
||
| 289 | $extensionASTNodes = $this->extendExtensionASTNodes($typeName, $extensionASTNodes); |
||
| 290 | } |
||
| 291 | |||
| 292 | return newInterfaceType([ |
||
| 293 | 'name' => $typeName, |
||
| 294 | 'description' => $type->getDescription(), |
||
| 295 | 'fields' => function () use ($type) { |
||
| 296 | return $this->extendFieldMap($type); |
||
| 297 | }, |
||
| 298 | 'astNode' => $type->getAstNode(), |
||
| 299 | 'extensionASTNodes' => $extensionASTNodes, |
||
| 300 | 'resolveType' => $type->getResolveTypeCallback(), |
||
| 301 | ]); |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $typeName |
||
| 306 | * @param array $nodes |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | protected function extendExtensionASTNodes(string $typeName, array $nodes): array |
||
| 310 | { |
||
| 311 | $typeExtensions = $this->info->getTypeExtensions($typeName); |
||
| 312 | return !empty($nodes) ? \array_merge($typeExtensions, $nodes) : $typeExtensions; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param UnionType $type |
||
| 317 | * @return UnionType |
||
| 318 | * @throws InvariantException |
||
| 319 | */ |
||
| 320 | protected function extendUnionType(UnionType $type): UnionType |
||
| 321 | { |
||
| 322 | return newUnionType([ |
||
| 323 | 'name' => $type->getName(), |
||
| 324 | 'description' => $type->getDescription(), |
||
| 325 | 'types' => \array_map(function ($unionType) { |
||
| 326 | return $this->getExtendedType($unionType); |
||
| 327 | }, $type->getTypes()), |
||
| 328 | 'astNode' => $type->getAstNode(), |
||
| 329 | 'resolveType' => $type->getResolveTypeCallback(), |
||
| 330 | ]); |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param ObjectType $type |
||
| 335 | * @return array |
||
| 336 | * @throws InvariantException |
||
| 337 | */ |
||
| 338 | protected function extendImplementedInterfaces(ObjectType $type): array |
||
| 339 | { |
||
| 340 | $typeName = $type->getName(); |
||
| 341 | |||
| 342 | $interfaces = \array_map(function (InterfaceType $interface) { |
||
| 343 | return $this->getExtendedType($interface); |
||
| 344 | }, $type->getInterfaces()); |
||
| 345 | |||
| 346 | // If there are any extensions to the interfaces, apply those here. |
||
| 347 | $extensions = $this->info->getTypeExtensions($typeName); |
||
| 348 | |||
| 349 | foreach ($extensions as $extension) { |
||
| 350 | foreach ($extension->getInterfaces() as $namedType) { |
||
| 351 | // Note: While this could make early assertions to get the correctly |
||
| 352 | // typed values, that would throw immediately while type system |
||
| 353 | // validation with validateSchema() will produce more actionable results. |
||
| 354 | $interfaces[] = $this->definitionBuilder->buildType($namedType); |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | return $interfaces; |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param FieldsAwareInterface $type |
||
| 363 | * @return array |
||
| 364 | * @throws InvalidTypeException |
||
| 365 | * @throws InvariantException |
||
| 366 | * @throws ExtensionException |
||
| 367 | * @throws InvalidArgumentException |
||
| 368 | */ |
||
| 369 | protected function extendFieldMap(FieldsAwareInterface $type): array |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param TypeInterface $typeDefinition |
||
| 415 | * @return TypeInterface |
||
| 416 | * @throws InvalidArgumentException |
||
| 417 | * @throws InvalidTypeException |
||
| 418 | * @throws InvariantException |
||
| 419 | */ |
||
| 420 | protected function extendFieldType(TypeInterface $typeDefinition): TypeInterface |
||
| 431 | } |
||
| 432 | } |
||
| 433 |