Total Complexity | 86 |
Total Lines | 424 |
Duplicated Lines | 0 % |
Changes | 1 | ||
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 |
||
10 | class TypeBuilder |
||
11 | { |
||
12 | const EXAMPLES = 'examples'; |
||
13 | const EXAMPLE = 'example'; |
||
14 | |||
15 | /** @var \SplObjectStorage */ |
||
16 | private $processed; |
||
17 | |||
18 | public $trimNamePrefix = [ |
||
19 | '#/definitions' |
||
20 | ]; |
||
21 | |||
22 | public $addNamePrefix = ''; |
||
23 | |||
24 | public $file = ''; |
||
25 | |||
26 | public function __construct() |
||
27 | { |
||
28 | $this->processed = new \SplObjectStorage(); |
||
29 | } |
||
30 | |||
31 | |||
32 | /** |
||
33 | * @param Schema|boolean|null $schema |
||
34 | * @param string $path |
||
35 | * @return string |
||
36 | */ |
||
37 | public function getTypeString($schema, $path = '') |
||
241 | } |
||
242 | |||
243 | private function typeName(Schema $schema, $path, $raw = false) |
||
244 | { |
||
245 | if ($fromRefs = $schema->getFromRefs()) { |
||
246 | $path = $fromRefs[count($fromRefs) - 1]; |
||
247 | } |
||
248 | |||
249 | foreach ($this->trimNamePrefix as $prefix) { |
||
250 | if ($prefix === substr($path, 0, strlen($prefix))) { |
||
251 | $path = substr($path, strlen($prefix)); |
||
252 | } |
||
253 | } |
||
254 | |||
255 | if (($path === '#' || empty($path)) && !empty($schema->title)) { |
||
256 | $path = $schema->title; |
||
257 | } |
||
258 | |||
259 | $name = PhpCode::makePhpName($this->addNamePrefix . '_' . $path, false); |
||
260 | |||
261 | if ($raw) { |
||
262 | return $name; |
||
263 | } |
||
264 | |||
265 | return '[`' . $name . '`](#' . strtolower($name) . ')'; |
||
266 | } |
||
267 | |||
268 | private static function constraints() |
||
269 | { |
||
270 | static $constraints; |
||
271 | |||
272 | if ($constraints === null) { |
||
273 | $names = Schema::names(); |
||
274 | $constraints = [ |
||
275 | $names->multipleOf, |
||
276 | $names->maximum, |
||
277 | $names->exclusiveMaximum, |
||
278 | $names->minimum, |
||
279 | $names->exclusiveMinimum, |
||
280 | $names->maxLength, |
||
281 | $names->minLength, |
||
282 | $names->pattern, |
||
283 | $names->maxItems, |
||
284 | $names->minItems, |
||
285 | $names->uniqueItems, |
||
286 | $names->maxProperties, |
||
287 | $names->minProperties, |
||
288 | $names->format, |
||
289 | ]; |
||
290 | } |
||
291 | |||
292 | return $constraints; |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * @param Schema $schema |
||
297 | */ |
||
298 | private function hasConstraints($schema) |
||
299 | { |
||
300 | foreach (self::constraints() as $name) { |
||
301 | if ($schema->$name !== null) { |
||
302 | return true; |
||
303 | } |
||
304 | } |
||
305 | |||
306 | return false; |
||
307 | } |
||
308 | |||
309 | private function makeTypeDef(Schema $schema, $path) |
||
310 | { |
||
311 | $tn = $this->typeName($schema, $path, true); |
||
312 | $typeName = $this->typeName($schema, $path); |
||
313 | $this->processed->attach($schema, $typeName); |
||
314 | |||
315 | $head = ''; |
||
316 | if (!empty($schema->title) && $schema->title != $tn) { |
||
317 | $head .= $schema->title . "\n"; |
||
318 | } |
||
319 | |||
320 | if (!empty($schema->description)) { |
||
321 | $head .= $schema->description . "\n"; |
||
322 | } |
||
323 | |||
324 | $examples = []; |
||
325 | if (!empty($schema->{self::EXAMPLES})) { |
||
326 | $examples = $schema->{self::EXAMPLES}; |
||
327 | } |
||
328 | |||
329 | if (!empty($schema->{self::EXAMPLE})) { |
||
330 | $examples[] = $schema->{self::EXAMPLE}; |
||
331 | } |
||
332 | |||
333 | if (!empty($examples)) { |
||
334 | $head .= "Example:\n\n"; |
||
335 | foreach ($examples as $example) { |
||
336 | $head .= <<<MD |
||
337 | ```json |
||
338 | $example |
||
339 | ``` |
||
340 | |||
341 | |||
342 | MD; |
||
343 | |||
344 | } |
||
345 | } |
||
346 | |||
347 | $tnl = strtolower($tn); |
||
348 | |||
349 | $res = <<<MD |
||
350 | |||
351 | |||
352 | ### <a id="$tnl"></a>$tn |
||
353 | $head |
||
354 | |||
355 | MD; |
||
356 | |||
357 | |||
358 | $rows = []; |
||
359 | foreach (self::constraints() as $name) { |
||
360 | if ($schema->$name !== null) { |
||
361 | $value = $schema->$name; |
||
362 | |||
363 | if ($value instanceof Schema) { |
||
364 | $value = $this->typeName($value, $path . '/' . $name); |
||
365 | } |
||
366 | |||
367 | $rows [] = [ |
||
368 | 'Constraint' => $name, |
||
369 | 'Value' => $value, |
||
370 | ]; |
||
371 | } |
||
372 | } |
||
373 | $res .= TableRenderer::create(new \ArrayIterator($rows)) |
||
374 | ->stripEmptyColumns() |
||
375 | ->setColDelimiter('|') |
||
376 | ->setHeadRowDelimiter('-') |
||
377 | ->setOutlineVertical(true) |
||
378 | ->setShowHeader(); |
||
379 | |||
380 | $res .= "\n\n"; |
||
381 | |||
382 | $rows = []; |
||
383 | $hasDescription = false; |
||
384 | if (!empty($schema->properties)) { |
||
385 | foreach ($schema->properties as $propertyName => $propertySchema) { |
||
386 | $typeString = $this->getTypeString($propertySchema, $path . '/' . $propertyName); |
||
387 | $desc = $this->description($propertySchema); |
||
388 | if (!empty($desc)) { |
||
389 | $hasDescription = true; |
||
390 | } |
||
391 | $isRequired = false; |
||
392 | if (!empty($schema->required)) { |
||
393 | $isRequired = in_array($propertyName, $schema->required); |
||
394 | } |
||
395 | $rows [] = array( |
||
396 | 'Property' => '`' . $propertyName . '`' . ($isRequired ? ' (required)' : ''), |
||
397 | 'Type' => $typeString, |
||
398 | 'Description' => $desc, |
||
399 | ); |
||
400 | } |
||
401 | |||
402 | if (!$hasDescription) { |
||
403 | foreach ($rows as &$row) { |
||
404 | unset($row['Description']); |
||
405 | } |
||
406 | } |
||
407 | |||
408 | $res .= TableRenderer::create(new \ArrayIterator($rows)) |
||
409 | ->stripEmptyColumns() |
||
410 | ->setColDelimiter('|') |
||
411 | ->setHeadRowDelimiter('-') |
||
412 | ->setOutlineVertical(true) |
||
413 | ->setShowHeader(); |
||
414 | |||
415 | } |
||
416 | |||
417 | $res .= <<<MD |
||
418 | |||
419 | MD; |
||
420 | |||
421 | $this->file .= $res; |
||
422 | |||
423 | return $typeName; |
||
424 | } |
||
425 | |||
426 | private function description(Schema $schema) |
||
434 | } |
||
435 | } |