Conditions | 22 |
Paths | 426 |
Total Lines | 154 |
Code Lines | 98 |
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 |
||
36 | public function render(array $schema): string |
||
37 | { |
||
38 | $class = new ClassDiagram(); |
||
39 | $roleAliasCollection = new RoleAliasCollection($schema); |
||
40 | |||
41 | foreach ($schema as $key => $value) { |
||
42 | if (!isset($value[SchemaInterface::COLUMNS])) { |
||
43 | continue; |
||
44 | } |
||
45 | |||
46 | $role = $roleAliasCollection->getAlias($key); |
||
47 | |||
48 | $entityTable = new EntityTable($role); |
||
49 | $entityRelation = new EntityRelation(); |
||
50 | |||
51 | if (\strpos($key, ':') !== false) { |
||
52 | $entityTable->addAnnotation( |
||
53 | new Annotation($key) |
||
54 | ); |
||
55 | } |
||
56 | |||
57 | foreach ($value[SchemaInterface::COLUMNS] as $column) { |
||
58 | $typecast = $this->formatTypecast($value[SchemaInterface::TYPECAST][$column] ?? 'string'); |
||
59 | |||
60 | $entityTable->addRow( |
||
61 | new Row($typecast, $column) |
||
62 | ); |
||
63 | } |
||
64 | |||
65 | foreach ($value[SchemaInterface::RELATIONS] ?? [] as $relationKey => $relation) { |
||
66 | if (!isset($relation[SchemaRelation::TARGET], $relation[SchemaRelation::SCHEMA])) { |
||
67 | continue; |
||
68 | } |
||
69 | |||
70 | $target = $roleAliasCollection->getAlias($relation[SchemaRelation::TARGET]); |
||
71 | |||
72 | if (\class_exists($target)) { |
||
73 | $target = \lcfirst($this->getClassShortName($target)); |
||
74 | } |
||
75 | |||
76 | $isNullable = $relation[SchemaRelation::SCHEMA][SchemaRelation::NULLABLE] ?? false; |
||
77 | |||
78 | switch ($relation[SchemaRelation::TYPE]) { |
||
79 | case SchemaRelation::HAS_ONE: |
||
80 | $entityTable->addMethod( |
||
81 | new HasOneMethod($relationKey, $target) |
||
82 | ); |
||
83 | $entityRelation->addRelation( |
||
84 | new HasOneRelation($role, $target, $relationKey, $isNullable) |
||
85 | ); |
||
86 | break; |
||
87 | case SchemaRelation::HAS_MANY: |
||
88 | $entityTable->addMethod( |
||
89 | new HasManyMethod($relationKey, $target) |
||
90 | ); |
||
91 | $entityRelation->addRelation( |
||
92 | new HasManyRelation($role, $target, $relationKey, $isNullable) |
||
93 | ); |
||
94 | break; |
||
95 | case SchemaRelation::BELONGS_TO: |
||
96 | $entityTable->addMethod( |
||
97 | new BelongsToMethod($relationKey, $target) |
||
98 | ); |
||
99 | $entityRelation->addRelation( |
||
100 | new BelongsToRelation($role, $target, $relationKey, $isNullable) |
||
101 | ); |
||
102 | break; |
||
103 | case SchemaRelation::MANY_TO_MANY: |
||
104 | $throughEntity = $relation[SchemaRelation::SCHEMA][SchemaRelation::THROUGH_ENTITY] ?? null; |
||
105 | |||
106 | if ($throughEntity !== null) { |
||
107 | if (\class_exists($throughEntity)) { |
||
108 | $throughEntity = \lcfirst($this->getClassShortName($throughEntity)); |
||
109 | } |
||
110 | |||
111 | $entityTable->addMethod( |
||
112 | new ManyToManyMethod($relationKey, $target) |
||
113 | ); |
||
114 | $entityRelation->addRelation( |
||
115 | new ManyToManyRelation($role, $target, $relationKey, $isNullable) |
||
116 | ); |
||
117 | $entityRelation->addRelation( |
||
118 | new Relation($throughEntity, $role, "$role.$relationKey", '..>', $isNullable) |
||
119 | ); |
||
120 | $entityRelation->addRelation( |
||
121 | new Relation($throughEntity, $target, "$role.$relationKey", '..>', $isNullable) |
||
122 | ); |
||
123 | } |
||
124 | break; |
||
125 | case SchemaRelation::REFERS_TO: |
||
126 | $entityTable->addMethod( |
||
127 | new RefersToMethod($relationKey, $target) |
||
128 | ); |
||
129 | $entityRelation->addRelation( |
||
130 | new RefersToRelation($role, $target, $relationKey, $isNullable) |
||
131 | ); |
||
132 | break; |
||
133 | case SchemaRelation::MORPHED_HAS_MANY: |
||
134 | $entityTable->addMethod( |
||
135 | new MorphedHasManyMethod($relationKey, $target) |
||
136 | ); |
||
137 | $entityRelation->addRelation( |
||
138 | new MorphedHasManyRelation($role, $target, $relationKey, $isNullable) |
||
139 | ); |
||
140 | break; |
||
141 | case SchemaRelation::MORPHED_HAS_ONE: |
||
142 | $entityTable->addMethod( |
||
143 | new MorphedHasOneMethod($relationKey, $target) |
||
144 | ); |
||
145 | $entityRelation->addRelation( |
||
146 | new MorphedHasOneRelation($role, $target, $relationKey, $isNullable) |
||
147 | ); |
||
148 | break; |
||
149 | case SchemaRelation::BELONGS_TO_MORPHED: |
||
150 | $entityTable->addMethod( |
||
151 | new BelongsToMorphedMethod($relationKey, $target) |
||
152 | ); |
||
153 | $entityRelation->addRelation( |
||
154 | new BelongsToMorphedRelation($role, $target, $relationKey, $isNullable) |
||
155 | ); |
||
156 | break; |
||
157 | case SchemaRelation::EMBEDDED: |
||
158 | $methodTarget = \explode('_', $target); |
||
159 | $prop = isset($methodTarget[2]) ? $methodTarget[2] . '_prop' : $target; |
||
160 | |||
161 | $entityTable->addMethod( |
||
162 | new EmbeddedMethod($prop, $relation[SchemaRelation::TARGET]) |
||
163 | ); |
||
164 | $entityRelation->addRelation( |
||
165 | new EmbeddedRelation($role, $target, $prop, $isNullable) |
||
166 | ); |
||
167 | break; |
||
168 | default: |
||
169 | throw new \ErrorException(\sprintf('Relation `%s` not found.', $relation[SchemaRelation::TYPE])); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | foreach ($value[SchemaInterface::CHILDREN] ?? [] as $children) { |
||
174 | $entityRelation->addRelation( |
||
175 | new SingleInheritanceRelation($role, $children, false) |
||
176 | ); |
||
177 | } |
||
178 | |||
179 | if (isset($value[SchemaInterface::PARENT])) { |
||
180 | $entityRelation->addRelation( |
||
181 | new JoinedInheritanceRelation($value[SchemaInterface::PARENT], $role, false) |
||
182 | ); |
||
183 | } |
||
184 | |||
185 | $class->addEntity($entityTable); |
||
186 | $class->addEntity($entityRelation); |
||
187 | } |
||
188 | |||
189 | return (string)$class; |
||
190 | } |
||
229 |