Total Complexity | 93 |
Total Lines | 472 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like TypeBuilder 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 TypeBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class TypeBuilder |
||
10 | { |
||
11 | const EXAMPLES = 'examples'; |
||
12 | const EXAMPLE = 'example'; |
||
13 | |||
14 | /** @var \SplObjectStorage */ |
||
15 | private $processed; |
||
16 | |||
17 | public $trimNamePrefix = [ |
||
18 | '#/definitions' |
||
19 | ]; |
||
20 | |||
21 | public $addNamePrefix = ''; |
||
22 | |||
23 | /** |
||
24 | * Map of type name to type doc. |
||
25 | * @var array<string,string> |
||
26 | */ |
||
27 | public $types = []; |
||
28 | |||
29 | public $uniqueTypeSchemas = []; |
||
30 | |||
31 | public $file = ''; |
||
32 | |||
33 | public function __construct() |
||
34 | { |
||
35 | $this->processed = new \SplObjectStorage(); |
||
36 | } |
||
37 | |||
38 | |||
39 | /** |
||
40 | * @param Schema|boolean|null $schema |
||
41 | * @param string $path |
||
42 | * @return string |
||
43 | */ |
||
44 | public function getTypeString($schema, $path = '') |
||
45 | { |
||
46 | if ($schema === null) { |
||
47 | return ''; |
||
48 | } |
||
49 | |||
50 | $schema = Schema::unboolSchema($schema); |
||
51 | |||
52 | $isOptional = false; |
||
53 | $isObject = false; |
||
54 | $isArray = false; |
||
55 | $isBoolean = false; |
||
56 | $isString = false; |
||
57 | $isNumber = false; |
||
58 | |||
59 | if ($schema->const !== null) { |
||
60 | return '`' . var_export($schema->const, true) . '`'; |
||
61 | } |
||
62 | |||
63 | if (!empty($schema->enum)) { |
||
64 | $res = ''; |
||
65 | foreach ($schema->enum as $value) { |
||
66 | $res .= '<br>`' . var_export($value, true) . '`, '; |
||
67 | } |
||
68 | return substr($res, 4, -2); |
||
69 | } |
||
70 | |||
71 | if (!empty($schema->getFromRefs())) { |
||
72 | $refs = $schema->getFromRefs(); |
||
73 | $path = $refs[0]; |
||
74 | } |
||
75 | |||
76 | $type = $schema->type; |
||
77 | if ($type === null) { |
||
|
|||
78 | $type = []; |
||
79 | |||
80 | if (!empty($schema->properties) || !empty($schema->additionalProperties) || !empty($schema->patternProperties)) { |
||
81 | $type[] = Schema::OBJECT; |
||
82 | } |
||
83 | |||
84 | if (!empty($schema->items) || !empty($schema->additionalItems)) { |
||
85 | $type[] = Schema::_ARRAY; |
||
86 | } |
||
87 | } |
||
88 | |||
89 | if (!is_array($type)) { |
||
90 | $type = [$type]; |
||
91 | } |
||
92 | |||
93 | $or = []; |
||
94 | |||
95 | if ($schema->oneOf !== null) { |
||
96 | foreach ($schema->oneOf as $i => $item) { |
||
97 | $or[] = $this->getTypeString($item, $path . '/oneOf/' . $i); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | if ($schema->anyOf !== null) { |
||
102 | foreach ($schema->anyOf as $i => $item) { |
||
103 | $or[] = $this->getTypeString($item, $path . '/anyOf/' . $i); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | if ($schema->allOf !== null) { |
||
108 | foreach ($schema->allOf as $i => $item) { |
||
109 | $or[] = $this->getTypeString($item, $path . '/allOf/' . $i); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | if ($schema->then !== null) { |
||
114 | $or[] = $this->getTypeString($schema->then, $path . '/then'); |
||
115 | } |
||
116 | |||
117 | if ($schema->else !== null) { |
||
118 | $or[] = $this->getTypeString($schema->else, $path . '/else'); |
||
119 | } |
||
120 | |||
121 | foreach ($type as $i => $t) { |
||
122 | switch ($t) { |
||
123 | case Schema::NULL: |
||
124 | $isOptional = true; |
||
125 | break; |
||
126 | |||
127 | case Schema::OBJECT: |
||
128 | $isObject = true; |
||
129 | break; |
||
130 | |||
131 | case Schema::_ARRAY: |
||
132 | $isArray = true; |
||
133 | break; |
||
134 | |||
135 | case Schema::NUMBER: |
||
136 | case Schema::INTEGER: |
||
137 | $isNumber = true; |
||
138 | break; |
||
139 | |||
140 | case Schema::STRING: |
||
141 | $isString = true; |
||
142 | break; |
||
143 | |||
144 | case Schema::BOOLEAN: |
||
145 | $isBoolean = true; |
||
146 | break; |
||
147 | |||
148 | } |
||
149 | } |
||
150 | |||
151 | |||
152 | $namedTypeAdded = false; |
||
153 | if (!empty($schema->properties) || $this->hasConstraints($schema)) { |
||
154 | if ($this->processed->contains($schema)) { |
||
155 | $or [] = $this->processed->offsetGet($schema); |
||
156 | $namedTypeAdded = true; |
||
157 | } else { |
||
158 | if ($schema instanceof Schema) { |
||
159 | $typeName = $this->typeName($schema, $path); |
||
160 | $this->makeTypeDef($schema, $path); |
||
161 | |||
162 | $or [] = $typeName; |
||
163 | $namedTypeAdded = true; |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | |||
168 | if ($isObject) { |
||
169 | $typeAdded = false; |
||
170 | |||
171 | if ($namedTypeAdded) { |
||
172 | $typeAdded = true; |
||
173 | } |
||
174 | |||
175 | if ($schema->additionalProperties instanceof Schema) { |
||
176 | $typeName = $this->getTypeString($schema->additionalProperties, $path . '/additionalProperties'); |
||
177 | $or [] = "`Map<String,`$typeName`>`"; |
||
178 | $typeAdded = true; |
||
179 | } |
||
180 | |||
181 | if (!empty($schema->patternProperties)) { |
||
182 | foreach ($schema->patternProperties as $pattern => $propertySchema) { |
||
183 | if ($propertySchema instanceof Schema) { |
||
184 | $typeName = $this->getTypeString($propertySchema, $path . '/patternProperties/' . $pattern); |
||
185 | $or [] = $typeName; |
||
186 | $typeAdded = true; |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | |||
191 | if (!$typeAdded) { |
||
192 | $or [] = '`Object`'; |
||
193 | } |
||
194 | } |
||
195 | |||
196 | if ($isArray) { |
||
197 | $typeAdded = false; |
||
198 | |||
199 | if ($schema->items instanceof Schema) { |
||
200 | $typeName = $this->getTypeString($schema->items, $path . '/items'); |
||
201 | $or [] = "`Array<`$typeName`>`"; |
||
202 | $typeAdded = true; |
||
203 | } |
||
204 | |||
205 | if ($schema->additionalItems instanceof Schema) { |
||
206 | $typeName = $this->getTypeString($schema->additionalItems, $path . '/additionalItems'); |
||
207 | $or [] = "`Array<`$typeName`>`"; |
||
208 | $typeAdded = true; |
||
209 | } |
||
210 | |||
211 | if (!$typeAdded) { |
||
212 | $or [] = '`Array`'; |
||
213 | } |
||
214 | } |
||
215 | |||
216 | if ($isOptional) { |
||
217 | $or [] = '`null`'; |
||
218 | } |
||
219 | |||
220 | if ($isString) { |
||
221 | $or [] = '`String`'; |
||
222 | } |
||
223 | |||
224 | if ($isNumber) { |
||
225 | $or [] = '`Number`'; |
||
226 | } |
||
227 | |||
228 | if ($isBoolean) { |
||
229 | $or [] = '`Boolean`'; |
||
230 | } |
||
231 | |||
232 | if ($schema->format !== null) { |
||
233 | $or [] = 'Format: `' . $schema->format . '`'; |
||
234 | } |
||
235 | |||
236 | $res = ''; |
||
237 | foreach ($or as $item) { |
||
238 | if (!empty($item) && $item !== '*') { |
||
239 | $res .= ', ' . $item; |
||
240 | } |
||
241 | } |
||
242 | |||
243 | if ($res !== '') { |
||
244 | $res = substr($res, 2); |
||
245 | } else { |
||
246 | $res = '`*`'; |
||
247 | } |
||
248 | |||
249 | $res = str_replace('``', '', $res); |
||
250 | |||
251 | return $res; |
||
252 | } |
||
253 | |||
254 | private function typeName(Schema $schema, $path, $raw = false) |
||
255 | { |
||
256 | if ($fromRefs = $schema->getFromRefs()) { |
||
257 | $path = $fromRefs[count($fromRefs) - 1]; |
||
258 | } |
||
259 | |||
260 | foreach ($this->trimNamePrefix as $prefix) { |
||
261 | if ($prefix === substr($path, 0, strlen($prefix))) { |
||
262 | $path = substr($path, strlen($prefix)); |
||
263 | } |
||
264 | } |
||
265 | |||
266 | if (($path === '#' || empty($path)) && !empty($schema->title)) { |
||
267 | $path = $schema->title; |
||
268 | } |
||
269 | |||
270 | $name = PhpCode::makePhpName($this->addNamePrefix . '_' . $path, false); |
||
271 | |||
272 | if ($raw) { |
||
273 | return $name; |
||
274 | } |
||
275 | |||
276 | return '[`' . $name . '`](#' . strtolower($name) . ')'; |
||
277 | } |
||
278 | |||
279 | private static function constraints() |
||
280 | { |
||
281 | static $constraints; |
||
282 | |||
283 | if ($constraints === null) { |
||
284 | $names = Schema::names(); |
||
285 | $constraints = [ |
||
286 | $names->multipleOf, |
||
287 | $names->maximum, |
||
288 | $names->exclusiveMaximum, |
||
289 | $names->minimum, |
||
290 | $names->exclusiveMinimum, |
||
291 | $names->maxLength, |
||
292 | $names->minLength, |
||
293 | $names->pattern, |
||
294 | $names->maxItems, |
||
295 | $names->minItems, |
||
296 | $names->uniqueItems, |
||
297 | $names->maxProperties, |
||
298 | $names->minProperties, |
||
299 | ]; |
||
300 | } |
||
301 | |||
302 | return $constraints; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * @param Schema $schema |
||
307 | */ |
||
308 | private function hasConstraints($schema) |
||
317 | } |
||
318 | |||
319 | public function renderTypeDef(Schema $schema, $typeName, $path) |
||
430 | } |
||
431 | |||
432 | private function makeTypeDef(Schema $schema, $path) |
||
433 | { |
||
434 | $tn = $this->typeName($schema, $path, true); |
||
435 | $typeName = $this->typeName($schema, $path); |
||
436 | $this->processed->attach($schema, $typeName); |
||
437 | |||
438 | $res = $this->renderTypeDef($schema, $tn, $path); |
||
439 | |||
440 | if (isset($this->uniqueTypeSchemas[$res])) { |
||
441 | return $this->uniqueTypeSchemas[$res]; |
||
442 | } |
||
443 | |||
444 | $this->types[$typeName] = $res; |
||
445 | $this->uniqueTypeSchemas[$res] = $typeName; |
||
446 | $this->file .= $res; |
||
447 | |||
448 | return $typeName; |
||
449 | } |
||
450 | |||
451 | public function sortTypes() |
||
452 | { |
||
453 | ksort($this->types); |
||
454 | } |
||
455 | |||
456 | public function tableOfContents() |
||
471 | } |
||
472 | |||
473 | private function description(Schema $schema) |
||
481 | } |
||
482 | } |