| Conditions | 11 |
| Paths | 82 |
| Total Lines | 76 |
| Code Lines | 44 |
| 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 = $value[SchemaInterface::ROLE] ?? $key; |
||
| 33 | |||
| 34 | // to avoid mermaid class error (user:credentials) |
||
| 35 | if (str_contains($role, ':')) { |
||
| 36 | $role = $key; |
||
| 37 | } |
||
| 38 | |||
| 39 | $table = new EntityTable($role); |
||
| 40 | $arrow = new EntityArrow(); |
||
| 41 | |||
| 42 | foreach ($value[SchemaInterface::COLUMNS] as $column) { |
||
| 43 | $table->addRow($value[SchemaInterface::TYPECAST][$column] ?? 'string', $column); |
||
| 44 | } |
||
| 45 | |||
| 46 | foreach ($value[SchemaInterface::RELATIONS] ?? [] as $relationKey => $relation) { |
||
| 47 | if (!isset($relation[Relation::TARGET], $relation[Relation::SCHEMA])) { |
||
| 48 | continue; |
||
| 49 | } |
||
| 50 | |||
| 51 | $target = $relation[Relation::TARGET]; |
||
| 52 | $throughEntity = $relation[Relation::SCHEMA][Relation::THROUGH_ENTITY] ?? null; |
||
| 53 | $isNullable = $relation[Relation::SCHEMA][Relation::NULLABLE] ?? false; |
||
| 54 | |||
| 55 | $mappedRelation = $relationMapper->mapWithNode($relation[Relation::TYPE], $isNullable); |
||
| 56 | |||
| 57 | switch ($relation[Relation::TYPE]) { |
||
| 58 | case Relation::MANY_TO_MANY: |
||
| 59 | $table->addMethod($relationKey, sprintf(self::METHOD_FORMAT, $mappedRelation[0], $target)); |
||
| 60 | |||
| 61 | // tag --* post : posts |
||
| 62 | $arrow->addArrow($role, $target, $relationKey, $mappedRelation[1]); |
||
| 63 | // postTag ..> tag : tag.posts |
||
| 64 | $arrow->addArrow($throughEntity, $role, "$role.$relationKey", '..>'); |
||
|
|
|||
| 65 | // postTag ..> post : tag.posts |
||
| 66 | $arrow->addArrow($throughEntity, $target, "$role.$relationKey", '..>'); |
||
| 67 | break; |
||
| 68 | case Relation::EMBEDDED: |
||
| 69 | // explode string like user:credentials |
||
| 70 | $methodTarget = explode(':', $target); |
||
| 71 | $table->addMethod( |
||
| 72 | $methodTarget[1] ?? $target, |
||
| 73 | sprintf(self::METHOD_FORMAT, $mappedRelation[0], $relationKey) |
||
| 74 | ); |
||
| 75 | |||
| 76 | $arrowTarget = str_replace(':', ':', $target); |
||
| 77 | $arrow->addArrow($role, $relationKey, $arrowTarget, $mappedRelation[1]); |
||
| 78 | break; |
||
| 79 | default: |
||
| 80 | $table->addMethod($relationKey, sprintf(self::METHOD_FORMAT, $mappedRelation[0], $target)); |
||
| 81 | $arrow->addArrow($role, $target, $relationKey, $mappedRelation[1]); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | foreach ($value[SchemaInterface::CHILDREN] ?? [] as $children) { |
||
| 86 | $arrow->addArrow($role, $children, 'STI', '--|>'); |
||
| 87 | } |
||
| 88 | |||
| 89 | if (isset($value[SchemaInterface::PARENT])) { |
||
| 90 | $arrow->addArrow($value[SchemaInterface::PARENT], $role, 'JTI', '--|>'); |
||
| 91 | } |
||
| 92 | |||
| 93 | $class->addEntity($table); |
||
| 94 | $class->addEntity($arrow); |
||
| 95 | } |
||
| 96 | |||
| 97 | return (string)$class; |
||
| 98 | } |
||
| 100 |