| Conditions | 18 |
| Paths | 384 |
| Total Lines | 115 |
| Code Lines | 71 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |
||
| 435 | } |