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 |
||
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 | /** |
||
53 | * @param DataGrid $datagrid |
||
54 | * @param mixed $item |
||
55 | * @param string $primary_key |
||
56 | */ |
||
57 | public function __construct(DataGrid $datagrid, $item, $primary_key) |
||
69 | |||
70 | |||
71 | /** |
||
72 | * Get id value of item |
||
73 | * @return mixed |
||
74 | */ |
||
75 | public function getId() |
||
79 | |||
80 | |||
81 | /** |
||
82 | * Get item value of key |
||
83 | * @param mixed $key |
||
84 | * @return mixed |
||
85 | */ |
||
86 | public function getValue($key) |
||
113 | |||
114 | |||
115 | /** |
||
116 | * @return Html |
||
117 | */ |
||
118 | public function getControl() |
||
122 | |||
123 | |||
124 | /** |
||
125 | * @return string |
||
126 | */ |
||
127 | public function getControlClass() |
||
135 | |||
136 | |||
137 | /** |
||
138 | * @param ActiveRow $item |
||
139 | * @param string $key |
||
140 | * @return mixed|NULL |
||
141 | */ |
||
142 | public function getActiveRowProperty(ActiveRow $item, $key) |
||
166 | |||
167 | |||
168 | /** |
||
169 | * LeanMapper: Access object properties to get a item value |
||
170 | * @param LeanMapper\Entity $item |
||
171 | * @param mixed $key |
||
172 | * @return mixed |
||
173 | */ |
||
174 | View Code Duplication | public function getLeanMapperEntityProperty(LeanMapper\Entity $item, $key) |
|
196 | |||
197 | |||
198 | /** |
||
199 | * Nextras: Access object properties to get a item value |
||
200 | * @param Nextras\Orm\Entity\Entity $item |
||
201 | * @param string $key |
||
202 | * @return mixed |
||
203 | */ |
||
204 | View Code Duplication | public function getNextrasEntityProperty(Nextras\Orm\Entity\Entity $item, $key) |
|
226 | |||
227 | |||
228 | /** |
||
229 | * Doctrine: Access object properties to get a item value |
||
230 | * @param mixed $item |
||
231 | * @param mixed $key |
||
232 | * @return mixed |
||
233 | */ |
||
234 | public function getDoctrineEntityProperty($item, $key) |
||
257 | |||
258 | |||
259 | /** |
||
260 | * Get original item |
||
261 | * @return mixed |
||
262 | */ |
||
263 | public function getItem() |
||
267 | |||
268 | |||
269 | /** |
||
270 | * Has particular row group actions allowed? |
||
271 | * @return bool |
||
272 | */ |
||
273 | public function hasGroupAction() |
||
279 | |||
280 | |||
281 | /** |
||
282 | * Has particular row a action allowed? |
||
283 | * @param mixed $key |
||
284 | * @return bool |
||
285 | */ |
||
286 | public function hasAction($key) |
||
292 | |||
293 | |||
294 | /** |
||
295 | * Has particular row inlie edit allowed? |
||
296 | * @return bool |
||
297 | */ |
||
298 | public function hasInlineEdit() |
||
304 | |||
305 | |||
306 | /** |
||
307 | * @param string $key |
||
308 | * @param Column\Column $column |
||
309 | * @return void |
||
310 | */ |
||
311 | public function applyColumnCallback($key, Column\Column $column) |
||
321 | |||
322 | |||
323 | /** |
||
324 | * Key may contain ".", get rid of it (+ the table alias) |
||
325 | * |
||
326 | * @param string $key |
||
327 | * @return string |
||
328 | */ |
||
329 | private function formatDibiRowKey($key) |
||
337 | } |
||
338 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.