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