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 Row 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 Row, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types = 1); |
||
21 | 1 | class Row |
|
22 | { |
||
23 | |||
24 | 1 | use SmartObject; |
|
25 | |||
26 | /** |
||
27 | * @var DataGrid |
||
28 | */ |
||
29 | protected $datagrid; |
||
30 | |||
31 | /** |
||
32 | * @var mixed |
||
33 | */ |
||
34 | protected $item; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $primary_key; |
||
40 | |||
41 | /** |
||
42 | * @var mixed |
||
43 | */ |
||
44 | protected $id; |
||
45 | |||
46 | /** |
||
47 | * @var Html |
||
48 | */ |
||
49 | protected $control; |
||
50 | |||
51 | /** |
||
52 | * @param DataGrid $datagrid |
||
53 | * @param mixed $item |
||
54 | * @param string $primary_key |
||
55 | */ |
||
56 | public function __construct(DataGrid $datagrid, $item, $primary_key) |
||
57 | { |
||
58 | $this->control = Html::el('tr'); |
||
59 | 1 | $this->datagrid = $datagrid; |
|
60 | 1 | $this->item = $item; |
|
61 | 1 | $this->primary_key = $primary_key; |
|
62 | 1 | $this->id = $this->getValue($primary_key); |
|
63 | 1 | ||
64 | if ($datagrid->hasColumnsSummary()) { |
||
65 | 1 | $datagrid->getColumnsSummary()->add($this); |
|
66 | } |
||
67 | } |
||
68 | 1 | ||
69 | |||
70 | /** |
||
71 | * Get id value of item |
||
72 | * |
||
73 | * @return mixed |
||
74 | */ |
||
75 | public function getId() |
||
79 | |||
80 | |||
81 | /** |
||
82 | * Get item value of key |
||
83 | * |
||
84 | * @param mixed $key |
||
85 | * @return mixed |
||
86 | */ |
||
87 | public function getValue($key) |
||
114 | |||
115 | |||
116 | /** |
||
117 | * @return Html |
||
118 | */ |
||
119 | public function getControl(): Html |
||
123 | |||
124 | |||
125 | /** |
||
126 | * @return string |
||
127 | */ |
||
128 | public function getControlClass(): string |
||
136 | |||
137 | |||
138 | /** |
||
139 | * @param ActiveRow $item |
||
140 | * @param string $key |
||
141 | * @return mixed|NULL |
||
142 | */ |
||
143 | public function getActiveRowProperty(ActiveRow $item, string $key) |
||
167 | |||
168 | |||
169 | /** |
||
170 | * LeanMapper: Access object properties to get a item value |
||
171 | * |
||
172 | * @param LeanMapper\Entity $item |
||
173 | * @param mixed $key |
||
174 | * @return mixed |
||
175 | */ |
||
176 | 1 | View Code Duplication | public function getLeanMapperEntityProperty(LeanMapper\Entity $item, $key) |
199 | |||
200 | |||
201 | /** |
||
202 | * Nextras: Access object properties to get a item value |
||
203 | * |
||
204 | * @param Nextras\Orm\Entity\Entity $item |
||
205 | * @param string $key |
||
206 | * @return mixed |
||
207 | */ |
||
208 | View Code Duplication | public function getNextrasEntityProperty(Nextras\Orm\Entity\Entity $item, string $key) |
|
231 | |||
232 | |||
233 | /** |
||
234 | * Doctrine: Access object properties to get a item value |
||
235 | * |
||
236 | 1 | * @param mixed $item |
|
237 | 1 | * @param mixed $key |
|
238 | 1 | * @return mixed |
|
239 | */ |
||
240 | 1 | public function getDoctrineEntityProperty($item, $key) |
|
264 | |||
265 | 1 | ||
266 | /** |
||
267 | * Get original item |
||
268 | * |
||
269 | * @return mixed |
||
270 | */ |
||
271 | public function getItem() |
||
275 | |||
276 | |||
277 | /** |
||
278 | * Has particular row group actions allowed? |
||
279 | * |
||
280 | * @return bool |
||
281 | */ |
||
282 | public function hasGroupAction(): bool |
||
288 | |||
289 | |||
290 | /** |
||
291 | * Has particular row a action allowed? |
||
292 | * |
||
293 | * @param mixed $key |
||
294 | * @return bool |
||
295 | */ |
||
296 | public function hasAction($key): bool |
||
302 | |||
303 | |||
304 | /** |
||
305 | * Has particular row inlie edit allowed? |
||
306 | */ |
||
307 | public function hasInlineEdit(): bool |
||
313 | |||
314 | |||
315 | public function applyColumnCallback(string $key, Column\Column $column): Column\Column |
||
325 | |||
326 | |||
327 | /** |
||
328 | * Key may contain ".", get rid of it (+ the table alias) |
||
329 | */ |
||
330 | private function formatDibiRowKey(string $key): string |
||
338 | 1 | ||
339 | } |
||
340 |
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.