Complex classes like DomToArraySimplifier 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 DomToArraySimplifier, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class DomToArraySimplifier implements SimplifyToArrayInterface |
||
12 | { |
||
13 | public function __construct() |
||
16 | |||
17 | /** |
||
18 | * @param ReflectionClass $dataType |
||
19 | */ |
||
20 | public function canSimplify(\ReflectionClass $dataType) |
||
27 | |||
28 | public function simplifyToArray($structuredData, FormatterOptions $options) |
||
40 | |||
41 | /** |
||
42 | * Recursively convert the provided DOM element into a php array. |
||
43 | * |
||
44 | * @param \DOMNode $element |
||
45 | * @return array |
||
46 | */ |
||
47 | protected function elementToArray(\DOMNode $element) |
||
57 | |||
58 | /** |
||
59 | * Get all of the attributes of the provided element. |
||
60 | * |
||
61 | * @param \DOMNode $element |
||
62 | * @return array |
||
63 | */ |
||
64 | protected function getNodeAttributes($element) |
||
75 | |||
76 | /** |
||
77 | * Get all of the children of the provided element, with simplification. |
||
78 | * |
||
79 | * @param \DOMNode $element |
||
80 | * @return array |
||
81 | */ |
||
82 | protected function getNodeChildren($element) |
||
95 | |||
96 | /** |
||
97 | * Get the data from the children of the provided node in preliminary |
||
98 | * form. |
||
99 | * |
||
100 | * @param \DOMNode $element |
||
101 | * @return array |
||
102 | */ |
||
103 | protected function getNodeChildrenData($element) |
||
111 | |||
112 | /** |
||
113 | * Determine whether the children of the provided element are uniform. |
||
114 | * @see getUniformChildren(), below. |
||
115 | * |
||
116 | * @param \DOMNode $element |
||
117 | * @return boolean |
||
118 | */ |
||
119 | protected function hasUniformChildren($element) |
||
134 | |||
135 | /** |
||
136 | * Convert the children of the provided DOM element into an array. |
||
137 | * Here, 'uniform' means that all of the element names of the children |
||
138 | * are identical, and further, the element name of the parent is the |
||
139 | * plural form of the child names. When the children are uniform in |
||
140 | * this way, then the parent element name will be used as the key to |
||
141 | * store the children in, and the child list will be returned as a |
||
142 | * simple list with their (duplicate) element names omitted. |
||
143 | * |
||
144 | * @param string $parentKey |
||
145 | * @param \DOMNode $element |
||
146 | * @return array |
||
147 | */ |
||
148 | protected function getUniformChildren($parentKey, $element) |
||
165 | |||
166 | /** |
||
167 | * Determine whether the provided value has additional unnecessary |
||
168 | * nesting. {"color": "red"} is converted to "red". No other |
||
169 | * simplification is done. |
||
170 | * |
||
171 | * @param \DOMNode $value |
||
172 | * @return boolean |
||
173 | */ |
||
174 | protected function valueCanBeSimplified($value) |
||
185 | |||
186 | /** |
||
187 | * If the object has an 'id' or 'name' element, then use that |
||
188 | * as the array key when storing this value in its parent. |
||
189 | * @param mixed $value |
||
190 | * @return string |
||
191 | */ |
||
192 | protected function getIdOfValue($value) |
||
204 | |||
205 | /** |
||
206 | * Convert the children of the provided DOM element into an array. |
||
207 | * Here, 'unique' means that all of the element names of the children are |
||
208 | * different. Since the element names will become the key of the |
||
209 | * associative array that is returned, so duplicates are not supported. |
||
210 | * If there are any duplicates, then an exception will be thrown. |
||
211 | * |
||
212 | * @param string $parentKey |
||
213 | * @param \DOMNode $element |
||
214 | * @return array |
||
215 | */ |
||
216 | protected function getUniqueChildren($parentKey, $element) |
||
236 | } |
||
237 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.