Complex classes like Sitemap 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 Sitemap, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Sitemap |
||
26 | { |
||
27 | use Domain; |
||
28 | |||
29 | /** |
||
30 | * Avaliable values for changefreq tag |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | const CHANGEFREQ = [ |
||
35 | 'always', |
||
36 | 'hourly', |
||
37 | 'daily', |
||
38 | 'weekly', |
||
39 | 'monthly', |
||
40 | 'yearly', |
||
41 | 'never' |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * Extension for sitemap file |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | const EXT = '.xml'; |
||
50 | |||
51 | /** |
||
52 | * Extension for gzipped sitemap file |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | const GZ_EXT = '.xml.gz'; |
||
57 | |||
58 | /** |
||
59 | * URL to Sitemap Schema |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | const SCHEMA = 'http://www.sitemaps.org/schemas/sitemap/0.9'; |
||
64 | |||
65 | /** |
||
66 | * Limit of items in Sitemap files |
||
67 | * |
||
68 | * @var int |
||
69 | */ |
||
70 | const ITEM_PER_SITEMAP = 50000; |
||
71 | |||
72 | /** |
||
73 | * Limit of Sitmeaps in SitemapsIndex |
||
74 | * |
||
75 | * @var int |
||
76 | */ |
||
77 | const SITEMAP_PER_SITEMAPINDEX = 1000; |
||
78 | |||
79 | /** |
||
80 | * Limit of single files size |
||
81 | * |
||
82 | * @var int |
||
83 | */ |
||
84 | const SITEMAP_MAX_SIZE = 52000000; |
||
85 | |||
86 | /** |
||
87 | * Path on disk to public directory. |
||
88 | * |
||
89 | * @var string |
||
90 | */ |
||
91 | private $publicDirectory = ''; |
||
92 | |||
93 | /** |
||
94 | * Path related to public directory to dir where sitemaps will be. |
||
95 | * |
||
96 | * @var string |
||
97 | */ |
||
98 | private $sitepamsDirectory = ''; |
||
99 | |||
100 | /** |
||
101 | * Path to temporary directory. |
||
102 | * |
||
103 | * @var string |
||
104 | */ |
||
105 | private $sitemapTempDirectory = ''; |
||
106 | |||
107 | /** |
||
108 | * Default filename for sitemap file |
||
109 | * |
||
110 | * @var string |
||
111 | */ |
||
112 | private $defaultFilename = 'sitemap'; |
||
113 | |||
114 | /** |
||
115 | * Name of index file |
||
116 | * |
||
117 | * @var string |
||
118 | */ |
||
119 | private $indexFilename = 'index'; |
||
120 | |||
121 | /** |
||
122 | * DataCollector instance |
||
123 | * |
||
124 | * @var DataCollector |
||
125 | */ |
||
126 | private $dataCollector = null; |
||
127 | |||
128 | /** |
||
129 | * Use compression |
||
130 | * |
||
131 | * @var bool |
||
132 | */ |
||
133 | private $useCompression = false; |
||
134 | |||
135 | /** |
||
136 | * XML Writer object |
||
137 | * |
||
138 | * @var XML |
||
139 | */ |
||
140 | private $xml; |
||
141 | |||
142 | /** |
||
143 | * Separator to be used in Sitemap filenames |
||
144 | * |
||
145 | * @var string |
||
146 | */ |
||
147 | private $separator = '-'; // ~49,6MB - to have some limit to close file |
||
148 | |||
149 | /** |
||
150 | * Construktor |
||
151 | * |
||
152 | * @param string $domain |
||
153 | */ |
||
154 | 11 | public function __construct(string $domain = null) |
|
160 | |||
161 | /** |
||
162 | * @param Items\Url $item |
||
163 | * @param string|null $group |
||
164 | */ |
||
165 | 1 | public function addItem(Items\Url $item, ?string $group = null): void |
|
175 | |||
176 | /** |
||
177 | * Get default filename for sitemap file |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | 2 | public function getDefaultFilename(): string |
|
185 | |||
186 | /** |
||
187 | * Set default filename for sitemap file |
||
188 | * |
||
189 | * @param string $defaultFilename |
||
190 | */ |
||
191 | 1 | public function setDefaultFilename(string $defaultFilename): void |
|
195 | |||
196 | /** |
||
197 | * Get DataCollecotr Object |
||
198 | * |
||
199 | * @return DataCollector|null |
||
200 | */ |
||
201 | 2 | public function getDataCollector(): ?DataCollector |
|
205 | |||
206 | /** |
||
207 | * @param string $driver |
||
208 | * @param mixed |
||
209 | */ |
||
210 | 2 | public function setDataCollector(string $driver, $config = null): void |
|
218 | |||
219 | /** |
||
220 | * @throws Exception |
||
221 | */ |
||
222 | public function generate() |
||
247 | |||
248 | /** |
||
249 | * @return string |
||
250 | */ |
||
251 | 1 | public function getPublicDirectory(): string |
|
255 | |||
256 | /** |
||
257 | * @param string $publicDirectory |
||
258 | * |
||
259 | * @throws Exception |
||
260 | */ |
||
261 | 1 | public function setPublicDirectory(string $publicDirectory): void |
|
269 | |||
270 | /** |
||
271 | * @return XML|null |
||
272 | */ |
||
273 | 1 | public function getXml(): ?XML |
|
277 | |||
278 | /** |
||
279 | * @param string $driver |
||
280 | * @param array $config |
||
281 | */ |
||
282 | 1 | public function setXml(string $driver, array $config = []): void |
|
296 | |||
297 | /** |
||
298 | * @param string $dir |
||
299 | */ |
||
300 | private function removeDir($dir) |
||
318 | |||
319 | /** |
||
320 | * @return string |
||
321 | */ |
||
322 | 1 | public function getTempDirectory(): string |
|
335 | |||
336 | /** |
||
337 | * @return string |
||
338 | */ |
||
339 | 1 | public function getSitepamsTempDirectory(): string |
|
349 | |||
350 | /** |
||
351 | * @throws Exception |
||
352 | */ |
||
353 | public function generateSitemaps(): array |
||
413 | |||
414 | /** |
||
415 | * @return string |
||
416 | */ |
||
417 | 1 | public function getSeparator(): string |
|
421 | |||
422 | /** |
||
423 | * @param string $separator |
||
424 | */ |
||
425 | 1 | public function setSeparator(string $separator): void |
|
429 | |||
430 | /** |
||
431 | * Check if compression is used |
||
432 | * |
||
433 | * @return bool |
||
434 | */ |
||
435 | 1 | public function isUseCompression(): bool |
|
439 | |||
440 | /** |
||
441 | * Set whether to use compression or not |
||
442 | * |
||
443 | * @param bool $useCompression |
||
444 | */ |
||
445 | 1 | public function setUseCompression(bool $useCompression): void |
|
452 | |||
453 | /** |
||
454 | * @param string $dir |
||
455 | * @param array $files |
||
456 | * |
||
457 | * @throws Exception |
||
458 | */ |
||
459 | private function compressFiles(string $dir, array &$files) |
||
489 | |||
490 | /** |
||
491 | * @param array $sitemaps |
||
492 | * |
||
493 | * @return array |
||
494 | * @throws Exception |
||
495 | */ |
||
496 | public function generateSitemapsIndex(array $sitemaps): array |
||
532 | |||
533 | /** |
||
534 | * Get filename of sitemap index file |
||
535 | * |
||
536 | * @return string |
||
537 | */ |
||
538 | 1 | public function getIndexFilename(): string |
|
542 | |||
543 | /** |
||
544 | * Set filename of sitemap index file |
||
545 | * |
||
546 | * @param string $indexFilename |
||
547 | */ |
||
548 | 1 | public function setIndexFilename(string $indexFilename): void |
|
552 | |||
553 | /** |
||
554 | * @return string |
||
555 | */ |
||
556 | public function getSitepamsDirectory(): string |
||
565 | |||
566 | /** |
||
567 | * @param string $sitepamsDirectory |
||
568 | */ |
||
569 | 1 | public function setSitepamsDirectory(string $sitepamsDirectory): void |
|
573 | |||
574 | private function publishSitemap() |
||
611 | |||
612 | /** |
||
613 | * @return string |
||
614 | */ |
||
615 | private function getExt() |
||
623 | } |
||
624 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: