Complex classes like ArrayToXml 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 ArrayToXml, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class ArrayToXml |
||
10 | { |
||
11 | /** |
||
12 | * The root DOM Document. |
||
13 | * |
||
14 | * @var DOMDocument |
||
15 | */ |
||
16 | protected $document; |
||
17 | |||
18 | /** |
||
19 | * Set to enable replacing space with underscore. |
||
20 | * |
||
21 | * @var bool |
||
22 | */ |
||
23 | protected $replaceSpacesByUnderScoresInKeyNames = true; |
||
24 | |||
25 | /** |
||
26 | * Prefix for the tags with numeric names. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $numericTagNamePrefix = 'num_'; |
||
31 | |||
32 | /** |
||
33 | * Construct a new instance. |
||
34 | * |
||
35 | * @param string[] $array |
||
36 | * @param string|array $rootElement |
||
37 | * @param bool $replaceSpacesByUnderScoresInKeyNames |
||
38 | * @param string $xmlEncoding |
||
39 | * @param string $xmlVersion |
||
40 | * |
||
41 | * @throws DOMException |
||
42 | */ |
||
43 | public function __construct(array $array, $rootElement = '', $replaceSpacesByUnderScoresInKeyNames = true, $xmlEncoding = null, $xmlVersion = '1.0') |
||
58 | |||
59 | public function setNumericTagNamePrefix(string $prefix) |
||
63 | |||
64 | /** |
||
65 | * Convert the given array to an xml string. |
||
66 | * |
||
67 | * @param string[] $array |
||
68 | * @param string|array $rootElement |
||
69 | * @param bool $replaceSpacesByUnderScoresInKeyNames |
||
70 | * @param string $xmlEncoding |
||
71 | * @param string $xmlVersion |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | public static function convert(array $array, $rootElement = '', $replaceSpacesByUnderScoresInKeyNames = true, $xmlEncoding = null, $xmlVersion = '1.0') |
||
81 | |||
82 | /** |
||
83 | * Return as XML. |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | public function toXml() |
||
91 | |||
92 | /** |
||
93 | * Return as DOM object. |
||
94 | * |
||
95 | * @return DOMDocument |
||
96 | */ |
||
97 | public function toDom() |
||
101 | |||
102 | /** |
||
103 | * Parse individual element. |
||
104 | * |
||
105 | * @param DOMElement $element |
||
106 | * @param string|string[] $value |
||
107 | */ |
||
108 | private function convertElement(DOMElement $element, $value) |
||
146 | |||
147 | /** |
||
148 | * Add node. |
||
149 | * |
||
150 | * @param DOMElement $element |
||
151 | * @param string $key |
||
152 | * @param string|string[] $value |
||
153 | */ |
||
154 | protected function addNode(DOMElement $element, $key, $value) |
||
164 | |||
165 | /** |
||
166 | * Add collection node. |
||
167 | * |
||
168 | * @param DOMElement $element |
||
169 | * @param string|string[] $value |
||
170 | * |
||
171 | * @internal param string $key |
||
172 | */ |
||
173 | protected function addCollectionNode(DOMElement $element, $value) |
||
185 | |||
186 | /** |
||
187 | * Add sequential node. |
||
188 | * |
||
189 | * @param DOMElement $element |
||
190 | * @param string|string[] $value |
||
191 | * |
||
192 | * @internal param string $key |
||
193 | */ |
||
194 | protected function addSequentialNode(DOMElement $element, $value) |
||
206 | |||
207 | /** |
||
208 | * Check if array are all sequential. |
||
209 | * |
||
210 | * @param array|string $value |
||
211 | * |
||
212 | * @return bool |
||
213 | */ |
||
214 | protected function isArrayAllKeySequential($value) |
||
226 | |||
227 | /** |
||
228 | * Add attributes. |
||
229 | * |
||
230 | * @param DOMElement $element |
||
231 | * @param string[] $data |
||
232 | */ |
||
233 | protected function addAttributes($element, $data) |
||
239 | |||
240 | /** |
||
241 | * Create the root element. |
||
242 | * |
||
243 | * @param string|array $rootElement |
||
244 | * @return DOMElement |
||
245 | */ |
||
246 | protected function createRootElement($rootElement) |
||
268 | |||
269 | /** |
||
270 | * @param $valuet |
||
271 | * @return string |
||
272 | */ |
||
273 | protected function removeControlCharacters($value) |
||
277 | } |
||
278 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.