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