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 DisplayTab 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 DisplayTab, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class DisplayTab implements TabInterface, DisplayInterface, FormInterface |
||
29 | { |
||
30 | use VisibleCondition, \SleepingOwl\Admin\Traits\Renderable; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $label; |
||
36 | |||
37 | /** |
||
38 | * @var bool |
||
39 | */ |
||
40 | protected $active = false; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $name; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $icon; |
||
51 | |||
52 | /** |
||
53 | * @var Renderable |
||
54 | */ |
||
55 | protected $content; |
||
56 | |||
57 | /** |
||
58 | * @var |
||
59 | */ |
||
60 | protected $badge; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $view = 'display.tab'; |
||
66 | |||
67 | /** |
||
68 | * @param Renderable $content |
||
69 | * @param string|null $label |
||
70 | * @param string|null $icon |
||
71 | * @param Badge|string|\Closure|null $badge |
||
72 | */ |
||
73 | 27 | public function __construct(Renderable $content, $label = null, $icon = null, $badge = null) |
|
89 | |||
90 | /** |
||
91 | * @param Badge|string|\Closure|null $badge |
||
92 | * @return $this |
||
93 | */ |
||
94 | public function setBadge($badge) |
||
108 | |||
109 | /** |
||
110 | * @return mixed |
||
111 | */ |
||
112 | 1 | public function getBadge() |
|
116 | |||
117 | /** |
||
118 | * @return string |
||
119 | */ |
||
120 | 4 | public function getLabel() |
|
124 | |||
125 | /** |
||
126 | * @param string $label |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | 3 | public function setLabel($label) |
|
136 | |||
137 | /** |
||
138 | * @return bool |
||
139 | */ |
||
140 | 2 | public function isActive() |
|
144 | |||
145 | /** |
||
146 | * @param bool $active |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | 1 | public function setActive($active = true) |
|
156 | |||
157 | /** |
||
158 | * @return $this |
||
159 | */ |
||
160 | public function addTabElement() |
||
212 | |||
213 | /** |
||
214 | * @return string |
||
215 | * @throws DisplayTabException |
||
216 | */ |
||
217 | 3 | public function getName() |
|
227 | |||
228 | /** |
||
229 | * @param string $name |
||
230 | * |
||
231 | * @return $this |
||
232 | */ |
||
233 | 1 | public function setName($name) |
|
239 | |||
240 | /** |
||
241 | * @return string |
||
242 | */ |
||
243 | 2 | public function getIcon() |
|
247 | |||
248 | /** |
||
249 | * @param string $icon |
||
250 | * |
||
251 | * @return $this |
||
252 | */ |
||
253 | 1 | public function setIcon($icon) |
|
259 | |||
260 | /** |
||
261 | * @return Renderable |
||
262 | */ |
||
263 | 19 | public function getContent() |
|
267 | |||
268 | /** |
||
269 | * @param string $class |
||
270 | * |
||
271 | * @return $this |
||
272 | */ |
||
273 | 2 | public function setModelClass($class) |
|
281 | |||
282 | /** |
||
283 | * Initialize tab. |
||
284 | * |
||
285 | * @return $this |
||
286 | */ |
||
287 | 2 | public function initialize() |
|
295 | |||
296 | /** |
||
297 | * @param string $action |
||
298 | * |
||
299 | * @return $this |
||
300 | */ |
||
301 | 2 | public function setAction($action) |
|
309 | |||
310 | /** |
||
311 | * @param int $id |
||
312 | * |
||
313 | * @return $this |
||
314 | */ |
||
315 | 2 | public function setId($id) |
|
323 | |||
324 | /** |
||
325 | * @param \Illuminate\Http\Request $request |
||
326 | * @param ModelConfigurationInterface $model |
||
327 | * |
||
328 | * @throws ValidationException |
||
329 | */ |
||
330 | 2 | public function validateForm(\Illuminate\Http\Request $request, ModelConfigurationInterface $model = null) |
|
336 | |||
337 | /** |
||
338 | * Save model. |
||
339 | * |
||
340 | * @param \Illuminate\Http\Request $request |
||
341 | * @param ModelConfigurationInterface $model |
||
342 | * |
||
343 | * @return void |
||
344 | */ |
||
345 | 2 | public function saveForm(\Illuminate\Http\Request $request, ModelConfigurationInterface $model = null) |
|
351 | |||
352 | /** |
||
353 | * Set currently rendered instance. |
||
354 | * |
||
355 | * @param Model $model |
||
356 | * |
||
357 | * @return $this |
||
358 | */ |
||
359 | 2 | public function setModel(Model $model) |
|
367 | |||
368 | /** |
||
369 | * @return Model $model |
||
370 | */ |
||
371 | 2 | public function getModel() |
|
377 | |||
378 | /** |
||
379 | * Get form item validation rules. |
||
380 | * @return array |
||
381 | */ |
||
382 | 2 | public function getValidationRules() |
|
390 | |||
391 | /** |
||
392 | * @return array |
||
393 | */ |
||
394 | 2 | public function getValidationMessages() |
|
402 | |||
403 | /** |
||
404 | * @return array |
||
405 | */ |
||
406 | 2 | public function getValidationLabels() |
|
414 | |||
415 | /** |
||
416 | * @param \Illuminate\Http\Request $request |
||
417 | * |
||
418 | * @return void |
||
419 | */ |
||
420 | 2 | public function save(\Illuminate\Http\Request $request) |
|
426 | |||
427 | /** |
||
428 | * @param \Illuminate\Http\Request $request |
||
429 | * |
||
430 | * @return void |
||
431 | */ |
||
432 | 2 | public function afterSave(\Illuminate\Http\Request $request) |
|
438 | |||
439 | /** |
||
440 | * @return mixed |
||
441 | */ |
||
442 | 2 | public function getValue() |
|
448 | |||
449 | /** |
||
450 | * @return bool |
||
451 | */ |
||
452 | 2 | public function isReadonly() |
|
460 | |||
461 | /** |
||
462 | * @return bool |
||
463 | */ |
||
464 | public function isValueSkipped() |
||
472 | |||
473 | /** |
||
474 | * @param string $path |
||
475 | * |
||
476 | * @return FormElementInterface|null |
||
477 | */ |
||
478 | 2 | public function getElement($path) |
|
484 | |||
485 | /** |
||
486 | * @return FormElementsCollection |
||
487 | */ |
||
488 | 2 | public function getElements() |
|
496 | |||
497 | /** |
||
498 | * @param array $elements |
||
499 | * |
||
500 | * @return $this |
||
501 | */ |
||
502 | 2 | public function setElements(array $elements) |
|
510 | |||
511 | /** |
||
512 | * @return array |
||
513 | */ |
||
514 | 1 | public function toArray() |
|
524 | } |
||
525 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: