| Conditions | 15 |
| Paths | 386 |
| Total Lines | 95 |
| Code Lines | 52 |
| 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 |
||
| 22 | public function render(array $schema): string |
||
| 23 | { |
||
| 24 | $class = new ClassDiagram(); |
||
| 25 | $relationMapper = new RelationMapper(); |
||
| 26 | |||
| 27 | foreach ($schema as $key => $value) { |
||
| 28 | if (!isset($value[SchemaInterface::COLUMNS])) { |
||
| 29 | continue; |
||
| 30 | } |
||
| 31 | |||
| 32 | $role = $key; |
||
| 33 | |||
| 34 | if (class_exists($role)) { |
||
| 35 | $role = $value[SchemaInterface::ROLE]; |
||
| 36 | } |
||
| 37 | |||
| 38 | if (strpos($role, ':') !== false) { |
||
| 39 | $role = $key; |
||
| 40 | } |
||
| 41 | |||
| 42 | $table = new EntityTable($role); |
||
| 43 | $arrow = new EntityArrow(); |
||
| 44 | |||
| 45 | foreach ($value[SchemaInterface::COLUMNS] as $column) { |
||
| 46 | $typecast = $this->formatTypecast($value[SchemaInterface::TYPECAST][$column] ?? 'string'); |
||
| 47 | |||
| 48 | $table->addRow($typecast, $column); |
||
| 49 | } |
||
| 50 | |||
| 51 | foreach ($value[SchemaInterface::RELATIONS] ?? [] as $relationKey => $relation) { |
||
| 52 | if (!isset($relation[Relation::TARGET], $relation[Relation::SCHEMA])) { |
||
| 53 | continue; |
||
| 54 | } |
||
| 55 | |||
| 56 | $target = $relation[Relation::TARGET]; |
||
| 57 | |||
| 58 | if (class_exists($target)) { |
||
| 59 | $target = lcfirst($this->getClassShortName($target)); |
||
| 60 | } |
||
| 61 | |||
| 62 | $isNullable = $relation[Relation::SCHEMA][Relation::NULLABLE] ?? false; |
||
| 63 | |||
| 64 | $mappedRelation = $relationMapper->mapWithNode($relation[Relation::TYPE], $isNullable); |
||
| 65 | |||
| 66 | switch ($relation[Relation::TYPE]) { |
||
| 67 | case Relation::MANY_TO_MANY: |
||
| 68 | $throughEntity = $relation[Relation::SCHEMA][Relation::THROUGH_ENTITY] ?? null; |
||
| 69 | |||
| 70 | if ($throughEntity) { |
||
| 71 | if (class_exists($throughEntity)) { |
||
| 72 | $throughEntity = lcfirst($this->getClassShortName($throughEntity)); |
||
| 73 | } |
||
| 74 | |||
| 75 | $table->addMethod($relationKey, sprintf(self::METHOD_FORMAT, $mappedRelation[0], $target)); |
||
| 76 | |||
| 77 | // tag --* post : posts |
||
| 78 | $arrow->addArrow($role, $target, $relationKey, $mappedRelation[1]); |
||
| 79 | // postTag ..> tag : tag.posts |
||
| 80 | $arrow->addArrow($throughEntity, $role, "$role.$relationKey", '..>'); |
||
| 81 | // postTag ..> post : tag.posts |
||
| 82 | $arrow->addArrow($throughEntity, $target, "$role.$relationKey", '..>'); |
||
| 83 | } |
||
| 84 | break; |
||
| 85 | case Relation::EMBEDDED: |
||
| 86 | // explode string like user:credentials |
||
| 87 | $methodTarget = explode(':', $target); |
||
| 88 | |||
| 89 | $table->addMethod( |
||
| 90 | $methodTarget[1] ?? $target, |
||
| 91 | sprintf(self::METHOD_FORMAT, $mappedRelation[0], $relationKey) |
||
| 92 | ); |
||
| 93 | |||
| 94 | $arrowTarget = str_replace(':', ':', $target); |
||
| 95 | |||
| 96 | $arrow->addArrow($role, $relationKey, $arrowTarget, $mappedRelation[1]); |
||
| 97 | break; |
||
| 98 | default: |
||
| 99 | $table->addMethod($relationKey, sprintf(self::METHOD_FORMAT, $mappedRelation[0], $target)); |
||
| 100 | $arrow->addArrow($role, $target, $relationKey, $mappedRelation[1]); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | foreach ($value[SchemaInterface::CHILDREN] ?? [] as $children) { |
||
| 105 | $arrow->addArrow($role, $children, 'STI', '--|>'); |
||
| 106 | } |
||
| 107 | |||
| 108 | if (isset($value[SchemaInterface::PARENT])) { |
||
| 109 | $arrow->addArrow($value[SchemaInterface::PARENT], $role, 'JTI', '--|>'); |
||
| 110 | } |
||
| 111 | |||
| 112 | $class->addEntity($table); |
||
| 113 | $class->addEntity($arrow); |
||
| 114 | } |
||
| 115 | |||
| 116 | return (string)$class; |
||
| 117 | } |
||
| 149 |