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 NewsLoader extends AbstractLoader |
||
15 | { |
||
16 | /** |
||
17 | * @var string $median The median between upcoming and archive entries. |
||
18 | */ |
||
19 | protected $median; |
||
20 | |||
21 | /** |
||
22 | * @var object $objType The object to load. |
||
23 | */ |
||
24 | protected $objType; |
||
25 | |||
26 | /** |
||
27 | * @return CollectionLoader |
||
28 | */ |
||
29 | View Code Duplication | public function all() |
|
37 | |||
38 | public function newsProto() |
||
42 | |||
43 | /** |
||
44 | * @return CollectionLoader |
||
45 | */ |
||
46 | View Code Duplication | public function published() |
|
55 | |||
56 | /** |
||
57 | * @return CollectionLoader |
||
58 | */ |
||
59 | View Code Duplication | public function expired() |
|
68 | |||
69 | /** |
||
70 | * Fetch upcoming entries based on the median or now. |
||
71 | * @return CollectionLoader |
||
72 | */ |
||
73 | public function upcoming() |
||
79 | |||
80 | /** |
||
81 | * Fetch upcoming entries based on the median or now. |
||
82 | * @return CollectionLoader |
||
83 | */ |
||
84 | public function archive() |
||
90 | |||
91 | // ========================================================================== |
||
92 | // GETTERS |
||
93 | // ========================================================================== |
||
94 | |||
95 | /** |
||
96 | * @return mixed |
||
97 | */ |
||
98 | public function median() |
||
102 | |||
103 | /** |
||
104 | * @return object |
||
105 | */ |
||
106 | public function objType() |
||
110 | |||
111 | // ========================================================================== |
||
112 | // SETTERS |
||
113 | // ========================================================================== |
||
114 | |||
115 | /** |
||
116 | * @param string $median The median between upcoming and archive. |
||
117 | * @return self |
||
118 | */ |
||
119 | public function setMedian($median) |
||
125 | |||
126 | /** |
||
127 | * @param object $objType The object type. |
||
128 | * @return self |
||
129 | */ |
||
130 | public function setObjType($objType) |
||
136 | } |
||
137 |
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.