Complex classes like SitemapItem 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 SitemapItem, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Arcanedev\LaravelSitemap\Entities; |
||
11 | class SitemapItem implements ArrayAccess |
||
12 | { |
||
13 | /* ------------------------------------------------------------------------------------------------ |
||
14 | | Properties |
||
15 | | ------------------------------------------------------------------------------------------------ |
||
16 | */ |
||
17 | /** |
||
18 | * URL of the page. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $loc; |
||
23 | |||
24 | /** |
||
25 | * The date of last modification of the file. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $lastmod; |
||
30 | |||
31 | /** |
||
32 | * How frequently the page is likely to change. |
||
33 | * Valid values: always|hourly|daily|weekly|monthly|yearly|never |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $freq; |
||
38 | |||
39 | /** |
||
40 | * The priority of this URL relative to other URLs on your site. |
||
41 | * Valid values range from 0.0 to 1.0 |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $priority; |
||
46 | |||
47 | /** @var string */ |
||
48 | protected $title; |
||
49 | |||
50 | /** @var array */ |
||
51 | protected $images = []; |
||
52 | |||
53 | /** @var array */ |
||
54 | protected $videos = []; |
||
55 | |||
56 | /** @var array */ |
||
57 | protected $translations = []; |
||
58 | |||
59 | /** @var array */ |
||
60 | protected $googlenews = []; |
||
61 | |||
62 | /** @var array */ |
||
63 | protected $alternates = []; |
||
64 | |||
65 | /* ------------------------------------------------------------------------------------------------ |
||
66 | | Constructor |
||
67 | | ------------------------------------------------------------------------------------------------ |
||
68 | */ |
||
69 | /** |
||
70 | * SitemapItem constructor. |
||
71 | * |
||
72 | * @param array $params |
||
73 | * @param bool $escape |
||
74 | */ |
||
75 | 144 | public function __construct(array $params, $escape = true) |
|
90 | |||
91 | /* ------------------------------------------------------------------------------------------------ |
||
92 | | Getters & Setters |
||
93 | | ------------------------------------------------------------------------------------------------ |
||
94 | */ |
||
95 | /** |
||
96 | * @return string |
||
97 | */ |
||
98 | 48 | public function getLoc() |
|
102 | |||
103 | /** |
||
104 | * @param string $loc |
||
105 | * |
||
106 | * @return self |
||
107 | */ |
||
108 | 144 | public function setLoc($loc) |
|
114 | |||
115 | /** |
||
116 | * @param bool $format |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | 48 | public function getLastmod($format = true) |
|
126 | |||
127 | /** |
||
128 | * @param \DateTime|string $lastmod |
||
129 | * |
||
130 | * @return self |
||
131 | */ |
||
132 | 144 | public function setLastmod($lastmod) |
|
142 | |||
143 | /** |
||
144 | * @return string |
||
145 | */ |
||
146 | 24 | public function getPriority() |
|
150 | |||
151 | /** |
||
152 | * @param string $priority |
||
153 | * |
||
154 | * @return self |
||
155 | */ |
||
156 | 144 | public function setPriority($priority) |
|
162 | |||
163 | /** |
||
164 | * @return string |
||
165 | */ |
||
166 | 24 | public function getFreq() |
|
170 | |||
171 | /** |
||
172 | * @param string $freq |
||
173 | * |
||
174 | * @return self |
||
175 | */ |
||
176 | 144 | public function setFreq($freq) |
|
182 | |||
183 | /** |
||
184 | * @return string |
||
185 | */ |
||
186 | 24 | public function getTitle() |
|
190 | |||
191 | /** |
||
192 | * @param string $title |
||
193 | * |
||
194 | * @return self |
||
195 | */ |
||
196 | 144 | public function setTitle($title) |
|
202 | |||
203 | /** |
||
204 | * @return array |
||
205 | */ |
||
206 | 24 | public function getImages() |
|
210 | |||
211 | /** |
||
212 | * @param array $images |
||
213 | * |
||
214 | * @return self |
||
215 | */ |
||
216 | 144 | public function setImages(array $images) |
|
222 | |||
223 | /** |
||
224 | * @return array |
||
225 | */ |
||
226 | 24 | public function getVideos() |
|
230 | |||
231 | /** |
||
232 | * @param array $videos |
||
233 | * |
||
234 | * @return self |
||
235 | */ |
||
236 | 144 | public function setVideos(array $videos) |
|
242 | |||
243 | /** |
||
244 | * @return array |
||
245 | */ |
||
246 | 24 | public function getTranslations() |
|
250 | |||
251 | /** |
||
252 | * @param array $translations |
||
253 | * |
||
254 | * @return self |
||
255 | */ |
||
256 | 144 | public function setTranslations(array $translations) |
|
262 | |||
263 | /** |
||
264 | * @return array |
||
265 | */ |
||
266 | 24 | public function getGooglenews() |
|
270 | |||
271 | /** |
||
272 | * @param array $googlenews |
||
273 | * |
||
274 | * @return self |
||
275 | */ |
||
276 | 144 | public function setGooglenews(array $googlenews) |
|
284 | |||
285 | /** |
||
286 | * @return array |
||
287 | */ |
||
288 | 24 | public function getAlternates() |
|
292 | |||
293 | /** |
||
294 | * @param array $alternates |
||
295 | * |
||
296 | * @return self |
||
297 | */ |
||
298 | 144 | public function setAlternates(array $alternates) |
|
304 | |||
305 | /* ------------------------------------------------------------------------------------------------ |
||
306 | | ArrayAccess Functions |
||
307 | | ------------------------------------------------------------------------------------------------ |
||
308 | */ |
||
309 | /** |
||
310 | * Whether a offset exists |
||
311 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
312 | * |
||
313 | * @param mixed $offset An offset to check for. |
||
314 | * |
||
315 | * @return bool |
||
316 | */ |
||
317 | 8 | public function offsetExists($offset) |
|
321 | |||
322 | /** |
||
323 | * Offset to retrieve |
||
324 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
325 | * |
||
326 | * @param mixed $offset The offset to retrieve. |
||
327 | * |
||
328 | * @return mixed |
||
329 | */ |
||
330 | 24 | public function offsetGet($offset) |
|
336 | |||
337 | /** |
||
338 | * Offset to set |
||
339 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
340 | * |
||
341 | * @param mixed $offset The offset to assign the value to. |
||
342 | * @param mixed $value The value to set. |
||
343 | */ |
||
344 | 16 | public function offsetSet($offset, $value) |
|
352 | |||
353 | /** |
||
354 | * Offset to unset |
||
355 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
356 | * |
||
357 | * @param mixed $offset The offset to unset. |
||
358 | */ |
||
359 | 8 | public function offsetUnset($offset) |
|
363 | |||
364 | /* ------------------------------------------------------------------------------------------------ |
||
365 | | Main Functions |
||
366 | | ------------------------------------------------------------------------------------------------ |
||
367 | */ |
||
368 | /** |
||
369 | * Make a new sitemap item. |
||
370 | * |
||
371 | * @param array $params |
||
372 | * @param bool $escape |
||
373 | * |
||
374 | * @return self |
||
375 | */ |
||
376 | 24 | public static function make(array $params, $escape = true) |
|
380 | |||
381 | /** |
||
382 | * Escaping the sitemap item. |
||
383 | */ |
||
384 | 144 | public function escape() |
|
394 | |||
395 | /* ------------------------------------------------------------------------------------------------ |
||
396 | | Other Functions |
||
397 | | ------------------------------------------------------------------------------------------------ |
||
398 | */ |
||
399 | 144 | private function escapeLoc() |
|
403 | |||
404 | 144 | private function escapeTitle() |
|
410 | |||
411 | 144 | private function escapeImages() |
|
421 | |||
422 | 144 | private function escapeTranslations() |
|
432 | |||
433 | 144 | private function escapeAlternates() |
|
443 | |||
444 | 144 | private function escapeVideos() |
|
455 | |||
456 | 144 | private function escapeGooglenews() |
|
462 | } |
||
463 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.