Complex classes like MetadataGrapher often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MetadataGrapher, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class MetadataGrapher |
||
15 | { |
||
16 | /** |
||
17 | * Temporary array where already visited collections are stored |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $visitedAssociations = []; |
||
22 | |||
23 | /** |
||
24 | * @var \Doctrine\Common\Persistence\Mapping\ClassMetadata[] |
||
25 | */ |
||
26 | private $metadata; |
||
27 | |||
28 | /** |
||
29 | * Temporary array where reverse association name are stored |
||
30 | * |
||
31 | * @var \Doctrine\Common\Persistence\Mapping\ClassMetadata[] |
||
32 | */ |
||
33 | private $classByNames = []; |
||
34 | |||
35 | /** |
||
36 | * Generate a YUML compatible `dsl_text` to describe a given array |
||
37 | * of entities |
||
38 | * |
||
39 | * @param $metadata \Doctrine\Common\Persistence\Mapping\ClassMetadata[] |
||
40 | * |
||
41 | * @return string |
||
42 | */ |
||
43 | 21 | public function generateFromMetadata(array $metadata) |
|
77 | |||
78 | /** |
||
79 | * @param ClassMetadata $class1 |
||
80 | * @param string $association |
||
81 | * @return string |
||
82 | */ |
||
83 | 17 | private function getAssociationString(ClassMetadata $class1, $association) |
|
125 | |||
126 | /** |
||
127 | * @param ClassMetadata $class1 |
||
128 | * @param ClassMetadata $class2 |
||
129 | * @return string|null |
||
130 | */ |
||
131 | 15 | private function getClassReverseAssociationName(ClassMetadata $class1, ClassMetadata $class2) |
|
142 | |||
143 | /** |
||
144 | * Build the string representing the single graph item |
||
145 | * |
||
146 | * @param ClassMetadata $class |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | 21 | private function getClassString(ClassMetadata $class) |
|
180 | |||
181 | /** |
||
182 | * Retrieve a class metadata instance by name from the given array |
||
183 | * |
||
184 | * @param string $className |
||
185 | * |
||
186 | * @return ClassMetadata|null |
||
187 | */ |
||
188 | 19 | private function getClassByName($className) |
|
189 | { |
||
190 | 19 | if (! isset($this->classByNames[$className])) { |
|
191 | 19 | foreach ($this->metadata as $class) { |
|
192 | 19 | if ($class->getName() === $className) { |
|
193 | 18 | $this->classByNames[$className] = $class; |
|
194 | 18 | break; |
|
195 | } |
||
196 | 19 | } |
|
197 | 19 | } |
|
198 | |||
199 | 19 | return $this->classByNames[$className] ?? null; |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * Retrieve a class metadata's parent class metadata |
||
204 | * |
||
205 | * @param ClassMetadata $class |
||
206 | * |
||
207 | * @return ClassMetadata|null |
||
208 | */ |
||
209 | 21 | private function getParent($class) |
|
219 | |||
220 | /** |
||
221 | * Visit a given association and mark it as visited |
||
222 | * |
||
223 | * @param string $className |
||
224 | * @param string|null $association |
||
225 | * |
||
226 | * @return bool true if the association was visited before |
||
227 | */ |
||
228 | 21 | private function visitAssociation($className, $association = null) |
|
252 | } |
||
253 |