| Total Complexity | 44 |
| Total Lines | 379 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 57 | class Schema implements SchemaInterface, ConfigAwareInterface |
||
| 58 | { |
||
| 59 | use ConfigAwareTrait; |
||
| 60 | use NodeTrait; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var TypeInterface|null |
||
| 64 | */ |
||
| 65 | protected $query; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var TypeInterface|null |
||
| 69 | */ |
||
| 70 | protected $mutation; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var TypeInterface|null |
||
| 74 | */ |
||
| 75 | protected $subscription; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var TypeInterface |
||
| 79 | */ |
||
| 80 | protected $types = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $directives = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var bool |
||
| 89 | */ |
||
| 90 | protected $assumeValid = false; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | protected $typeMap = []; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | protected $implementations = []; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | protected $possibleTypesMap = []; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @inheritdoc |
||
| 109 | */ |
||
| 110 | public function getQueryType(): ?TypeInterface |
||
| 111 | { |
||
| 112 | return $this->query; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @inheritdoc |
||
| 117 | */ |
||
| 118 | public function getMutationType(): ?TypeInterface |
||
| 119 | { |
||
| 120 | return $this->mutation; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @inheritdoc |
||
| 125 | */ |
||
| 126 | public function getSubscriptionType(): ?TypeInterface |
||
| 127 | { |
||
| 128 | return $this->subscription; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @inheritdoc |
||
| 133 | */ |
||
| 134 | public function getDirective(string $name): ?Directive |
||
| 135 | { |
||
| 136 | return find($this->directives, function (Directive $directive) use ($name) { |
||
| 137 | return $directive->getName() === $name; |
||
| 138 | }); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @inheritdoc |
||
| 143 | */ |
||
| 144 | public function getDirectives(): array |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @inheritdoc |
||
| 151 | */ |
||
| 152 | public function getTypeMap(): array |
||
| 153 | { |
||
| 154 | return $this->typeMap; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @inheritdoc |
||
| 159 | */ |
||
| 160 | public function getAssumeValid(): bool |
||
| 161 | { |
||
| 162 | return $this->assumeValid; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @inheritdoc |
||
| 167 | */ |
||
| 168 | public function isPossibleType(AbstractTypeInterface $abstractType, TypeInterface $possibleType): bool |
||
| 169 | { |
||
| 170 | /** @noinspection PhpUndefinedMethodInspection */ |
||
| 171 | $abstractTypeName = $abstractType->getName(); |
||
|
|
|||
| 172 | /** @noinspection PhpUndefinedMethodInspection */ |
||
| 173 | $possibleTypeName = $possibleType->getName(); |
||
| 174 | |||
| 175 | if (!isset($this->possibleTypesMap[$abstractTypeName])) { |
||
| 176 | $possibleTypes = $this->getPossibleTypes($abstractType); |
||
| 177 | |||
| 178 | invariant( |
||
| 179 | \is_array($possibleTypes), |
||
| 180 | \sprintf( |
||
| 181 | 'Could not find possible implementing types for %s ' . |
||
| 182 | 'in schema. Check that schema.types is defined and is an array of ' . |
||
| 183 | 'all possible types in the schema.', |
||
| 184 | $abstractTypeName |
||
| 185 | ) |
||
| 186 | ); |
||
| 187 | |||
| 188 | $this->possibleTypesMap[$abstractTypeName] = \array_reduce($possibleTypes, |
||
| 189 | function (array $map, TypeInterface $type) { |
||
| 190 | /** @var NameAwareInterface $type */ |
||
| 191 | $map[$type->getName()] = true; |
||
| 192 | return $map; |
||
| 193 | }, []); |
||
| 194 | } |
||
| 195 | |||
| 196 | return isset($this->possibleTypesMap[$abstractTypeName][$possibleTypeName]); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @inheritdoc |
||
| 201 | */ |
||
| 202 | public function getPossibleTypes(AbstractTypeInterface $abstractType): ?array |
||
| 203 | { |
||
| 204 | if ($abstractType instanceof UnionType) { |
||
| 205 | return $abstractType->getTypes(); |
||
| 206 | } |
||
| 207 | |||
| 208 | return $this->implementations[$abstractType->getName()] ?? null; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @inheritdoc |
||
| 213 | */ |
||
| 214 | public function getType(string $name): ?TypeInterface |
||
| 215 | { |
||
| 216 | return $this->typeMap[$name] ?? null; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @inheritdoc |
||
| 221 | */ |
||
| 222 | protected function beforeConfig(): void |
||
| 223 | { |
||
| 224 | $this->setDirectives(specifiedDirectives()); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @throws InvariantException |
||
| 229 | */ |
||
| 230 | protected function afterConfig(): void |
||
| 231 | { |
||
| 232 | $this->buildTypeMap(); |
||
| 233 | $this->buildImplementations(); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * |
||
| 238 | */ |
||
| 239 | protected function buildTypeMap(): void |
||
| 240 | { |
||
| 241 | $initialTypes = [ |
||
| 242 | $this->query, |
||
| 243 | $this->mutation, |
||
| 244 | $this->subscription, |
||
| 245 | __Schema(), // Introspection |
||
| 246 | ]; |
||
| 247 | |||
| 248 | if ($this->types) { |
||
| 249 | $initialTypes = \array_merge($initialTypes, $this->types); |
||
| 250 | } |
||
| 251 | |||
| 252 | // Keep track of all types referenced within the schema. |
||
| 253 | $typeMap = []; |
||
| 254 | |||
| 255 | // First by deeply visiting all initial types. |
||
| 256 | $typeMap = \array_reduce($initialTypes, [$this, 'typeMapReducer'], $typeMap); |
||
| 257 | |||
| 258 | // Then by deeply visiting all directive types. |
||
| 259 | $typeMap = \array_reduce($this->directives, [$this, 'typeMapDirectiveReducer'], $typeMap); |
||
| 260 | |||
| 261 | // Storing the resulting map for reference by the schema. |
||
| 262 | $this->typeMap = $typeMap; |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @throws InvariantException |
||
| 267 | */ |
||
| 268 | protected function buildImplementations() |
||
| 269 | { |
||
| 270 | $implementations = []; |
||
| 271 | |||
| 272 | // Keep track of all implementations by interface name. |
||
| 273 | foreach ($this->typeMap as $typeName => $type) { |
||
| 274 | if ($type instanceof ObjectType) { |
||
| 275 | foreach ($type->getInterfaces() as $interface) { |
||
| 276 | if (!($interface instanceof InterfaceType)) { |
||
| 277 | continue; |
||
| 278 | } |
||
| 279 | |||
| 280 | $interfaceName = $interface->getName(); |
||
| 281 | |||
| 282 | if (!isset($implementations[$interfaceName])) { |
||
| 283 | $implementations[$interfaceName] = []; |
||
| 284 | } |
||
| 285 | |||
| 286 | $implementations[$interfaceName][] = $type; |
||
| 287 | } |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | $this->implementations = $implementations; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param TypeInterface|null $query |
||
| 296 | * @return Schema |
||
| 297 | */ |
||
| 298 | protected function setQuery(?TypeInterface $query): Schema |
||
| 299 | { |
||
| 300 | $this->query = $query; |
||
| 301 | return $this; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param TypeInterface|null $mutation |
||
| 306 | * @return Schema |
||
| 307 | */ |
||
| 308 | protected function setMutation(?TypeInterface $mutation): Schema |
||
| 309 | { |
||
| 310 | $this->mutation = $mutation; |
||
| 311 | return $this; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param TypeInterface|null $subscription |
||
| 316 | * @return Schema |
||
| 317 | */ |
||
| 318 | protected function setSubscription(?TypeInterface $subscription): Schema |
||
| 319 | { |
||
| 320 | $this->subscription = $subscription; |
||
| 321 | return $this; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param array $types |
||
| 326 | * @return Schema |
||
| 327 | */ |
||
| 328 | protected function setTypes(array $types): Schema |
||
| 329 | { |
||
| 330 | $this->types = $types; |
||
| 331 | return $this; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param DirectiveInterface[] $directives |
||
| 336 | * @return Schema |
||
| 337 | */ |
||
| 338 | protected function setDirectives(array $directives): Schema |
||
| 339 | { |
||
| 340 | $this->directives = $directives; |
||
| 341 | return $this; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param bool $assumeValid |
||
| 346 | * @return Schema |
||
| 347 | */ |
||
| 348 | protected function setAssumeValid(bool $assumeValid): Schema |
||
| 349 | { |
||
| 350 | $this->assumeValid = $assumeValid; |
||
| 351 | return $this; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param array $map |
||
| 356 | * @param TypeInterface|NameAwareInterface|null $type |
||
| 357 | * @return array |
||
| 358 | * @throws InvariantException |
||
| 359 | */ |
||
| 360 | protected function typeMapReducer(array $map, ?TypeInterface $type): array |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Note: We do not type-hint the `$directive`, because we want the `SchemaValidator` to catch these errors. |
||
| 421 | * |
||
| 422 | * @param array $map |
||
| 423 | * @param mixed|null $directive |
||
| 424 | * @return array |
||
| 425 | */ |
||
| 426 | protected function typeMapDirectiveReducer(array $map, $directive): array |
||
| 427 | { |
||
| 428 | if (!($directive instanceof Directive) || |
||
| 429 | ($directive instanceof Directive && !$directive->hasArguments())) { |
||
| 430 | return $map; |
||
| 431 | } |
||
| 432 | |||
| 433 | return \array_reduce($directive->getArguments(), function ($map, Argument $argument) { |
||
| 436 | } |
||
| 437 | |||
| 438 | } |
||
| 439 |