| Total Complexity | 62 |
| Total Lines | 379 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Complex classes like OpenApiSchemaGenerator 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 OpenApiSchemaGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class OpenApiSchemaGenerator |
||
| 19 | { |
||
| 20 | private const MAX_RECURSION = 2; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var DynamicSchemaInterface[] |
||
| 24 | */ |
||
| 25 | private $schemaGenerators; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var Schema[] |
||
| 29 | */ |
||
| 30 | private $predefined = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var bool[] |
||
| 34 | */ |
||
| 35 | private $building = []; |
||
| 36 | /** |
||
| 37 | * @var ObjectAccessInterface |
||
| 38 | */ |
||
| 39 | private $objectAccess; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var NameConverterInterface |
||
| 43 | */ |
||
| 44 | private $nameConverter; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var ClassMetadataFactoryInterface |
||
| 48 | */ |
||
| 49 | private $classMetadataFactory; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var Schema[] |
||
| 53 | */ |
||
| 54 | protected $alreadyDefined = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var int |
||
| 58 | */ |
||
| 59 | protected $oldRecursion = -1; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param DynamicSchemaInterface[] $schemaGenerators |
||
| 63 | * @param ObjectAccessInterface $objectAccess |
||
| 64 | * @param ClassMetadataFactoryInterface $classMetadataFactory |
||
| 65 | * @param NameConverterInterface $nameConverter |
||
| 66 | */ |
||
| 67 | public function __construct( |
||
| 68 | array $schemaGenerators, |
||
| 69 | ObjectAccessInterface $objectAccess, |
||
| 70 | ClassMetadataFactoryInterface $classMetadataFactory, |
||
| 71 | NameConverterInterface $nameConverter |
||
| 72 | ) { |
||
| 73 | $this->schemaGenerators = $schemaGenerators; |
||
| 74 | $this->objectAccess = $objectAccess; |
||
| 75 | $this->nameConverter = $nameConverter; |
||
| 76 | $this->classMetadataFactory = $classMetadataFactory; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Define a resource class and Schema manually. |
||
| 81 | * @param string $resourceClass |
||
| 82 | * @param Schema $schema |
||
| 83 | * @return OpenApiSchemaGenerator |
||
| 84 | */ |
||
| 85 | public function defineSchemaForResource(string $resourceClass, Schema $schema) |
||
| 86 | { |
||
| 87 | $this->predefined[$resourceClass] = $schema; |
||
| 88 | $this->alreadyDefined = []; |
||
| 89 | |||
| 90 | return $this; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Creates a Schema for specific resource class. |
||
| 95 | * |
||
| 96 | * @param string $resourceClass |
||
| 97 | * @param string $operation |
||
| 98 | * @param array $groups |
||
| 99 | * @return Schema |
||
| 100 | */ |
||
| 101 | public function createSchema(string $resourceClass, string $operation, array $groups): Schema |
||
| 102 | { |
||
| 103 | return unserialize(serialize($this->createSchemaRecursive($resourceClass, $operation, $groups, $this->oldRecursion + 1))); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Creates a unique cache key to be used for already defined schemas for performance reasons. |
||
| 108 | * |
||
| 109 | * @param string $resourceClass |
||
| 110 | * @param string $operation |
||
| 111 | * @param string[] $groups |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | private function getCacheKey(string $resourceClass, string $operation, array $groups) |
||
| 115 | { |
||
| 116 | return $resourceClass . ',' . $operation . ',' . implode(', ', $groups); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Iterate over a list of callbacks to see if they provide a schema for this resource class. |
||
| 121 | * |
||
| 122 | * @param string $cacheKey |
||
| 123 | * @param string $resourceClass |
||
| 124 | * @param string $operation |
||
| 125 | * @param array $groups |
||
| 126 | * @param int $recursion |
||
| 127 | * |
||
| 128 | * @return Schema|null |
||
| 129 | */ |
||
| 130 | private function runCallbacks(string $cacheKey, string $resourceClass, string $operation, array $groups, int $recursion): ?Schema |
||
| 131 | { |
||
| 132 | if (!empty($this->building[$cacheKey])) { |
||
| 133 | return null; |
||
| 134 | } |
||
| 135 | $this->building[$cacheKey] = true; |
||
| 136 | $oldValue = $this->oldRecursion; |
||
| 137 | try { |
||
| 138 | // specifically defined: just call it. |
||
| 139 | if (isset($this->schemaGenerators[$resourceClass])) { |
||
| 140 | return $this->schemaGenerators[$resourceClass]($resourceClass, $operation, $groups, $recursion, $this); |
||
| 141 | } |
||
| 142 | foreach ($this->schemaGenerators as $classDeclaration => $callable) { |
||
| 143 | if (is_a($resourceClass, $classDeclaration, true)) { |
||
| 144 | $res = $callable($resourceClass, $operation, $groups, $recursion, $this); |
||
| 145 | if ($res instanceof Schema) { |
||
| 146 | return $res; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | return null; |
||
| 151 | } finally { |
||
| 152 | $this->oldRecursion = $oldValue; |
||
| 153 | unset($this->building[$cacheKey]); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | private function createSchemaRecursive(string $resourceClass, string $operation, array $groups, int $recursion = 0): Schema |
||
| 158 | { |
||
| 159 | $cacheKey = $this->getCacheKey($resourceClass, $operation, $groups) . ',' . $recursion; |
||
| 160 | if (isset($this->alreadyDefined[$cacheKey])) { |
||
| 161 | return $this->alreadyDefined[$cacheKey]; |
||
| 162 | } |
||
| 163 | |||
| 164 | foreach ($this->predefined as $className => $schema) { |
||
| 165 | if (is_a($resourceClass, $className, true)) { |
||
| 166 | $this->alreadyDefined[$cacheKey] = $schema; |
||
| 167 | |||
| 168 | return $this->alreadyDefined[$cacheKey]; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | if ($predefinedSchema = $this->runCallbacks($cacheKey, $resourceClass, $operation, $groups, $recursion)) { |
||
| 173 | return $this->alreadyDefined[$cacheKey] = $predefinedSchema; |
||
| 174 | } |
||
| 175 | $refl = new ReflectionClass($resourceClass); |
||
| 176 | $schema = SchemaFactory::createObjectSchemaWithoutProperties($refl, $operation, $groups); |
||
| 177 | |||
| 178 | // if definition is an interface or abstract base class it is possible that it has additional properties. |
||
| 179 | if ($refl->isAbstract() || $refl->isInterface()) { |
||
| 180 | $schema->additionalProperties = true; |
||
| 181 | } |
||
| 182 | if ($recursion > self::MAX_RECURSION) { |
||
| 183 | $schema->properties = null; |
||
| 184 | $schema->additionalProperties = true; |
||
| 185 | return $this->alreadyDefined[$cacheKey] = $schema; |
||
| 186 | } |
||
| 187 | $objectAccess = $this->filterObjectAccess($this->objectAccess, $resourceClass, $groups); |
||
| 188 | switch ($operation) { |
||
| 189 | case 'post': |
||
| 190 | $constructorArgs = $objectAccess->getConstructorArguments($refl); |
||
| 191 | foreach ($constructorArgs as $key => $type) { |
||
| 192 | /** @scrutinizer ignore-call */ |
||
| 193 | $fieldName = $this->nameConverter->normalize($key, $resourceClass); |
||
| 194 | $schema->properties[$fieldName] = $this->convertTypeToSchema($type, $operation, $groups, $recursion); |
||
| 195 | $description = $objectAccess->getDescription($refl, $key, false); |
||
| 196 | if ($description) { |
||
| 197 | $schema->properties[$fieldName]->description = $description; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | // FALLTHROUGH |
||
| 201 | case 'put': |
||
| 202 | $setterFields = $objectAccess->getSetterFields($refl); |
||
| 203 | foreach ($setterFields as $setterField) { |
||
| 204 | /** @scrutinizer ignore-call */ |
||
| 205 | $fieldName = $this->nameConverter->normalize($setterField, $resourceClass); |
||
| 206 | $schema->properties[$fieldName] = $this->convertTypesToSchema($objectAccess->getSetterTypes($refl, $setterField), $operation, $groups, $recursion); |
||
| 207 | $description = $objectAccess->getDescription($refl, $setterField, false); |
||
| 208 | if ($description) { |
||
| 209 | $schema->properties[$fieldName]->description = $description; |
||
| 210 | } |
||
| 211 | } |
||
| 212 | break; |
||
| 213 | case 'get': |
||
| 214 | $getterFields = $objectAccess->getGetterFields($refl); |
||
| 215 | foreach ($getterFields as $getterField) { |
||
| 216 | /** @scrutinizer ignore-call */ |
||
| 217 | $fieldName = $this->nameConverter->normalize($getterField, $resourceClass); |
||
| 218 | $schema->properties[$fieldName] = $this->convertTypesToSchema($objectAccess->getGetterTypes($refl, $getterField), $operation, $groups, $recursion); |
||
| 219 | $description = $objectAccess->getDescription($refl, $getterField, true); |
||
| 220 | if ($description) { |
||
| 221 | $schema->properties[$fieldName]->description = $description; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | break; |
||
| 225 | } |
||
| 226 | if (is_array($schema->properties) && empty($schema->properties)) { |
||
| 227 | $schema->properties = null; |
||
| 228 | } |
||
| 229 | return $this->alreadyDefined[$cacheKey] = $schema; |
||
| 230 | } |
||
| 231 | |||
| 232 | private function filterObjectAccess(ObjectAccessInterface $objectAccess, string $className, array $groups): ObjectAccessInterface |
||
| 243 | } |
||
| 244 | |||
| 245 | private function convertTypesToSchema(array $types, string $operation, array $groups, int $recursion = 0): Schema |
||
| 246 | { |
||
| 247 | if (empty($types)) { |
||
| 248 | return SchemaFactory::createAnyTypeSchema(); |
||
| 249 | } |
||
| 250 | $type = reset($types); |
||
| 251 | // this is only because this serializer does not do a deep populate. |
||
| 252 | if ($operation === 'put') { |
||
| 253 | $operation = 'post'; |
||
| 254 | } |
||
| 255 | return $this->convertTypeToSchema($type, $operation, $groups, $recursion); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Returns OpenApi property type for scalars. |
||
| 260 | * |
||
| 261 | * @param string $type |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | private function translateType(string $type): string |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Convert Type into Schema. |
||
| 278 | * |
||
| 279 | * @param Type $type |
||
| 280 | * @param string $operation |
||
| 281 | * @param string[] $groups |
||
| 282 | * @param int $recursion |
||
| 283 | * @internal |
||
| 284 | * |
||
| 285 | * @return Schema |
||
| 286 | */ |
||
| 287 | public function convertTypeToSchema(?Type $type, string $operation, array $groups, int $recursion): Schema |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Define an OpenAPI discriminator spec for an interface or base class that have a discriminator column. |
||
| 355 | * |
||
| 356 | * @param string $resourceInterface |
||
| 357 | * @param string $discriminatorColumn |
||
| 358 | * @param array $subclasses |
||
| 359 | * @param string $operation |
||
| 360 | * @param string[] $groups |
||
| 361 | * @return Schema |
||
| 362 | */ |
||
| 363 | public function defineSchemaForPolymorphicObject( |
||
| 397 | } |
||
| 398 | } |
||
| 399 |