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 = 'numeric_'; |
||
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 | * @param array $domProperties |
||
41 | * |
||
42 | * @throws DOMException |
||
43 | */ |
||
44 | public function __construct(array $array, $rootElement = '', $replaceSpacesByUnderScoresInKeyNames = true, $xmlEncoding = null, $xmlVersion = '1.0', $domProperties = []) |
||
64 | |||
65 | public function setNumericTagNamePrefix(string $prefix) |
||
69 | |||
70 | /** |
||
71 | * Convert the given array to an xml string. |
||
72 | * |
||
73 | * @param array $array |
||
74 | * @param string $rootElement |
||
75 | * @param bool $replaceSpacesByUnderScoresInKeyNames |
||
76 | * @param null $xmlEncoding |
||
77 | * @param string $xmlVersion |
||
78 | * @param array $domProperties |
||
79 | * @return string |
||
80 | * |
||
81 | * @throws DOMException |
||
82 | */ |
||
83 | public static function convert(array $array, $rootElement = '', $replaceSpacesByUnderScoresInKeyNames = true, $xmlEncoding = null, $xmlVersion = '1.0', $domProperties = []) |
||
89 | |||
90 | /** |
||
91 | * Return as XML. |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | public function toXml() |
||
99 | |||
100 | /** |
||
101 | * Return as DOM object. |
||
102 | * |
||
103 | * @return DOMDocument |
||
104 | */ |
||
105 | public function toDom() |
||
109 | |||
110 | /** |
||
111 | * Sets dom properties on $this->document. |
||
112 | * |
||
113 | * @param $domProperties |
||
114 | * @throws \Exception |
||
115 | */ |
||
116 | public function setDomProperties($domProperties) |
||
125 | |||
126 | /** |
||
127 | * Parse individual element. |
||
128 | * |
||
129 | * @param DOMElement $element |
||
130 | * @param string|string[] $value |
||
131 | */ |
||
132 | private function convertElement(DOMElement $element, $value) |
||
170 | |||
171 | /** |
||
172 | * Add node with numeric keys. |
||
173 | * |
||
174 | * @param DOMElement $element |
||
175 | * @param string|string[] $value |
||
176 | */ |
||
177 | protected function addNumericNode(DOMElement $element, $value) |
||
183 | |||
184 | /** |
||
185 | * Add node. |
||
186 | * |
||
187 | * @param DOMElement $element |
||
188 | * @param string $key |
||
189 | * @param string|string[] $value |
||
190 | */ |
||
191 | protected function addNode(DOMElement $element, $key, $value) |
||
201 | |||
202 | /** |
||
203 | * Add collection node. |
||
204 | * |
||
205 | * @param DOMElement $element |
||
206 | * @param string|string[] $value |
||
207 | * |
||
208 | * @internal param string $key |
||
209 | */ |
||
210 | protected function addCollectionNode(DOMElement $element, $value) |
||
222 | |||
223 | /** |
||
224 | * Add sequential node. |
||
225 | * |
||
226 | * @param DOMElement $element |
||
227 | * @param string|string[] $value |
||
228 | * |
||
229 | * @internal param string $key |
||
230 | */ |
||
231 | protected function addSequentialNode(DOMElement $element, $value) |
||
243 | |||
244 | /** |
||
245 | * Check if array are all sequential. |
||
246 | * |
||
247 | * @param array|string $value |
||
248 | * |
||
249 | * @return bool |
||
250 | */ |
||
251 | protected function isArrayAllKeySequential($value) |
||
267 | |||
268 | /** |
||
269 | * Add attributes. |
||
270 | * |
||
271 | * @param DOMElement $element |
||
272 | * @param string[] $data |
||
273 | */ |
||
274 | protected function addAttributes($element, $data) |
||
280 | |||
281 | /** |
||
282 | * Create the root element. |
||
283 | * |
||
284 | * @param string|array $rootElement |
||
285 | * @return DOMElement |
||
286 | */ |
||
287 | protected function createRootElement($rootElement) |
||
309 | |||
310 | /** |
||
311 | * @param $valuet |
||
312 | * @return string |
||
313 | */ |
||
314 | protected function removeControlCharacters($value) |
||
318 | } |
||
319 |
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.