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 |
||
16 | class Action extends Column |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $title; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $class; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $icon; |
||
33 | |||
34 | /** |
||
35 | * @var DataGrid |
||
36 | */ |
||
37 | protected $grid; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $href; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $name; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $params; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $confirm; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $data_attributes = []; |
||
63 | |||
64 | |||
65 | /** |
||
66 | * @param DataGrid $grid |
||
67 | * @param string $href |
||
68 | * @param string $name |
||
69 | * @param array $params |
||
70 | */ |
||
71 | View Code Duplication | public function __construct(DataGrid $grid, $href, $name, $params) |
|
80 | |||
81 | |||
82 | /** |
||
83 | * Render row item into template |
||
84 | * @param Row $row |
||
85 | * @return mixed |
||
86 | */ |
||
87 | public function render(Row $row) |
||
128 | |||
129 | |||
130 | /** |
||
131 | * Set attribute title |
||
132 | * @param string $title |
||
133 | * @return static |
||
134 | */ |
||
135 | public function setTitle($title) |
||
141 | |||
142 | |||
143 | /** |
||
144 | * Get attribute title |
||
145 | * @return string |
||
146 | */ |
||
147 | public function getTitle() |
||
151 | |||
152 | |||
153 | /** |
||
154 | * Set attribute class |
||
155 | * @param string $class |
||
156 | * @return static |
||
157 | */ |
||
158 | public function setClass($class) |
||
164 | |||
165 | |||
166 | /** |
||
167 | * Get attribute class |
||
168 | * @return string |
||
169 | */ |
||
170 | public function getClass() |
||
174 | |||
175 | |||
176 | /** |
||
177 | * Set icon |
||
178 | * @param string $icon |
||
179 | * @return static |
||
180 | */ |
||
181 | public function setIcon($icon) |
||
187 | |||
188 | |||
189 | /** |
||
190 | * Get icon |
||
191 | * @return string |
||
192 | */ |
||
193 | public function getIcon() |
||
197 | |||
198 | |||
199 | /** |
||
200 | * Set confirm dialog |
||
201 | * @param string $message |
||
202 | * @param string $column |
||
203 | * @return static |
||
204 | */ |
||
205 | public function setConfirm($message, $column = NULL) |
||
211 | |||
212 | |||
213 | /** |
||
214 | * Get confirm dialog for particular row item |
||
215 | * @param Row $row |
||
216 | * @return string |
||
217 | */ |
||
218 | public function getConfirm(Row $row) |
||
230 | |||
231 | |||
232 | /** |
||
233 | * Setting data attributes |
||
234 | * @param string $key |
||
235 | * @param mixed $value |
||
236 | */ |
||
237 | public function setDataAttribute($key, $value) |
||
241 | |||
242 | |||
243 | /** |
||
244 | * Create link to custom destination |
||
245 | * @param Row $row |
||
246 | * @return string |
||
247 | * @throws DataGridHasToBeAttachedToPresenterComponentException |
||
248 | */ |
||
249 | protected function createLink(Row $row) |
||
259 | |||
260 | |||
261 | /** |
||
262 | * Get row item params (E.g. action may be called id => $item->id, name => $item->name, ...) |
||
263 | * @param Row $row |
||
264 | * @return array |
||
265 | */ |
||
266 | View Code Duplication | protected function getItemParams(Row $row) |
|
276 | |||
277 | |||
278 | /** |
||
279 | * Translator helper |
||
280 | * @param string $message |
||
281 | * @return string |
||
282 | */ |
||
283 | protected function translate($message) |
||
287 | |||
288 | } |
||
289 |
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.