Complex classes like FormButtons 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 FormButtons, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class FormButtons implements FormButtonsInterface |
||
19 | { |
||
20 | use HtmlAttributes, Renderable; |
||
21 | |||
22 | /** |
||
23 | * @var |
||
24 | */ |
||
25 | protected $placements; |
||
26 | |||
27 | /** |
||
28 | * @var string|null |
||
29 | */ |
||
30 | protected $cancelButtonText; |
||
31 | |||
32 | /** |
||
33 | * @var string|null |
||
34 | */ |
||
35 | protected $saveButtonText; |
||
36 | |||
37 | /** |
||
38 | * @var string|null |
||
39 | */ |
||
40 | protected $saveAndCloseButtonText; |
||
41 | |||
42 | /** |
||
43 | * @var string|null |
||
44 | */ |
||
45 | protected $saveAndCreateButtonText; |
||
46 | |||
47 | /** |
||
48 | * @var string|null |
||
49 | */ |
||
50 | protected $deleteButtonText; |
||
51 | |||
52 | /** |
||
53 | * @var string|null |
||
54 | */ |
||
55 | protected $destroyButtonText; |
||
56 | |||
57 | /** |
||
58 | * @var string|null |
||
59 | */ |
||
60 | protected $restoreButtonText; |
||
61 | |||
62 | /** |
||
63 | * @var bool |
||
64 | */ |
||
65 | protected $showCancelButton = true; |
||
66 | |||
67 | /** |
||
68 | * @var bool |
||
69 | */ |
||
70 | protected $showSaveAndCloseButton = true; |
||
71 | |||
72 | /** |
||
73 | * @var bool |
||
74 | */ |
||
75 | protected $showSaveAndCreateButton = true; |
||
76 | |||
77 | /** |
||
78 | * @var bool|null |
||
79 | */ |
||
80 | protected $showDeleteButton = true; |
||
81 | |||
82 | /** |
||
83 | * @var bool|null |
||
84 | */ |
||
85 | protected $showDestroyButton = true; |
||
86 | |||
87 | /** |
||
88 | * @var bool|null |
||
89 | */ |
||
90 | protected $showRestoreButton = true; |
||
91 | |||
92 | /** |
||
93 | * @var ModelConfigurationInterface |
||
94 | */ |
||
95 | protected $modelConfiguration; |
||
96 | |||
97 | /** |
||
98 | * @var Model |
||
99 | */ |
||
100 | protected $model; |
||
101 | |||
102 | /** |
||
103 | * @var string|View |
||
104 | */ |
||
105 | protected $view = 'form.buttons'; |
||
106 | |||
107 | protected $buttons; |
||
108 | |||
109 | /** |
||
110 | * FormButtons constructor. |
||
111 | */ |
||
112 | 10 | public function __construct() |
|
117 | |||
118 | /** |
||
119 | * @return array |
||
120 | */ |
||
121 | protected function getButtons() |
||
142 | |||
143 | /** |
||
144 | * @param $buttons |
||
145 | * @return $this |
||
146 | */ |
||
147 | public function setButtons($buttons) |
||
153 | |||
154 | /** |
||
155 | * @param $buttons |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function replaceButtons($buttons) |
||
180 | |||
181 | /** |
||
182 | * @param $placements |
||
183 | * @return $this |
||
184 | */ |
||
185 | public function setPlacements($placements) |
||
191 | |||
192 | /** |
||
193 | * @return mixed |
||
194 | */ |
||
195 | public function getPlacements() |
||
199 | |||
200 | /** |
||
201 | * Set start buttons. Default logic. |
||
202 | */ |
||
203 | 10 | protected function setButtonsOnConstruct() |
|
214 | |||
215 | /** |
||
216 | * @return bool |
||
217 | */ |
||
218 | protected function isTrashed() |
||
222 | |||
223 | /** |
||
224 | * @deprecated new version available |
||
225 | * @return null|string |
||
226 | */ |
||
227 | public function getCancelButtonText() |
||
235 | |||
236 | /** |
||
237 | * @param string $cancelButtonText |
||
238 | * @deprecated new version available |
||
239 | * @return $this |
||
240 | */ |
||
241 | public function setCancelButtonText($cancelButtonText) |
||
247 | |||
248 | /** |
||
249 | * @deprecated new version available |
||
250 | * @return string |
||
251 | */ |
||
252 | public function getSaveButtonText() |
||
260 | |||
261 | /** |
||
262 | * @param string $saveButtonText |
||
263 | * @deprecated |
||
264 | * @return $this |
||
265 | */ |
||
266 | public function setSaveButtonText($saveButtonText) |
||
272 | |||
273 | /** |
||
274 | * @deprecated new version available |
||
275 | * @return string |
||
276 | */ |
||
277 | public function getSaveAndCloseButtonText() |
||
285 | |||
286 | /** |
||
287 | * @param string $saveAndCloseButtonText |
||
288 | * |
||
289 | * @deprecated new version available |
||
290 | * @return $this |
||
291 | */ |
||
292 | public function setSaveAndCloseButtonText($saveAndCloseButtonText) |
||
298 | |||
299 | /** |
||
300 | * @deprecated new version available |
||
301 | * @return string |
||
302 | */ |
||
303 | public function getSaveAndCreateButtonText() |
||
311 | |||
312 | /** |
||
313 | * @param null|string $saveAndCreateButtonText |
||
314 | * |
||
315 | * @deprecated new version available |
||
316 | * @return $this |
||
317 | */ |
||
318 | public function setSaveAndCreateButtonText($saveAndCreateButtonText) |
||
324 | |||
325 | /** |
||
326 | * @deprecated new version available |
||
327 | * @return string |
||
328 | */ |
||
329 | public function getDeleteButtonText() |
||
337 | |||
338 | /** |
||
339 | * @param null|string $deleteButtonText |
||
340 | * @deprecated new version available |
||
341 | * @return $this |
||
342 | */ |
||
343 | public function setDeleteButtonText($deleteButtonText) |
||
349 | |||
350 | /** |
||
351 | * @deprecated new version available |
||
352 | * @return null |
||
353 | */ |
||
354 | public function getDestroyButtonText() |
||
362 | |||
363 | /** |
||
364 | * @deprecated new version available |
||
365 | * @param null|string $destroyButtonText |
||
366 | */ |
||
367 | public function setDestroyButtonText($destroyButtonText) |
||
371 | |||
372 | /** |
||
373 | * @deprecated new version available |
||
374 | * @return string |
||
375 | */ |
||
376 | public function getRestoreButtonText() |
||
384 | |||
385 | /** |
||
386 | * @deprecated new version available |
||
387 | * @param null|string $restoreButtonText |
||
388 | */ |
||
389 | public function setRestoreButtonText($restoreButtonText) |
||
393 | |||
394 | /** |
||
395 | * @deprecated new version available |
||
396 | * @return bool |
||
397 | */ |
||
398 | public function isShowCancelButton() |
||
402 | |||
403 | /** |
||
404 | * @deprecated new version available |
||
405 | * @return $this |
||
406 | */ |
||
407 | public function hideCancelButton() |
||
413 | |||
414 | /** |
||
415 | * @deprecated new version available |
||
416 | * @return bool |
||
417 | */ |
||
418 | public function isShowSaveAndCloseButton() |
||
422 | |||
423 | /** |
||
424 | * @deprecated new version available |
||
425 | * @return $this |
||
426 | */ |
||
427 | public function hideSaveAndCloseButton() |
||
433 | |||
434 | /** |
||
435 | * @deprecated new version available |
||
436 | * @return bool |
||
437 | */ |
||
438 | public function isShowSaveAndCreateButton() |
||
442 | |||
443 | /** |
||
444 | * @deprecated new version available |
||
445 | * @return $this |
||
446 | */ |
||
447 | public function hideSaveAndCreateButton() |
||
453 | |||
454 | /** |
||
455 | * @deprecated new version available |
||
456 | * @return bool|null |
||
457 | */ |
||
458 | public function isShowDeleteButton() |
||
468 | |||
469 | /** |
||
470 | * @deprecated new version available |
||
471 | * @return $this |
||
472 | */ |
||
473 | public function hideDeleteButton() |
||
479 | |||
480 | /** |
||
481 | * @deprecated new version available |
||
482 | * @return bool|null |
||
483 | */ |
||
484 | public function isShowDestroyButton() |
||
495 | |||
496 | /** |
||
497 | * @deprecated new version available |
||
498 | * @return $this |
||
499 | */ |
||
500 | public function hideDestroyButton() |
||
506 | |||
507 | /** |
||
508 | * @deprecated new version available |
||
509 | * @return bool|null |
||
510 | */ |
||
511 | public function isShowRestoreButton() |
||
522 | |||
523 | /** |
||
524 | * @deprecated new version available |
||
525 | * @return $this |
||
526 | */ |
||
527 | public function hideRestoreButton() |
||
533 | |||
534 | /** |
||
535 | * @return array |
||
536 | */ |
||
537 | public function toArray() |
||
563 | |||
564 | /** |
||
565 | * @param ModelConfigurationInterface $modelConfiguration |
||
566 | * |
||
567 | * @return $this |
||
568 | */ |
||
569 | 3 | public function setModelConfiguration(ModelConfigurationInterface $modelConfiguration) |
|
575 | |||
576 | /** |
||
577 | * @return ModelConfigurationInterface |
||
578 | */ |
||
579 | public function getModelConfiguration() |
||
583 | |||
584 | /** |
||
585 | * @return Model |
||
586 | */ |
||
587 | public function getModel() |
||
591 | |||
592 | /** |
||
593 | * @param Model $model |
||
594 | * |
||
595 | * @return $this |
||
596 | */ |
||
597 | 3 | public function setModel(Model $model) |
|
603 | } |
||
604 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.