Conditions | 15 |
Paths | 386 |
Total Lines | 93 |
Code Lines | 51 |
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 | if (class_exists($role)) { |
||
35 | $role = $this->getClassShortName($key); |
||
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 | $table->addRow($value[SchemaInterface::TYPECAST][$column] ?? 'string', $column); |
||
47 | } |
||
48 | |||
49 | foreach ($value[SchemaInterface::RELATIONS] ?? [] as $relationKey => $relation) { |
||
50 | if (!isset($relation[Relation::TARGET], $relation[Relation::SCHEMA])) { |
||
51 | continue; |
||
52 | } |
||
53 | |||
54 | $target = $relation[Relation::TARGET]; |
||
55 | |||
56 | if (class_exists($target)) { |
||
57 | $target = $this->getClassShortName($target); |
||
58 | } |
||
59 | |||
60 | $isNullable = $relation[Relation::SCHEMA][Relation::NULLABLE] ?? false; |
||
61 | |||
62 | $mappedRelation = $relationMapper->mapWithNode($relation[Relation::TYPE], $isNullable); |
||
63 | |||
64 | switch ($relation[Relation::TYPE]) { |
||
65 | case Relation::MANY_TO_MANY: |
||
66 | $throughEntity = $relation[Relation::SCHEMA][Relation::THROUGH_ENTITY] ?? null; |
||
67 | |||
68 | if ($throughEntity) { |
||
69 | if (class_exists($throughEntity)) { |
||
70 | $throughEntity = $this->getClassShortName($throughEntity); |
||
71 | } |
||
72 | |||
73 | $table->addMethod($relationKey, sprintf(self::METHOD_FORMAT, $mappedRelation[0], $target)); |
||
74 | |||
75 | // tag --* post : posts |
||
76 | $arrow->addArrow($role, $target, $relationKey, $mappedRelation[1]); |
||
77 | // postTag ..> tag : tag.posts |
||
78 | $arrow->addArrow($throughEntity, $role, "$role.$relationKey", '..>'); |
||
79 | // postTag ..> post : tag.posts |
||
80 | $arrow->addArrow($throughEntity, $target, "$role.$relationKey", '..>'); |
||
81 | } |
||
82 | break; |
||
83 | case Relation::EMBEDDED: |
||
84 | // explode string like user:credentials |
||
85 | $methodTarget = explode(':', $target); |
||
86 | |||
87 | $table->addMethod( |
||
88 | $methodTarget[1] ?? $target, |
||
89 | sprintf(self::METHOD_FORMAT, $mappedRelation[0], $relationKey) |
||
90 | ); |
||
91 | |||
92 | $arrowTarget = str_replace(':', ':', $target); |
||
93 | |||
94 | $arrow->addArrow($role, $relationKey, $arrowTarget, $mappedRelation[1]); |
||
95 | break; |
||
96 | default: |
||
97 | $table->addMethod($relationKey, sprintf(self::METHOD_FORMAT, $mappedRelation[0], $target)); |
||
98 | $arrow->addArrow($role, $target, $relationKey, $mappedRelation[1]); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | foreach ($value[SchemaInterface::CHILDREN] ?? [] as $children) { |
||
103 | $arrow->addArrow($role, $children, 'STI', '--|>'); |
||
104 | } |
||
105 | |||
106 | if (isset($value[SchemaInterface::PARENT])) { |
||
107 | $arrow->addArrow($value[SchemaInterface::PARENT], $role, 'JTI', '--|>'); |
||
108 | } |
||
109 | |||
110 | $class->addEntity($table); |
||
111 | $class->addEntity($arrow); |
||
112 | } |
||
113 | |||
114 | return (string)$class; |
||
115 | } |
||
124 |