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 |
||
12 | View Code Duplication | class Urlset implements OutputInterface |
|
|
|||
13 | { |
||
14 | /** |
||
15 | * Array of URL objects. |
||
16 | * |
||
17 | * @var OutputInterface[] |
||
18 | */ |
||
19 | protected $urls = []; |
||
20 | |||
21 | /** |
||
22 | * Add a new URL object. |
||
23 | * |
||
24 | * @param OutputInterface $url |
||
25 | * |
||
26 | * @return $this |
||
27 | */ |
||
28 | public function addUrl(OutputInterface $url) |
||
29 | { |
||
30 | $this->urls[] = $url; |
||
31 | |||
32 | return $this; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | public function generateXML(XMLWriter $XMLWriter) |
||
56 | |||
57 | /** |
||
58 | * Get array of URL objects. |
||
59 | * |
||
60 | * @return OutputInterface[] |
||
61 | */ |
||
62 | public function getUrls() |
||
66 | } |
||
67 |
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.