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:
1 | <?php |
||
14 | class SitemapObserver implements ParsedContentObserverInterface |
||
15 | { |
||
16 | /** |
||
17 | * Config writer |
||
18 | * |
||
19 | * @var XmlWriter |
||
20 | */ |
||
21 | private $xmlWriter; |
||
22 | |||
23 | /** |
||
24 | * Sitemap path |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | private $sitemapPath; |
||
29 | |||
30 | /** |
||
31 | * Base url |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | private $baseUrl; |
||
36 | |||
37 | /** |
||
38 | * Constructor |
||
39 | * |
||
40 | * @param XmlWriter $xmlWriter |
||
41 | * @param string $sitemapPath |
||
42 | * @param string $baseUrl |
||
43 | */ |
||
44 | public function __construct(XmlWriter $xmlWriter, string $sitemapPath, string $baseUrl) |
||
50 | |||
51 | /** |
||
52 | * Handle parsed config |
||
53 | * @param array $config |
||
54 | */ |
||
55 | public function __invoke(array $config) |
||
87 | |||
88 | /** |
||
89 | * Get xml config writer |
||
90 | * |
||
91 | * @return XmlXmlWriter |
||
92 | */ |
||
93 | public function getXmlWriter() : XmlWriter |
||
97 | |||
98 | /** |
||
99 | * Get sitemap path |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | public function getSitemapPath() : string |
||
107 | |||
108 | /** |
||
109 | * Get base url |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getBaseUrl() : string |
||
117 | |||
118 | } |
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.