Complex classes like XMLWriter 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 XMLWriter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class XMLWriter extends \XMLWriter { |
||
9 | |||
10 | /** |
||
11 | * @var Namespaces |
||
12 | */ |
||
13 | private $_namespaces; |
||
14 | |||
15 | /** |
||
16 | * @var XMLWriter\NamespaceStack |
||
17 | */ |
||
18 | private $_xmlnsStack; |
||
19 | |||
20 | 11 | public function __construct() { |
|
24 | |||
25 | /** |
||
26 | * register a namespace prefix for the xml reader, it will be used in |
||
27 | * next() and other methods with a tag name argument |
||
28 | * |
||
29 | * @param string $prefix |
||
30 | * @param string $namespaceURI |
||
31 | * @throws \LogicException |
||
32 | */ |
||
33 | 7 | public function registerNamespace(string $prefix, string $namespaceURI) { |
|
36 | |||
37 | /** |
||
38 | * Add the current namespace configuration as xmlns* attributes to the element node. |
||
39 | */ |
||
40 | 1 | public function applyNamespaces() { |
|
41 | 1 | foreach ($this->_namespaces as $prefix => $namespaceURI) { |
|
42 | 1 | $this->writeAttribute( |
|
43 | 1 | empty($prefix) || $prefix === '#default' ? 'xmlns' : 'xmlns:'.$prefix, $namespaceURI |
|
44 | ); |
||
45 | } |
||
46 | 1 | } |
|
47 | |||
48 | /** |
||
49 | * @param string $name |
||
50 | * @return bool |
||
51 | */ |
||
52 | 7 | public function startElement($name) { |
|
57 | |||
58 | /** |
||
59 | * @param string $name |
||
60 | * @param NULL|string $content |
||
61 | * @return bool |
||
62 | */ |
||
63 | 3 | public function writeElement($name, $content = NULL) { |
|
68 | |||
69 | /** |
||
70 | * @param string $prefix |
||
71 | * @param string $name |
||
72 | * @param string $namespaceURI |
||
73 | * @return bool |
||
74 | */ |
||
75 | 10 | public function startElementNS($prefix, $name, $namespaceURI) { |
|
85 | |||
86 | /** |
||
87 | * @param string $prefix |
||
88 | * @param string $name |
||
89 | * @param string $uri |
||
90 | * @param NULL|string $content |
||
91 | * @return bool |
||
92 | */ |
||
93 | 3 | public function writeElementNS($prefix, $name, $uri, $content = NULL) { |
|
99 | |||
100 | /** |
||
101 | * @return bool |
||
102 | */ |
||
103 | 10 | public function endElement() { |
|
107 | |||
108 | /** |
||
109 | * @param string $name |
||
110 | * @return bool |
||
111 | */ |
||
112 | 3 | public function startAttribute($name) { |
|
118 | |||
119 | /** |
||
120 | * @param string $name |
||
121 | * @param string $value |
||
122 | * @return bool |
||
123 | */ |
||
124 | 3 | public function writeAttribute($name, $value) { |
|
130 | |||
131 | /** |
||
132 | * @param string $prefix |
||
133 | * @param string $name |
||
134 | * @param string $uri |
||
135 | * @return bool |
||
136 | */ |
||
137 | 3 | public function startAttributeNS($prefix, $name, $uri) { |
|
146 | |||
147 | /** |
||
148 | * @param string $prefix |
||
149 | * @param string $localName |
||
150 | * @param string $uri |
||
151 | * @param string $content |
||
152 | * @return bool |
||
153 | */ |
||
154 | 5 | public function writeAttributeNS($prefix, $localName, $uri, $content) { |
|
173 | |||
174 | /** |
||
175 | * Write a DOM node |
||
176 | * |
||
177 | * @param \DOMNode|\DOMNode[]|\Traversable $nodes |
||
178 | * @param int $maximumDepth |
||
179 | */ |
||
180 | 3 | public function collapse($nodes, int $maximumDepth = 1000) { |
|
192 | |||
193 | /** |
||
194 | * @param \DOMNode $node |
||
195 | * @param int $maximumDepth |
||
196 | */ |
||
197 | 3 | private function collapseNode(\DOMNode $node, int $maximumDepth) { |
|
233 | } |
||
234 | } |
||
235 |