Conditions | 13 |
Paths | 98 |
Total Lines | 80 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
23 | public function render(array $schema): string |
||
24 | { |
||
25 | $class = new ClassDiagram(); |
||
26 | $relationMapper = new RelationMapper(); |
||
27 | |||
28 | foreach ($schema as $key => $value) { |
||
29 | if (!isset($value[SchemaInterface::COLUMNS]) || !preg_match(self::REGEX, $key)) { |
||
30 | continue; |
||
31 | } |
||
32 | |||
33 | $role = $value[SchemaInterface::ROLE] ?? ''; |
||
34 | |||
35 | if (!preg_match(self::REGEX, $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 | $isNullable = $relation[Relation::SCHEMA][Relation::NULLABLE] ?? false; |
||
53 | |||
54 | $mappedRelation = $relationMapper->mapWithNode($relation[Relation::TYPE], $isNullable); |
||
55 | |||
56 | switch ($relation[Relation::TYPE]) { |
||
57 | case Relation::MANY_TO_MANY: |
||
58 | $throughEntity = $relation[Relation::SCHEMA][Relation::THROUGH_ENTITY] ?? null; |
||
59 | |||
60 | if ($throughEntity) { |
||
61 | $table->addMethod($relationKey, sprintf(self::METHOD_FORMAT, $mappedRelation[0], $target)); |
||
62 | |||
63 | // tag --* post : posts |
||
64 | $arrow->addArrow($role, $target, $relationKey, $mappedRelation[1]); |
||
65 | // postTag ..> tag : tag.posts |
||
66 | $arrow->addArrow($throughEntity, $role, "$role.$relationKey", '..>'); |
||
67 | // postTag ..> post : tag.posts |
||
68 | $arrow->addArrow($throughEntity, $target, "$role.$relationKey", '..>'); |
||
69 | } |
||
70 | break; |
||
71 | case Relation::EMBEDDED: |
||
72 | // explode string like user:credentials |
||
73 | $methodTarget = explode(':', $target); |
||
74 | |||
75 | $table->addMethod( |
||
76 | $methodTarget[1] ?? $target, |
||
77 | sprintf(self::METHOD_FORMAT, $mappedRelation[0], $relationKey) |
||
78 | ); |
||
79 | |||
80 | $arrowTarget = str_replace(':', ':', $target); |
||
81 | |||
82 | $arrow->addArrow($role, $relationKey, $arrowTarget, $mappedRelation[1]); |
||
83 | break; |
||
84 | default: |
||
85 | $table->addMethod($relationKey, sprintf(self::METHOD_FORMAT, $mappedRelation[0], $target)); |
||
86 | $arrow->addArrow($role, $target, $relationKey, $mappedRelation[1]); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | foreach ($value[SchemaInterface::CHILDREN] ?? [] as $children) { |
||
91 | $arrow->addArrow($role, $children, 'STI', '--|>'); |
||
92 | } |
||
93 | |||
94 | if (isset($value[SchemaInterface::PARENT])) { |
||
95 | $arrow->addArrow($value[SchemaInterface::PARENT], $role, 'JTI', '--|>'); |
||
96 | } |
||
97 | |||
98 | $class->addEntity($table); |
||
99 | $class->addEntity($arrow); |
||
100 | } |
||
101 | |||
102 | return (string)$class; |
||
103 | } |
||
105 |