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 |
||
11 | View Code Duplication | class CanonicalizationMethodType |
|
|
|||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @property string $algorithm |
||
16 | */ |
||
17 | private $algorithm = null; |
||
18 | |||
19 | /** |
||
20 | * @property mixed[] $anyElement |
||
21 | */ |
||
22 | private $anyElement = array( |
||
23 | |||
24 | ); |
||
25 | |||
26 | /** |
||
27 | * Gets as algorithm |
||
28 | * |
||
29 | * @return string |
||
30 | */ |
||
31 | public function getAlgorithm() |
||
35 | |||
36 | /** |
||
37 | * Sets a new algorithm |
||
38 | * |
||
39 | * @param string $algorithm |
||
40 | * @return self |
||
41 | */ |
||
42 | public function setAlgorithm($algorithm) |
||
47 | |||
48 | /** |
||
49 | * Adds as array |
||
50 | * |
||
51 | * @return self |
||
52 | * @param mixed $array |
||
53 | */ |
||
54 | public function addToAnyElement($array) |
||
59 | |||
60 | /** |
||
61 | * isset anyElement |
||
62 | * |
||
63 | * @param scalar $index |
||
64 | * @return boolean |
||
65 | */ |
||
66 | public function issetAnyElement($index) |
||
70 | |||
71 | /** |
||
72 | * unset anyElement |
||
73 | * |
||
74 | * @param scalar $index |
||
75 | * @return void |
||
76 | */ |
||
77 | public function unsetAnyElement($index) |
||
81 | |||
82 | /** |
||
83 | * Gets as anyElement |
||
84 | * |
||
85 | * @return mixed[] |
||
86 | */ |
||
87 | public function getAnyElement() |
||
91 | |||
92 | /** |
||
93 | * Sets a new anyElement |
||
94 | * |
||
95 | * @param mixed[] $anyElement |
||
96 | * @return self |
||
97 | */ |
||
98 | public function setAnyElement(array $anyElement) |
||
103 | |||
104 | |||
105 | } |
||
106 | |||
107 |
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.