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 | 8 | public function __construct(array $config) |
|
| 53 | |||
| 54 | /** |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | 1 | public function getCurrentSitemap(): string |
|
| 61 | |||
| 62 | /** |
||
| 63 | * @param string $currentSitemap |
||
| 64 | */ |
||
| 65 | 4 | public function setCurrentSitemap(string $currentSitemap): void |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @param string $sitemap |
||
| 72 | * @param array $extensions |
||
| 73 | */ |
||
| 74 | 2 | public function openSitemap(string $sitemap, array $extensions = []): void |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @throws \Exception |
||
| 92 | */ |
||
| 93 | 2 | public function closeSitemap(): void |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @return \XMLWriter |
||
| 103 | */ |
||
| 104 | 3 | private function getXMLWriter(): \XMLWriter |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Save from buffer to file |
||
| 111 | * |
||
| 112 | * @return void |
||
| 113 | */ |
||
| 114 | 3 | private function flushData(): void |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | 3 | private function getSitemapFileFullPath(): string |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | 4 | public function getWorkDir(): string |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $dir |
||
| 137 | */ |
||
| 138 | 4 | public function setWorkDir(string $dir): 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 | 3 | private function endFile(): void |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @return int |
||
| 174 | */ |
||
| 175 | 3 | public function getSitemapSize(): int |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @param array $element |
||
| 184 | */ |
||
| 185 | 1 | public function addUrl(array $element): void |
|
| 193 | |||
| 194 | /** |
||
| 195 | * @param string $element |
||
| 196 | * @param $value |
||
| 197 | * @param string|null $namespace |
||
| 198 | */ |
||
| 199 | 1 | private function addElement(string $element, $value, ?string $namespace = null): void |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $element |
||
| 214 | * @param $value |
||
| 215 | * @param string|null $namespace |
||
| 216 | */ |
||
| 217 | 1 | private function addElementArray(string $element, $value, ?string $namespace = null): void |
|
| 229 | |||
| 230 | 1 | private function addElementArrayAssoc(string $element, $value, ?string $namespace = null): void |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @param string $element |
||
| 261 | * @param $value |
||
| 262 | * @param string|null $namespace |
||
| 263 | */ |
||
| 264 | 1 | private function addElementArrayNonAssoc(string $element, $value, ?string $namespace = null): void |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @param string $sitemap |
||
| 273 | */ |
||
| 274 | 1 | public function openSitemapIndex(string $sitemap): void |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @throws \Exception |
||
| 287 | */ |
||
| 288 | 1 | public function closeSitemapIndex(): void |
|
| 295 | |||
| 296 | /** |
||
| 297 | * @param string $sitemap |
||
| 298 | * @param string|null $lastmod |
||
| 299 | */ |
||
| 300 | 1 | public function addSitemap(string $sitemap, string $lastmod = null): void |
|
| 310 | } |
||
| 311 |