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 DataProcessor |
||
16 | { |
||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | protected $start; |
||
21 | |||
22 | /** |
||
23 | * Columns to escape value. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $escapeColumns = []; |
||
28 | |||
29 | /** |
||
30 | * Processed data output |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $output = []; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $appendColumns = []; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $editColumns = []; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $excessColumns = []; |
||
50 | |||
51 | /** |
||
52 | * @var mixed |
||
53 | */ |
||
54 | protected $results; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $templates; |
||
60 | |||
61 | /** |
||
62 | * @var bool |
||
63 | */ |
||
64 | protected $includeIndex; |
||
65 | |||
66 | /** |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $rawColumns; |
||
70 | |||
71 | /** |
||
72 | * @param mixed $results |
||
73 | * @param array $columnDef |
||
74 | * @param array $templates |
||
75 | * @param int $start |
||
76 | */ |
||
77 | public function __construct($results, array $columnDef, array $templates, $start) |
||
89 | |||
90 | /** |
||
91 | * Process data to output on browser |
||
92 | * |
||
93 | * @param bool $object |
||
94 | * @return array |
||
95 | */ |
||
96 | public function process($object = false) |
||
117 | |||
118 | /** |
||
119 | * Process add columns. |
||
120 | * |
||
121 | * @param mixed $data |
||
122 | * @param mixed $row |
||
123 | * @return array |
||
124 | */ |
||
125 | View Code Duplication | protected function addColumns($data, $row) |
|
134 | |||
135 | /** |
||
136 | * Process edit columns. |
||
137 | * |
||
138 | * @param mixed $data |
||
139 | * @param mixed $row |
||
140 | * @return array |
||
141 | */ |
||
142 | View Code Duplication | protected function editColumns($data, $row) |
|
151 | |||
152 | /** |
||
153 | * Setup additional DT row variables. |
||
154 | * |
||
155 | * @param mixed $data |
||
156 | * @param mixed $row |
||
157 | * @return array |
||
158 | */ |
||
159 | protected function setupRowVariables($data, $row) |
||
170 | |||
171 | /** |
||
172 | * Remove declared hidden columns. |
||
173 | * |
||
174 | * @param array $data |
||
175 | * @return array |
||
176 | */ |
||
177 | protected function removeExcessColumns(array $data) |
||
185 | |||
186 | /** |
||
187 | * Flatten array with exceptions. |
||
188 | * |
||
189 | * @param array $array |
||
190 | * @return array |
||
191 | */ |
||
192 | public function flatten(array $array) |
||
207 | |||
208 | /** |
||
209 | * Escape column values as declared. |
||
210 | * |
||
211 | * @param array $output |
||
212 | * @return array |
||
213 | */ |
||
214 | protected function escapeColumns(array $output) |
||
230 | |||
231 | /** |
||
232 | * Escape all values of row. |
||
233 | * |
||
234 | * @param array $row |
||
235 | * @return array |
||
236 | */ |
||
237 | protected function escapeRow(array $row) |
||
251 | } |
||
252 |
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.