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) |
||
96 | |||
97 | /** |
||
98 | * Get the data from the children of the provided node in preliminary |
||
99 | * form. |
||
100 | * |
||
101 | * @param \DOMNode $element |
||
102 | * @return array |
||
103 | */ |
||
104 | protected function getNodeChildrenData($element) |
||
112 | |||
113 | /** |
||
114 | * Determine whether the children of the provided element are uniform. |
||
115 | * @see getUniformChildren(), below. |
||
116 | * |
||
117 | * @param \DOMNode $element |
||
118 | * @return boolean |
||
119 | */ |
||
120 | protected function hasUniformChildren($element) |
||
135 | |||
136 | /** |
||
137 | * Convert the children of the provided DOM element into an array. |
||
138 | * Here, 'uniform' means that all of the element names of the children |
||
139 | * are identical, and further, the element name of the parent is the |
||
140 | * plural form of the child names. When the children are uniform in |
||
141 | * this way, then the parent element name will be used as the key to |
||
142 | * store the children in, and the child list will be returned as a |
||
143 | * simple list with their (duplicate) element names omitted. |
||
144 | * |
||
145 | * @param string $parentKey |
||
146 | * @param \DOMNode $element |
||
147 | * @return array |
||
148 | */ |
||
149 | protected function getUniformChildren($parentKey, $element) |
||
166 | |||
167 | /** |
||
168 | * Determine whether the provided value has additional unnecessary |
||
169 | * nesting. {"color": "red"} is converted to "red". No other |
||
170 | * simplification is done. |
||
171 | * |
||
172 | * @param \DOMNode $value |
||
173 | * @return boolean |
||
174 | */ |
||
175 | protected function valueCanBeSimplified($value) |
||
186 | |||
187 | /** |
||
188 | * If the object has an 'id' or 'name' element, then use that |
||
189 | * as the array key when storing this value in its parent. |
||
190 | * @param mixed $value |
||
191 | * @return string |
||
192 | */ |
||
193 | protected function getIdOfValue($value) |
||
205 | |||
206 | /** |
||
207 | * Convert the children of the provided DOM element into an array. |
||
208 | * Here, 'unique' means that all of the element names of the children are |
||
209 | * different. Since the element names will become the key of the |
||
210 | * associative array that is returned, so duplicates are not supported. |
||
211 | * If there are any duplicates, then an exception will be thrown. |
||
212 | * |
||
213 | * @param string $parentKey |
||
214 | * @param \DOMNode $element |
||
215 | * @return array |
||
216 | */ |
||
217 | protected function getUniqueChildren($parentKey, $element) |
||
237 | } |
||
238 |