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:
Complex classes like Action often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Action, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Action extends Column |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * @var string|callable |
||
22 | */ |
||
23 | protected $title; |
||
24 | |||
25 | /** |
||
26 | * @var string|callable |
||
27 | */ |
||
28 | protected $class; |
||
29 | |||
30 | /** |
||
31 | * @var string|callable |
||
32 | */ |
||
33 | protected $icon; |
||
34 | |||
35 | /** |
||
36 | * @var DataGrid |
||
37 | */ |
||
38 | protected $grid; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $href; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $name; |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $params; |
||
54 | |||
55 | /** |
||
56 | * @var array|callable |
||
57 | */ |
||
58 | protected $confirm; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $data_attributes = []; |
||
64 | |||
65 | |||
66 | /** |
||
67 | * @param DataGrid $grid |
||
68 | * @param string $href |
||
69 | * @param string $name |
||
70 | * @param array $params |
||
71 | */ |
||
72 | View Code Duplication | public function __construct(DataGrid $grid, $href, $name, $params) |
|
81 | |||
82 | |||
83 | /** |
||
84 | * Render row item into template |
||
85 | * @param Row $row |
||
86 | * @return mixed |
||
87 | */ |
||
88 | public function render(Row $row) |
||
129 | |||
130 | |||
131 | /** |
||
132 | * Set attribute title |
||
133 | * @param string|callable $title |
||
134 | * @return static |
||
135 | */ |
||
136 | View Code Duplication | public function setTitle($title) |
|
137 | { |
||
138 | if (!is_string($title) && !is_callable($title) && !is_null($title)) { |
||
139 | throw new DataGridException( |
||
140 | 'Action title has to be either string or callback, that will return string' |
||
141 | ); |
||
142 | } |
||
143 | |||
144 | $this->title = $title; |
||
145 | |||
146 | return $this; |
||
147 | } |
||
148 | |||
149 | |||
150 | /** |
||
151 | * Get attribute title |
||
152 | * @param Row $row |
||
153 | * @return string |
||
154 | */ |
||
155 | View Code Duplication | public function getTitle(Row $row) |
|
172 | |||
173 | |||
174 | /** |
||
175 | * Set attribute class |
||
176 | * @param string|callable $class |
||
177 | * @return static |
||
178 | */ |
||
179 | View Code Duplication | public function setClass($class) |
|
180 | { |
||
181 | if (!is_string($class) && !is_callable($class) && !is_null($class)) { |
||
182 | throw new DataGridException( |
||
183 | 'Action class has to be either string or callback, that will return string' |
||
184 | ); |
||
185 | } |
||
186 | |||
187 | $this->class = $class; |
||
188 | |||
189 | return $this; |
||
190 | } |
||
191 | |||
192 | |||
193 | /** |
||
194 | * Get attribute class |
||
195 | * @param Row $row |
||
196 | * @return string |
||
197 | */ |
||
198 | View Code Duplication | public function getClass(Row $row) |
|
215 | |||
216 | |||
217 | /** |
||
218 | * Set icon |
||
219 | * @param string|callable $icon |
||
220 | * @return static|callable |
||
221 | */ |
||
222 | View Code Duplication | public function setIcon($icon) |
|
223 | { |
||
224 | if (!is_string($icon) && !is_callable($icon) && !is_null($icon)) { |
||
225 | throw new DataGridException( |
||
226 | 'Action icon has to be either string or callback, that will return string' |
||
227 | ); |
||
228 | } |
||
229 | |||
230 | $this->icon = $icon; |
||
231 | |||
232 | return $this; |
||
233 | } |
||
234 | |||
235 | |||
236 | /** |
||
237 | * Get icon |
||
238 | * @param Row $row |
||
239 | * @return string |
||
240 | */ |
||
241 | View Code Duplication | public function getIcon(Row $row) |
|
258 | |||
259 | |||
260 | /** |
||
261 | * Set confirm dialog |
||
262 | * @param string|callable $message |
||
263 | * @param string $column |
||
264 | * @return static |
||
265 | */ |
||
266 | View Code Duplication | public function setConfirm($message, $column = NULL) |
|
267 | { |
||
268 | if (!is_string($message) && !is_callable($message) && !is_null($message)) { |
||
269 | throw new DataGridException( |
||
270 | 'Action message has to be either string or callback, that will return string' |
||
271 | ); |
||
272 | } |
||
273 | |||
274 | $this->confirm = [$message, $column]; |
||
275 | |||
276 | return $this; |
||
277 | } |
||
278 | |||
279 | |||
280 | /** |
||
281 | * Get confirm dialog for particular row item |
||
282 | * @param Row $row |
||
283 | * @return string |
||
284 | */ |
||
285 | public function getConfirm(Row $row) |
||
310 | |||
311 | |||
312 | /** |
||
313 | * Setting data attributes |
||
314 | * @param string $key |
||
315 | * @param mixed $value |
||
316 | */ |
||
317 | public function setDataAttribute($key, $value) |
||
321 | |||
322 | |||
323 | /** |
||
324 | * Get row item params (E.g. action may be called id => $item->id, name => $item->name, ...) |
||
325 | * @param Row $row |
||
326 | * @return array |
||
327 | */ |
||
328 | View Code Duplication | protected function getItemParams(Row $row) |
|
338 | |||
339 | |||
340 | /** |
||
341 | * Translator helper |
||
342 | * @param string $message |
||
343 | * @return string |
||
344 | */ |
||
345 | protected function translate($message) |
||
349 | |||
350 | } |
||
351 |
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.