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