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 | View Code Duplication | class SeriesList extends ComponentBase |
|
|
|||
16 | { |
||
17 | use UrlHelperTrait; |
||
18 | |||
19 | /** |
||
20 | * @var Series |
||
21 | */ |
||
22 | public $series; |
||
23 | |||
24 | /** |
||
25 | * Reference to the page name for linking to series |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | public $seriesPage; |
||
30 | |||
31 | /** |
||
32 | * If the series list should be ordered by another attribute |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | public $orderBy; |
||
37 | |||
38 | /** |
||
39 | * Whether display or not empty series |
||
40 | * |
||
41 | * @var bool |
||
42 | */ |
||
43 | public $displayEmpty; |
||
44 | |||
45 | /** |
||
46 | * Limits the number of records to display |
||
47 | * |
||
48 | * @var int |
||
49 | */ |
||
50 | public $limit; |
||
51 | |||
52 | /** |
||
53 | * @return array |
||
54 | */ |
||
55 | public function componentDetails() |
||
62 | |||
63 | /** |
||
64 | * @return array |
||
65 | */ |
||
66 | public function defineProperties() |
||
97 | |||
98 | /** |
||
99 | * @return mixed |
||
100 | */ |
||
101 | public function getSeriesPageOptions() |
||
105 | |||
106 | /** |
||
107 | * @return mixed |
||
108 | */ |
||
109 | public function getOrderByOptions() |
||
113 | |||
114 | /** |
||
115 | * Prepare and return a series list |
||
116 | * |
||
117 | * @return mixed |
||
118 | */ |
||
119 | public function onRun() |
||
128 | |||
129 | /** |
||
130 | * Get Series |
||
131 | * |
||
132 | * @return mixed |
||
133 | */ |
||
134 | protected function listSeries() |
||
146 | } |
||
147 |
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.