Total Complexity | 44 |
Total Lines | 185 |
Duplicated Lines | 33.51 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like XmlFormatter 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.
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 XmlFormatter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class XmlFormatter extends BaseFormatter implements SitemapIndexFormatterInterface |
||
11 | { |
||
12 | public function getSitemapStart() |
||
13 | { |
||
14 | return '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<urlset '.'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '.'xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" '.'xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">'."\n"; |
||
15 | } |
||
16 | |||
17 | public function getSitemapEnd() |
||
18 | { |
||
19 | return '</urlset>'; |
||
20 | } |
||
21 | |||
22 | public function getSitemapIndexStart() |
||
23 | { |
||
24 | return '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n"; |
||
25 | } |
||
26 | |||
27 | public function getSitemapIndexEnd() |
||
28 | { |
||
29 | return '</sitemapindex>'; |
||
30 | } |
||
31 | |||
32 | public function formatUrl(Url $url) |
||
33 | { |
||
34 | return '<url>'."\n".$this->formatBody($url).'</url>'."\n"; |
||
35 | } |
||
36 | |||
37 | protected function formatBody(Url $url) |
||
38 | { |
||
39 | $buffer = "\t".'<loc>'.$this->escape($url->getLoc()).'</loc>'."\n"; |
||
40 | |||
41 | View Code Duplication | if ($url->getLastmod() !== null) { |
|
|
|||
42 | $buffer .= "\t".'<lastmod>'.$this->escape($url->getLastmod()).'</lastmod>'."\n"; |
||
43 | } |
||
44 | |||
45 | if ($url->getChangefreq() !== null) { |
||
46 | $buffer .= "\t".'<changefreq>'.$this->escape($url->getChangefreq()).'</changefreq>'."\n"; |
||
47 | } |
||
48 | |||
49 | if ($url->getPriority() !== null) { |
||
50 | $buffer .= "\t".'<priority>'.$this->escape($url->getPriority()).'</priority>'."\n"; |
||
51 | } |
||
52 | |||
53 | foreach ($url->getVideos() as $video) { |
||
54 | $buffer .= $this->formatVideo($video); |
||
55 | } |
||
56 | |||
57 | foreach ($url->getImages() as $image) { |
||
58 | $buffer .= $this->formatImage($image); |
||
59 | } |
||
60 | |||
61 | return $buffer; |
||
62 | } |
||
63 | |||
64 | public function formatSitemapIndex(SitemapIndex $sitemapIndex) |
||
65 | { |
||
66 | return '<sitemap>'."\n".$this->formatSitemapIndexBody($sitemapIndex).'</sitemap>'."\n"; |
||
67 | } |
||
68 | |||
69 | protected function formatSitemapIndexBody(SitemapIndex $sitemapIndex) |
||
70 | { |
||
71 | $buffer = "\t".'<loc>'.$this->escape($sitemapIndex->getLoc()).'</loc>'."\n"; |
||
72 | |||
73 | View Code Duplication | if ($sitemapIndex->getLastmod() !== null) { |
|
74 | $buffer .= "\t".'<lastmod>'.$this->escape($sitemapIndex->getLastmod()).'</lastmod>'."\n"; |
||
75 | } |
||
76 | |||
77 | return $buffer; |
||
78 | } |
||
79 | |||
80 | protected function formatVideo(Video $video) |
||
170 | } |
||
171 | |||
172 | protected function formatImage(Image $image) |
||
173 | { |
||
174 | $buffer = "\t".'<image:image>'."\n"; |
||
175 | |||
176 | $buffer .= "\t\t".'<image:loc>'.$this->escape($image->getLoc()).'</image:loc>'."\n"; |
||
177 | |||
178 | View Code Duplication | if ($image->getCaption() !== null) { |
|
195 | } |
||
196 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.