| Total Complexity | 40 |
| Total Lines | 357 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Schema 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 Schema, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class Schema extends Definition implements SchemaInterface |
||
| 54 | { |
||
| 55 | use ExtensionASTNodesTrait; |
||
| 56 | use ASTNodeTrait; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var ObjectType|null |
||
| 60 | */ |
||
| 61 | protected $queryType; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var ObjectType|null |
||
| 65 | */ |
||
| 66 | protected $mutationType; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var ObjectType|null |
||
| 70 | */ |
||
| 71 | protected $subscriptionType; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var TypeInterface[] |
||
| 75 | */ |
||
| 76 | protected $types = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $directives = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var bool |
||
| 85 | */ |
||
| 86 | protected $assumeValid = false; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var TypeInterface[] |
||
| 90 | */ |
||
| 91 | protected $typeMap = []; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected $implementations = []; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var NamedTypeInterface[] |
||
| 100 | */ |
||
| 101 | protected $possibleTypesMap = []; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Schema constructor. |
||
| 105 | * |
||
| 106 | * @param ObjectType|null $queryType |
||
| 107 | * @param ObjectType|null $mutationType |
||
| 108 | * @param ObjectType|null $subscriptionType |
||
| 109 | * @param TypeInterface[] $types |
||
| 110 | * @param Directive[] $directives |
||
| 111 | * @param bool $assumeValid |
||
| 112 | * @param SchemaDefinitionNode|null $astNode |
||
| 113 | * @param ObjectTypeExtensionNode[]|InterfaceTypeExtensionNode[] $extensionASTNodes |
||
| 114 | * @throws InvariantException |
||
| 115 | */ |
||
| 116 | public function __construct( |
||
| 117 | ?ObjectType $queryType, |
||
| 118 | ?ObjectType $mutationType, |
||
| 119 | ?ObjectType $subscriptionType, |
||
| 120 | array $types, |
||
| 121 | array $directives, |
||
| 122 | bool $assumeValid, |
||
| 123 | ?SchemaDefinitionNode $astNode, |
||
| 124 | array $extensionASTNodes |
||
| 125 | ) { |
||
| 126 | $this->queryType = $queryType; |
||
| 127 | $this->mutationType = $mutationType; |
||
| 128 | $this->subscriptionType = $subscriptionType; |
||
| 129 | $this->types = $types; |
||
| 130 | $this->directives = !empty($directives) |
||
| 131 | ? $directives |
||
| 132 | : specifiedDirectives(); |
||
| 133 | $this->assumeValid = $assumeValid; |
||
| 134 | $this->astNode = $astNode; |
||
| 135 | $this->extensionAstNodes = $extensionASTNodes; |
||
| 136 | |||
| 137 | $this->buildTypeMap(); |
||
| 138 | $this->buildImplementations(); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return ObjectType|null |
||
| 143 | */ |
||
| 144 | public function getQueryType(): ?ObjectTypeInterface |
||
| 145 | { |
||
| 146 | return $this->queryType; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return ObjectType|null |
||
| 151 | */ |
||
| 152 | public function getMutationType(): ?ObjectTypeInterface |
||
| 153 | { |
||
| 154 | return $this->mutationType; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return ObjectType|null |
||
| 159 | */ |
||
| 160 | public function getSubscriptionType(): ?ObjectTypeInterface |
||
| 161 | { |
||
| 162 | return $this->subscriptionType; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param string $name |
||
| 167 | * @return Directive|null |
||
| 168 | */ |
||
| 169 | public function getDirective(string $name): ?DirectiveInterface |
||
| 170 | { |
||
| 171 | return find($this->directives, static function (Directive $directive) use ($name) { |
||
| 172 | return $directive->getName() === $name; |
||
| 173 | }); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | public function getDirectives(): array |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | public function getTypeMap(): iterable |
||
| 188 | { |
||
| 189 | return $this->typeMap; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | public function getAssumeValid(): bool |
||
| 196 | { |
||
| 197 | return $this->assumeValid; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param AbstractTypeContract|AbstractTypeInterface $abstractType |
||
| 202 | * @param ObjectTypeInterface $possibleType |
||
| 203 | * @return bool |
||
| 204 | * @throws InvariantException |
||
| 205 | */ |
||
| 206 | public function isPossibleType(AbstractTypeContract $abstractType, ObjectTypeInterface $possibleType): bool |
||
| 207 | { |
||
| 208 | assert($abstractType instanceof NamedTypeInterface); |
||
| 209 | |||
| 210 | $abstractTypeName = $abstractType->getName(); |
||
| 211 | $possibleTypeName = $possibleType->getName(); |
||
| 212 | |||
| 213 | if (!isset($this->possibleTypesMap[$abstractTypeName])) { |
||
| 214 | $possibleTypes = $this->getPossibleTypes($abstractType); |
||
| 215 | |||
| 216 | if ($possibleTypes === []) { |
||
| 217 | throw new InvariantException(\sprintf( |
||
| 218 | 'Could not find possible implementing types for %s ' . |
||
| 219 | 'in schema. Check that schema.types is defined and is an array of ' . |
||
| 220 | 'all possible types in the schema.', |
||
| 221 | $abstractTypeName |
||
| 222 | )); |
||
| 223 | } |
||
| 224 | |||
| 225 | $this->possibleTypesMap[$abstractTypeName] = \array_reduce( |
||
| 226 | $possibleTypes, |
||
| 227 | function (array $map, NamedTypeInterface $type) { |
||
| 228 | $map[$type->getName()] = true; |
||
| 229 | return $map; |
||
| 230 | }, |
||
| 231 | [] |
||
| 232 | ); |
||
| 233 | } |
||
| 234 | |||
| 235 | return isset($this->possibleTypesMap[$abstractTypeName][$possibleTypeName]); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param AbstractTypeContract $abstractType |
||
| 240 | * @return NamedTypeInterface[]|null |
||
| 241 | * @throws InvariantException |
||
| 242 | */ |
||
| 243 | public function getPossibleTypes(AbstractTypeContract $abstractType): iterable |
||
| 244 | { |
||
| 245 | if ($abstractType instanceof UnionType) { |
||
| 246 | return $abstractType->getTypes(); |
||
| 247 | } |
||
| 248 | |||
| 249 | return $this->implementations[$abstractType->getName()] ?? []; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $name |
||
| 254 | * @return TypeInterface|null |
||
| 255 | */ |
||
| 256 | public function getType(string $name): ?NamedTypeInterface |
||
| 257 | { |
||
| 258 | return $this->typeMap[$name] ?? null; |
||
|
|
|||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * |
||
| 263 | */ |
||
| 264 | protected function buildTypeMap(): void |
||
| 265 | { |
||
| 266 | $initialTypes = [ |
||
| 267 | $this->queryType, |
||
| 268 | $this->mutationType, |
||
| 269 | $this->subscriptionType, |
||
| 270 | __Schema(), // Introspection schema |
||
| 271 | ]; |
||
| 272 | |||
| 273 | if (!empty($this->types)) { |
||
| 274 | $initialTypes = \array_merge($initialTypes, $this->types); |
||
| 275 | } |
||
| 276 | |||
| 277 | // Keep track of all types referenced within the schema. |
||
| 278 | $typeMap = []; |
||
| 279 | |||
| 280 | // First by deeply visiting all initial types. |
||
| 281 | $typeMap = \array_reduce($initialTypes, [$this, 'typeMapReducer'], $typeMap); |
||
| 282 | |||
| 283 | // Then by deeply visiting all directive types. |
||
| 284 | $typeMap = \array_reduce($this->directives, [$this, 'typeMapDirectiveReducer'], $typeMap); |
||
| 285 | |||
| 286 | // Storing the resulting map for reference by the schema. |
||
| 287 | $this->typeMap = $typeMap; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @throws InvariantException |
||
| 292 | */ |
||
| 293 | protected function buildImplementations(): void |
||
| 294 | { |
||
| 295 | $implementations = []; |
||
| 296 | |||
| 297 | // Keep track of all implementations by interface name. |
||
| 298 | foreach ($this->typeMap as $typeName => $type) { |
||
| 299 | if ($type instanceof ObjectType) { |
||
| 300 | foreach ($type->getInterfaces() as $interface) { |
||
| 301 | if (!($interface instanceof InterfaceType)) { |
||
| 302 | continue; |
||
| 303 | } |
||
| 304 | |||
| 305 | $interfaceName = $interface->getName(); |
||
| 306 | |||
| 307 | if (!isset($implementations[$interfaceName])) { |
||
| 308 | $implementations[$interfaceName] = []; |
||
| 309 | } |
||
| 310 | |||
| 311 | $implementations[$interfaceName][] = $type; |
||
| 312 | } |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | $this->implementations = $implementations; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param array $map |
||
| 321 | * @param TypeInterface|null $type |
||
| 322 | * @return array |
||
| 323 | * @throws InvariantException |
||
| 324 | */ |
||
| 325 | protected function typeMapReducer(array $map, ?TypeInterface $type): array |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param array $map |
||
| 389 | * @param Directive $directive |
||
| 390 | * @return array |
||
| 391 | * @throws InvariantException |
||
| 392 | */ |
||
| 393 | protected function typeMapDirectiveReducer(array $map, Directive $directive): array |
||
| 394 | { |
||
| 395 | if (!$directive->hasArguments()) { |
||
| 396 | return $map; |
||
| 397 | } |
||
| 398 | |||
| 399 | return \array_reduce($directive->getArguments(), function ($map, Argument $argument) { |
||
| 400 | return $this->typeMapReducer($map, $argument->getNullableType()); |
||
| 401 | }, $map); |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | public function __toString(): string |
||
| 410 | } |
||
| 411 | } |
||
| 412 |