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 |
||
8 | class DataProcessor |
||
9 | { |
||
10 | /** |
||
11 | * @var int |
||
12 | */ |
||
13 | protected $start; |
||
14 | |||
15 | /** |
||
16 | * Columns to escape value. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $escapeColumns = []; |
||
21 | |||
22 | /** |
||
23 | * Processed data output. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $output = []; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $appendColumns = []; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $editColumns = []; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $excessColumns = []; |
||
43 | |||
44 | /** |
||
45 | * @var mixed |
||
46 | */ |
||
47 | protected $results; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $templates; |
||
53 | |||
54 | /** |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $includeIndex; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $rawColumns; |
||
63 | |||
64 | /** |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $exceptions = ['DT_RowId', 'DT_RowClass', 'DT_RowData', 'DT_RowAttr']; |
||
68 | |||
69 | /** |
||
70 | * @param mixed $results |
||
71 | * @param array $columnDef |
||
72 | * @param array $templates |
||
73 | * @param int $start |
||
74 | */ |
||
75 | public function __construct($results, array $columnDef, array $templates, $start) |
||
88 | |||
89 | /** |
||
90 | * Process data to output on browser. |
||
91 | * |
||
92 | * @param bool $object |
||
93 | * @return array |
||
94 | */ |
||
95 | 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 | * Get only needed columns. |
||
173 | * |
||
174 | * @param array $data |
||
175 | * @return array |
||
176 | */ |
||
177 | protected function selectOnlyNeededColumns(array $data) |
||
185 | |||
186 | /** |
||
187 | * Remove declared hidden columns. |
||
188 | * |
||
189 | * @param array $data |
||
190 | * @return array |
||
191 | */ |
||
192 | protected function removeExcessColumns(array $data) |
||
200 | |||
201 | /** |
||
202 | * Flatten array with exceptions. |
||
203 | * |
||
204 | * @param array $array |
||
205 | * @return array |
||
206 | */ |
||
207 | public function flatten(array $array) |
||
220 | |||
221 | /** |
||
222 | * Escape column values as declared. |
||
223 | * |
||
224 | * @param array $output |
||
225 | * @return array |
||
226 | */ |
||
227 | protected function escapeColumns(array $output) |
||
242 | |||
243 | /** |
||
244 | * Escape all values of row. |
||
245 | * |
||
246 | * @param array $row |
||
247 | * @return array |
||
248 | */ |
||
249 | protected function escapeRow(array $row) |
||
264 | } |
||
265 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: