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 |
||
15 | class PDepend implements AnalyzerInterface |
||
16 | { |
||
17 | /** |
||
18 | * pdepend XML filename. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $xml = 'pdepend.xml'; |
||
23 | |||
24 | /** |
||
25 | * cauditor JSON filename. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $json = 'cauditor.json'; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $buildPath; |
||
35 | |||
36 | /** |
||
37 | * @var Config |
||
38 | */ |
||
39 | protected $config; |
||
40 | |||
41 | /** |
||
42 | * @param Config $config |
||
43 | */ |
||
44 | public function __construct(Config $config) |
||
48 | |||
49 | /** |
||
50 | * @param Config $config |
||
51 | */ |
||
52 | View Code Duplication | public function setConfig(Config $config) |
|
60 | |||
61 | /** |
||
62 | * @return array |
||
63 | * |
||
64 | * @throws Exception |
||
65 | */ |
||
66 | public function execute() |
||
85 | |||
86 | /** |
||
87 | * Runs pdepend to generate the metrics. |
||
88 | * |
||
89 | * @throws Exception |
||
90 | */ |
||
91 | View Code Duplication | protected function pdepend() |
|
110 | |||
111 | /** |
||
112 | * Transform pdepend output into the (more succinct) format cauditor |
||
113 | * understands. |
||
114 | * |
||
115 | * @param string $xml |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | protected function convert($xml) |
||
131 | } |
||
132 |
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.