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