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 |
||
10 | class ArrayToXml |
||
11 | { |
||
12 | /** |
||
13 | * The root DOM Document. |
||
14 | * |
||
15 | * @var DOMDocument |
||
16 | */ |
||
17 | protected $document; |
||
18 | |||
19 | /** |
||
20 | * Set to enable replacing space with underscore. |
||
21 | * |
||
22 | * @var bool |
||
23 | */ |
||
24 | protected $replaceSpacesByUnderScoresInKeyNames = true; |
||
25 | |||
26 | /** |
||
27 | * Prefix for the tags with numeric names. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $numericTagNamePrefix = 'numeric_'; |
||
32 | |||
33 | /** |
||
34 | * Construct a new instance. |
||
35 | * |
||
36 | * @param string[] $array |
||
37 | * @param string|array $rootElement |
||
38 | * @param bool $replaceSpacesByUnderScoresInKeyNames |
||
39 | * @param string $xmlEncoding |
||
40 | * @param string $xmlVersion |
||
41 | * @param array $domProperties |
||
42 | * |
||
43 | * @throws DOMException |
||
44 | */ |
||
45 | public function __construct(array $array, $rootElement = '', $replaceSpacesByUnderScoresInKeyNames = true, $xmlEncoding = null, $xmlVersion = '1.0', $domProperties = []) |
||
65 | |||
66 | public function setNumericTagNamePrefix(string $prefix) |
||
70 | |||
71 | /** |
||
72 | * Convert the given array to an xml string. |
||
73 | * |
||
74 | * @param array $array |
||
75 | * @param string $rootElement |
||
76 | * @param bool $replaceSpacesByUnderScoresInKeyNames |
||
77 | * @param null $xmlEncoding |
||
78 | * @param string $xmlVersion |
||
79 | * @param array $domProperties |
||
80 | * @return string |
||
81 | * |
||
82 | * @throws DOMException |
||
83 | */ |
||
84 | public static function convert(array $array, $rootElement = '', $replaceSpacesByUnderScoresInKeyNames = true, $xmlEncoding = null, $xmlVersion = '1.0', $domProperties = []) |
||
90 | |||
91 | /** |
||
92 | * Return as XML. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | public function toXml() |
||
100 | |||
101 | /** |
||
102 | * Return as DOM object. |
||
103 | * |
||
104 | * @return DOMDocument |
||
105 | */ |
||
106 | public function toDom() |
||
110 | |||
111 | /** |
||
112 | * Ensure valid dom properties |
||
113 | * |
||
114 | * @param array $domProperties |
||
115 | * @throws Exception |
||
116 | */ |
||
117 | protected function ensureValidDomProperties($domProperties) { |
||
124 | |||
125 | /** |
||
126 | * Sets dom properties on $this->document. |
||
127 | * |
||
128 | * @param array $domProperties |
||
129 | * @throws Exception |
||
130 | */ |
||
131 | public function setDomProperties($domProperties) |
||
138 | |||
139 | /** |
||
140 | * Parse individual element. |
||
141 | * |
||
142 | * @param DOMElement $element |
||
143 | * @param string|string[] $value |
||
144 | */ |
||
145 | private function convertElement(DOMElement $element, $value) |
||
183 | |||
184 | /** |
||
185 | * Add node with numeric keys. |
||
186 | * |
||
187 | * @param DOMElement $element |
||
188 | * @param string|string[] $value |
||
189 | */ |
||
190 | protected function addNumericNode(DOMElement $element, $value) |
||
196 | |||
197 | /** |
||
198 | * Add node. |
||
199 | * |
||
200 | * @param DOMElement $element |
||
201 | * @param string $key |
||
202 | * @param string|string[] $value |
||
203 | */ |
||
204 | protected function addNode(DOMElement $element, $key, $value) |
||
214 | |||
215 | /** |
||
216 | * Add collection node. |
||
217 | * |
||
218 | * @param DOMElement $element |
||
219 | * @param string|string[] $value |
||
220 | * |
||
221 | * @internal param string $key |
||
222 | */ |
||
223 | protected function addCollectionNode(DOMElement $element, $value) |
||
235 | |||
236 | /** |
||
237 | * Add sequential node. |
||
238 | * |
||
239 | * @param DOMElement $element |
||
240 | * @param string|string[] $value |
||
241 | * |
||
242 | * @internal param string $key |
||
243 | */ |
||
244 | protected function addSequentialNode(DOMElement $element, $value) |
||
256 | |||
257 | /** |
||
258 | * Check if array are all sequential. |
||
259 | * |
||
260 | * @param array|string $value |
||
261 | * |
||
262 | * @return bool |
||
263 | */ |
||
264 | protected function isArrayAllKeySequential($value) |
||
280 | |||
281 | /** |
||
282 | * Add attributes. |
||
283 | * |
||
284 | * @param DOMElement $element |
||
285 | * @param string[] $data |
||
286 | */ |
||
287 | protected function addAttributes($element, $data) |
||
293 | |||
294 | /** |
||
295 | * Create the root element. |
||
296 | * |
||
297 | * @param string|array $rootElement |
||
298 | * @return DOMElement |
||
299 | */ |
||
300 | protected function createRootElement($rootElement) |
||
322 | |||
323 | /** |
||
324 | * @param $valuet |
||
325 | * @return string |
||
326 | */ |
||
327 | protected function removeControlCharacters($value) |
||
331 | } |
||
332 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.