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 |
||
17 | class XMLWriter implements XML |
||
18 | { |
||
19 | use IsAssoc; |
||
20 | use Domain; |
||
21 | |||
22 | /** |
||
23 | * @var \XMLWriter |
||
24 | */ |
||
25 | private $XMLWriter; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $currentSitemap; |
||
31 | |||
32 | /** |
||
33 | * @var string|null |
||
34 | */ |
||
35 | private $workDir; |
||
36 | |||
37 | /** |
||
38 | * XMLWriter constructor. |
||
39 | * |
||
40 | * @param array $config |
||
41 | * |
||
42 | * @throws \InvalidArgumentException |
||
43 | */ |
||
44 | 4 | public function __construct(array $config) |
|
53 | |||
54 | /** |
||
55 | * @return string |
||
56 | */ |
||
57 | public function getCurrentSitemap(): string |
||
61 | |||
62 | /** |
||
63 | * @param string $currentSitemap |
||
64 | */ |
||
65 | public function setCurrentSitemap(string $currentSitemap): void |
||
69 | |||
70 | /** |
||
71 | * @param string $sitemap |
||
72 | * @param array $extensions |
||
73 | */ |
||
74 | public function openSitemap(string $sitemap, array $extensions = []): void |
||
89 | |||
90 | /** |
||
91 | * @return \XMLWriter |
||
92 | */ |
||
93 | private function getXMLWriter(): \XMLWriter |
||
97 | |||
98 | /** |
||
99 | * Save from buffer to file |
||
100 | * |
||
101 | * @return void |
||
102 | */ |
||
103 | private function flushData(): void |
||
107 | |||
108 | /** |
||
109 | * @return string |
||
110 | */ |
||
111 | private function getSitemapFileFullPath(): string |
||
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | 1 | public function getWorkDir(): string |
|
123 | |||
124 | /** |
||
125 | * @param string $dir |
||
126 | */ |
||
127 | 1 | public function setWorkDir(string $dir): void |
|
131 | |||
132 | /** |
||
133 | * @throws \Exception |
||
134 | */ |
||
135 | public function closeSitemap(): void |
||
142 | |||
143 | /** |
||
144 | * Remove whitespace chars from end of file (Google don't like them) |
||
145 | * |
||
146 | * @return void |
||
147 | * @throws Exception |
||
148 | */ |
||
149 | private function endFile(): void |
||
175 | |||
176 | /** |
||
177 | * @return int |
||
178 | */ |
||
179 | public function getSitemapSize(): int |
||
185 | |||
186 | /** |
||
187 | * @param array $element |
||
188 | */ |
||
189 | public function addUrl(array $element): void |
||
197 | |||
198 | /** |
||
199 | * @param string $element |
||
200 | * @param $value |
||
201 | * @param string|null $namespace |
||
202 | */ |
||
203 | private function addElement(string $element, $value, ?string $namespace = null): void |
||
215 | |||
216 | /** |
||
217 | * @param string $element |
||
218 | * @param $value |
||
219 | * @param string|null $namespace |
||
220 | */ |
||
221 | private function addElementArray(string $element, $value, ?string $namespace = null): void |
||
233 | |||
234 | private function addElementArrayAssoc(string $element, $value, ?string $namespace = null): void |
||
262 | |||
263 | /** |
||
264 | * @param string $element |
||
265 | * @param $value |
||
266 | * @param string|null $namespace |
||
267 | */ |
||
268 | private function addElementArrayNonAssoc(string $element, $value, ?string $namespace = null): void |
||
274 | |||
275 | /** |
||
276 | * @param string $sitemap |
||
277 | */ |
||
278 | public function openSitemapIndex(string $sitemap): void |
||
288 | |||
289 | /** |
||
290 | * @throws \Exception |
||
291 | */ |
||
292 | public function closeSitemapIndex(): void |
||
299 | |||
300 | /** |
||
301 | * @param string $sitemap |
||
302 | * @param string|null $lastmod |
||
303 | */ |
||
304 | public function addSitemap(string $sitemap, string $lastmod = null): void |
||
314 | } |
||
315 |