| Total Complexity | 56 |
| Total Lines | 393 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like RecursiveTypeMapper 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 RecursiveTypeMapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class RecursiveTypeMapper implements RecursiveTypeMapperInterface |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var TypeMapperInterface |
||
| 32 | */ |
||
| 33 | private $typeMapper; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * An array mapping a class name to the MappedClass instance (useful to know if the class has children) |
||
| 37 | * |
||
| 38 | * @var array<string,MappedClass>|null |
||
| 39 | */ |
||
| 40 | private $mappedClasses; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * An array of interfaces OR object types if no interface matching. |
||
| 44 | * |
||
| 45 | * @var array<string,OutputType&Type> |
||
| 46 | */ |
||
| 47 | private $interfaces = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array<string,MutableObjectType> Key: FQCN |
||
| 51 | */ |
||
| 52 | private $classToTypeCache = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var NamingStrategyInterface |
||
| 56 | */ |
||
| 57 | private $namingStrategy; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var CacheInterface |
||
| 61 | */ |
||
| 62 | private $cache; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var int|null |
||
| 66 | */ |
||
| 67 | private $ttl; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var array<string, string> An array mapping a GraphQL interface name to the PHP class name that triggered its generation. |
||
| 71 | */ |
||
| 72 | private $interfaceToClassNameMap; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var TypeRegistry |
||
| 76 | */ |
||
| 77 | private $typeRegistry; |
||
| 78 | |||
| 79 | |||
| 80 | public function __construct(TypeMapperInterface $typeMapper, NamingStrategyInterface $namingStrategy, CacheInterface $cache, TypeRegistry $typeRegistry, ?int $ttl = null) |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Returns true if this type mapper can map the $className FQCN to a GraphQL type. |
||
| 91 | * |
||
| 92 | * @param string $className The class name to look for (this function looks into parent classes if the class does not match a type). |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | public function canMapClassToType(string $className): bool |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Maps a PHP fully qualified class name to a GraphQL type. |
||
| 102 | * |
||
| 103 | * @param string $className The class name to look for (this function looks into parent classes if the class does not match a type) |
||
| 104 | * @param (OutputType&MutableObjectType)|(OutputType&InterfaceType)|null $subType An optional sub-type if the main class is an iterator that needs to be typed. |
||
| 105 | * @return MutableObjectType |
||
| 106 | * @throws CannotMapTypeExceptionInterface |
||
| 107 | */ |
||
| 108 | public function mapClassToType(string $className, ?OutputType $subType): MutableObjectType |
||
| 109 | { |
||
| 110 | $cacheKey = $className; |
||
| 111 | if ($subType !== null) { |
||
| 112 | $cacheKey .= '__`__'.$subType->name; |
||
|
|
|||
| 113 | } |
||
| 114 | if (isset($this->classToTypeCache[$cacheKey])) { |
||
| 115 | return $this->classToTypeCache[$cacheKey]; |
||
| 116 | } |
||
| 117 | |||
| 118 | $closestClassName = $this->findClosestMatchingParent($className); |
||
| 119 | if ($closestClassName === null) { |
||
| 120 | throw CannotMapTypeException::createForType($className); |
||
| 121 | } |
||
| 122 | $type = $this->typeMapper->mapClassToType($closestClassName, $subType, $this); |
||
| 123 | |||
| 124 | // In the event this type was already part of cache, let's not extend it. |
||
| 125 | if ($this->typeRegistry->hasType($type->name)) { |
||
| 126 | $cachedType = $this->typeRegistry->getType($type->name); |
||
| 127 | if ($cachedType !== $type) { |
||
| 128 | throw new \RuntimeException('Cached type in registry is not the type returned by type mapper.'); |
||
| 129 | } |
||
| 130 | //if ($cachedType->getStatus() === MutableObjectType::STATUS_FROZEN) { |
||
| 131 | return $type; |
||
| 132 | //} |
||
| 133 | } |
||
| 134 | |||
| 135 | $this->typeRegistry->registerType($type); |
||
| 136 | $this->classToTypeCache[$cacheKey] = $type; |
||
| 137 | |||
| 138 | $this->extendType($className, $type); |
||
| 139 | |||
| 140 | $type->freeze(); |
||
| 141 | |||
| 142 | return $type; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Returns the closest parent that can be mapped, or null if nothing can be matched. |
||
| 147 | * |
||
| 148 | * @param string $className |
||
| 149 | * @return string|null |
||
| 150 | */ |
||
| 151 | private function findClosestMatchingParent(string $className): ?string |
||
| 152 | { |
||
| 153 | do { |
||
| 154 | if ($this->typeMapper->canMapClassToType($className)) { |
||
| 155 | return $className; |
||
| 156 | } |
||
| 157 | } while ($className = get_parent_class($className)); |
||
| 158 | return null; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Extends a type using available type extenders. |
||
| 163 | * |
||
| 164 | * @param string $className |
||
| 165 | * @param MutableObjectType $type |
||
| 166 | * @throws CannotMapTypeExceptionInterface |
||
| 167 | */ |
||
| 168 | private function extendType(string $className, MutableObjectType $type): void |
||
| 169 | { |
||
| 170 | $classes = []; |
||
| 171 | do { |
||
| 172 | if ($this->typeMapper->canExtendTypeForClass($className, $type, $this)) { |
||
| 173 | $classes[] = $className; |
||
| 174 | } |
||
| 175 | } while ($className = get_parent_class($className)); |
||
| 176 | |||
| 177 | // Let's apply extenders from the most basic type. |
||
| 178 | $classes = array_reverse($classes); |
||
| 179 | foreach ($classes as $class) { |
||
| 180 | $this->typeMapper->extendTypeForClass($class, $type, $this); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Maps a PHP fully qualified class name to a GraphQL type. Returns an interface if possible (if the class |
||
| 186 | * has children) or returns an output type otherwise. |
||
| 187 | * |
||
| 188 | * @param string $className The exact class name to look for (this function does not look into parent classes). |
||
| 189 | * @param (OutputType&ObjectType)|(OutputType&InterfaceType)|null $subType A subtype (if the main className is an iterator) |
||
| 190 | * @return OutputType&Type |
||
| 191 | * @throws CannotMapTypeExceptionInterface |
||
| 192 | */ |
||
| 193 | public function mapClassToInterfaceOrType(string $className, ?OutputType $subType): OutputType |
||
| 194 | { |
||
| 195 | $closestClassName = $this->findClosestMatchingParent($className); |
||
| 196 | if ($closestClassName === null) { |
||
| 197 | throw CannotMapTypeException::createForType($className); |
||
| 198 | } |
||
| 199 | $cacheKey = $closestClassName; |
||
| 200 | if ($subType !== null) { |
||
| 201 | $cacheKey .= '__`__'.$subType->name; |
||
| 202 | } |
||
| 203 | if (!isset($this->interfaces[$cacheKey])) { |
||
| 204 | $objectType = $this->mapClassToType($className, $subType); |
||
| 205 | |||
| 206 | $supportedClasses = $this->getClassTree(); |
||
| 207 | if (isset($supportedClasses[$closestClassName]) && !empty($supportedClasses[$closestClassName]->getChildren())) { |
||
| 208 | // Cast as an interface |
||
| 209 | $this->interfaces[$cacheKey] = new InterfaceFromObjectType($this->namingStrategy->getInterfaceNameFromConcreteName($objectType->name), $objectType, $subType, $this); |
||
| 210 | $this->typeRegistry->registerType($this->interfaces[$cacheKey]); |
||
| 211 | } else { |
||
| 212 | $this->interfaces[$cacheKey] = $objectType; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | return $this->interfaces[$cacheKey]; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Build a map mapping GraphQL interface names to the PHP class name of the object creating this interface. |
||
| 220 | * |
||
| 221 | * @return array<string, string> |
||
| 222 | */ |
||
| 223 | private function buildInterfaceToClassNameMap(): array |
||
| 224 | { |
||
| 225 | $map = []; |
||
| 226 | $supportedClasses = $this->getClassTree(); |
||
| 227 | foreach ($supportedClasses as $className => $mappedClass) { |
||
| 228 | if (!empty($mappedClass->getChildren())) { |
||
| 229 | $objectType = $this->mapClassToType($className, null); |
||
| 230 | $interfaceName = $this->namingStrategy->getInterfaceNameFromConcreteName($objectType->name); |
||
| 231 | $map[$interfaceName] = $className; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | return $map; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Returns a map mapping GraphQL interface names to the PHP class name of the object creating this interface. |
||
| 239 | * The map may come from the cache. |
||
| 240 | * |
||
| 241 | * @return array<string, string> |
||
| 242 | */ |
||
| 243 | private function getInterfaceToClassNameMap(): array |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Finds the list of interfaces returned by $className. |
||
| 260 | * |
||
| 261 | * @param string $className |
||
| 262 | * @return InterfaceType[] |
||
| 263 | */ |
||
| 264 | public function findInterfaces(string $className): array |
||
| 265 | { |
||
| 266 | $interfaces = []; |
||
| 267 | while ($className = $this->findClosestMatchingParent($className)) { |
||
| 268 | $type = $this->mapClassToInterfaceOrType($className, null); |
||
| 269 | if ($type instanceof InterfaceType) { |
||
| 270 | $interfaces[] = $type; |
||
| 271 | } |
||
| 272 | $className = get_parent_class($className); |
||
| 273 | if ($className === false) { |
||
| 274 | break; |
||
| 275 | } |
||
| 276 | } |
||
| 277 | return $interfaces; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return array<string,MappedClass> |
||
| 282 | */ |
||
| 283 | private function getClassTree(): array |
||
| 284 | { |
||
| 285 | if ($this->mappedClasses === null) { |
||
| 286 | $supportedClasses = array_flip($this->typeMapper->getSupportedClasses()); |
||
| 287 | foreach ($supportedClasses as $supportedClass => $foo) { |
||
| 288 | $this->getMappedClass($supportedClass, $supportedClasses); |
||
| 289 | } |
||
| 290 | } |
||
| 291 | return $this->mappedClasses; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $className |
||
| 296 | * @param array<string,int> $supportedClasses |
||
| 297 | * @return MappedClass |
||
| 298 | */ |
||
| 299 | private function getMappedClass(string $className, array $supportedClasses): MappedClass |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Returns true if this type mapper can map the $className FQCN to a GraphQL input type. |
||
| 319 | * |
||
| 320 | * @param string $className |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | public function canMapClassToInputType(string $className): bool |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Maps a PHP fully qualified class name to a GraphQL input type. |
||
| 330 | * |
||
| 331 | * @param string $className |
||
| 332 | * @return InputObjectType |
||
| 333 | * @throws CannotMapTypeExceptionInterface |
||
| 334 | */ |
||
| 335 | public function mapClassToInputType(string $className): InputObjectType |
||
| 336 | { |
||
| 337 | return $this->typeMapper->mapClassToInputType($className, $this); |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Returns an array containing all OutputTypes. |
||
| 342 | * Needed for introspection because of interfaces. |
||
| 343 | * |
||
| 344 | * @return array<string, OutputType> |
||
| 345 | */ |
||
| 346 | public function getOutputTypes(): array |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Returns true if this type mapper can map the $typeName GraphQL name to a GraphQL type. |
||
| 357 | * |
||
| 358 | * @param string $typeName The name of the GraphQL type |
||
| 359 | * @return bool |
||
| 360 | */ |
||
| 361 | public function canMapNameToType(string $typeName): bool |
||
| 362 | { |
||
| 363 | $result = $this->typeMapper->canMapNameToType($typeName); |
||
| 364 | if ($result === true) { |
||
| 365 | return true; |
||
| 366 | } |
||
| 367 | |||
| 368 | // Maybe the type is an interface? |
||
| 369 | $interfaceToClassNameMap = $this->getInterfaceToClassNameMap(); |
||
| 370 | if (isset($interfaceToClassNameMap[$typeName])) { |
||
| 371 | return true; |
||
| 372 | } |
||
| 373 | |||
| 374 | return false; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Returns a GraphQL type by name (can be either an input or output type) |
||
| 379 | * |
||
| 380 | * @param string $typeName The name of the GraphQL type |
||
| 381 | * @return Type&(InputType|OutputType) |
||
| 382 | */ |
||
| 383 | public function mapNameToType(string $typeName): Type |
||
| 421 | } |
||
| 422 | } |
||
| 423 |