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 |
||
16 | class XMLWriter implements XML |
||
17 | { |
||
18 | use IsAssoc; |
||
19 | |||
20 | /** |
||
21 | * @var \XMLWriter |
||
22 | */ |
||
23 | private $XMLWriter; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $currentSitemap; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $workDir; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $domain; |
||
39 | |||
40 | /** |
||
41 | * XMLWriter constructor. |
||
42 | * |
||
43 | * @param array $config |
||
44 | * |
||
45 | * @throws \Exception |
||
46 | */ |
||
47 | public function __construct(array $config) |
||
57 | |||
58 | /** |
||
59 | * @return string |
||
60 | */ |
||
61 | public function getDomain(): string |
||
65 | |||
66 | /** |
||
67 | * @param string $domain |
||
68 | */ |
||
69 | public function setDomain(string $domain): void |
||
73 | |||
74 | /** |
||
75 | * @return string |
||
76 | */ |
||
77 | public function getCurrentSitemap(): string |
||
81 | |||
82 | /** |
||
83 | * @param string $currentSitemap |
||
84 | */ |
||
85 | public function setCurrentSitemap(string $currentSitemap): void |
||
89 | |||
90 | /** |
||
91 | * @param string $sitemap |
||
92 | * @param array $extensions |
||
93 | */ |
||
94 | public function openSitemap(string $sitemap, array $extensions = []): void |
||
109 | |||
110 | /** |
||
111 | * @return \XMLWriter |
||
112 | */ |
||
113 | private function getXMLWriter(): \XMLWriter |
||
117 | |||
118 | /** |
||
119 | * Save from buffer to file |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | private function flushData(): void |
||
127 | |||
128 | /** |
||
129 | * @return string |
||
130 | */ |
||
131 | private function getSitemapFileFullPath(): string |
||
135 | |||
136 | /** |
||
137 | * @return string |
||
138 | */ |
||
139 | public function getWorkDir(): string |
||
143 | |||
144 | /** |
||
145 | * @param string $dir |
||
146 | */ |
||
147 | public function setWorkDir(string $dir): void |
||
151 | |||
152 | /** |
||
153 | * @throws \Exception |
||
154 | */ |
||
155 | public function closeSitemap(): void |
||
162 | |||
163 | /** |
||
164 | * Remove whitespace chars from end of file (Google don't like them) |
||
165 | * |
||
166 | * @return void |
||
167 | * @throws Exception |
||
168 | */ |
||
169 | private function endFile(): void |
||
195 | |||
196 | /** |
||
197 | * @return int |
||
198 | */ |
||
199 | public function getSitemapSize(): int |
||
205 | |||
206 | /** |
||
207 | * @param array $element |
||
208 | */ |
||
209 | public function addUrl(array $element): void |
||
217 | |||
218 | /** |
||
219 | * @param string $element |
||
220 | * @param $value |
||
221 | * @param string|null $namespace |
||
222 | */ |
||
223 | private function addElement(string $element, $value, ?string $namespace = null): void |
||
235 | |||
236 | /** |
||
237 | * @param string $element |
||
238 | * @param $value |
||
239 | * @param string|null $namespace |
||
240 | */ |
||
241 | private function addElementArray(string $element, $value, ?string $namespace = null): void |
||
253 | |||
254 | private function addElementArrayAssoc(string $element, $value, ?string $namespace = null): void |
||
282 | |||
283 | /** |
||
284 | * @param string $element |
||
285 | * @param $value |
||
286 | * @param string|null $namespace |
||
287 | */ |
||
288 | private function addElementArrayNonAssoc(string $element, $value, ?string $namespace = null): void |
||
294 | |||
295 | /** |
||
296 | * @param string $sitemap |
||
297 | */ |
||
298 | public function openSitemapIndex(string $sitemap): void |
||
308 | |||
309 | /** |
||
310 | * @throws \Exception |
||
311 | */ |
||
312 | public function closeSitemapIndex(): void |
||
319 | |||
320 | /** |
||
321 | * @param string $sitemap |
||
322 | * @param string|null $lastmod |
||
323 | */ |
||
324 | public function addSitemap(string $sitemap, string $lastmod = null): void |
||
334 | } |
||
335 |