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 with numeric keys. |
||
| 149 | * |
||
| 150 | * @param DOMElement $element |
||
| 151 | * @param string|string[] $value |
||
| 152 | */ |
||
| 153 | protected function addNumericNode(DOMElement $element, $value) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Add node. |
||
| 162 | * |
||
| 163 | * @param DOMElement $element |
||
| 164 | * @param string $key |
||
| 165 | * @param string|string[] $value |
||
| 166 | */ |
||
| 167 | protected function addNode(DOMElement $element, $key, $value) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Add collection node. |
||
| 180 | * |
||
| 181 | * @param DOMElement $element |
||
| 182 | * @param string|string[] $value |
||
| 183 | * |
||
| 184 | * @internal param string $key |
||
| 185 | */ |
||
| 186 | protected function addCollectionNode(DOMElement $element, $value) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Add sequential node. |
||
| 201 | * |
||
| 202 | * @param DOMElement $element |
||
| 203 | * @param string|string[] $value |
||
| 204 | * |
||
| 205 | * @internal param string $key |
||
| 206 | */ |
||
| 207 | protected function addSequentialNode(DOMElement $element, $value) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Check if array are all sequential. |
||
| 222 | * |
||
| 223 | * @param array|string $value |
||
| 224 | * |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | protected function isArrayAllKeySequential($value) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Add attributes. |
||
| 246 | * |
||
| 247 | * @param DOMElement $element |
||
| 248 | * @param string[] $data |
||
| 249 | */ |
||
| 250 | protected function addAttributes($element, $data) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Create the root element. |
||
| 259 | * |
||
| 260 | * @param string|array $rootElement |
||
| 261 | * @return DOMElement |
||
| 262 | */ |
||
| 263 | protected function createRootElement($rootElement) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param $valuet |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | protected function removeControlCharacters($value) |
||
| 294 | } |
||
| 295 |
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.