Complex classes like ModelConfiguration 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 ModelConfiguration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class ModelConfiguration extends ModelConfigurationManager |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $createTitle; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $editTitle; |
||
22 | |||
23 | /** |
||
24 | * @var Closure|null |
||
25 | */ |
||
26 | protected $display; |
||
27 | |||
28 | /** |
||
29 | * @var Closure|null |
||
30 | */ |
||
31 | protected $create; |
||
32 | |||
33 | /** |
||
34 | * @var bool |
||
35 | */ |
||
36 | protected $displayable = true; |
||
37 | |||
38 | /** |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected $creatable = true; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | protected $editable = true; |
||
47 | |||
48 | /** |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected $restorable = true; |
||
52 | |||
53 | /** |
||
54 | * @var bool |
||
55 | */ |
||
56 | protected $deletable = true; |
||
57 | |||
58 | /** |
||
59 | * @var bool |
||
60 | */ |
||
61 | protected $destroyable = true; |
||
62 | |||
63 | /** |
||
64 | * @var Closure|null |
||
65 | */ |
||
66 | protected $edit; |
||
67 | |||
68 | /** |
||
69 | * @var Closure|null |
||
70 | */ |
||
71 | protected $delete = true; |
||
72 | |||
73 | /** |
||
74 | * @var Closure|null |
||
75 | */ |
||
76 | protected $destroy = true; |
||
77 | |||
78 | /** |
||
79 | * @var Closure|null |
||
80 | */ |
||
81 | protected $restore = true; |
||
82 | |||
83 | /** |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $messageOnCreate; |
||
87 | |||
88 | /** |
||
89 | * @var string |
||
90 | */ |
||
91 | protected $messageOnUpdate; |
||
92 | |||
93 | /** |
||
94 | * @var string |
||
95 | */ |
||
96 | protected $messageOnDelete; |
||
97 | |||
98 | /** |
||
99 | * @var string |
||
100 | */ |
||
101 | protected $messageOnDestroy; |
||
102 | |||
103 | /** |
||
104 | * @var string |
||
105 | */ |
||
106 | protected $messageOnRestore; |
||
107 | |||
108 | /** |
||
109 | * @param string $alias |
||
110 | * |
||
111 | * @return $this |
||
112 | */ |
||
113 | public function setAlias($alias) |
||
119 | |||
120 | /** |
||
121 | * @param string $title |
||
122 | * |
||
123 | * @return $this |
||
124 | */ |
||
125 | public function setTitle($title) |
||
131 | |||
132 | /** |
||
133 | * @param bool $deletable |
||
134 | * |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function setDeletable($deletable) |
||
143 | |||
144 | /** |
||
145 | * @return string|\Symfony\Component\Translation\TranslatorInterface |
||
146 | */ |
||
147 | public function getCreateTitle() |
||
155 | |||
156 | /** |
||
157 | * @param string $title |
||
158 | * |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function setCreateTitle($title) |
||
167 | |||
168 | /** |
||
169 | * @return string|\Symfony\Component\Translation\TranslatorInterface |
||
170 | */ |
||
171 | public function getEditTitle() |
||
179 | |||
180 | /** |
||
181 | * @param string $title |
||
182 | * |
||
183 | * @return $this |
||
184 | */ |
||
185 | public function setEditTitle($title) |
||
191 | |||
192 | /** |
||
193 | * @return Closure|null |
||
194 | */ |
||
195 | public function getRestore() |
||
199 | |||
200 | /** |
||
201 | * @return Closure|null |
||
202 | */ |
||
203 | public function getDelete() |
||
207 | |||
208 | /** |
||
209 | * @return Closure|null |
||
210 | */ |
||
211 | public function getDestroy() |
||
215 | |||
216 | /** |
||
217 | * @return Closure|null |
||
218 | */ |
||
219 | public function getEdit() |
||
223 | |||
224 | /** |
||
225 | * @return Closure|null |
||
226 | */ |
||
227 | public function getCreate() |
||
231 | |||
232 | /** |
||
233 | * @return Closure|null |
||
234 | */ |
||
235 | public function getDisplay() |
||
239 | |||
240 | /** |
||
241 | * @param Closure|null $callback |
||
242 | * |
||
243 | * @return $this |
||
244 | */ |
||
245 | public function onCreate(Closure $callback = null) |
||
251 | |||
252 | /** |
||
253 | * @param Closure|null $callback |
||
254 | * |
||
255 | * @return $this |
||
256 | */ |
||
257 | public function onEdit(Closure $callback = null) |
||
263 | |||
264 | /** |
||
265 | * @param Closure|null $callback |
||
266 | * |
||
267 | * @return $this |
||
268 | */ |
||
269 | public function onCreateAndEdit(Closure $callback = null) |
||
276 | |||
277 | /** |
||
278 | * @param Closure|null $callback |
||
279 | * |
||
280 | * @return $this |
||
281 | */ |
||
282 | public function onDelete(Closure $callback = null) |
||
288 | |||
289 | /** |
||
290 | * @param Closure|null $callback |
||
291 | * |
||
292 | * @return $this |
||
293 | */ |
||
294 | public function onDestroy(Closure $callback = null) |
||
300 | |||
301 | /** |
||
302 | * @param Closure|null $callback |
||
303 | * |
||
304 | * @return $this |
||
305 | */ |
||
306 | public function onRestore(Closure $callback = null) |
||
312 | |||
313 | /** |
||
314 | * @param Closure $callback |
||
315 | * |
||
316 | * @return $this |
||
317 | */ |
||
318 | public function onDisplay(Closure $callback) |
||
324 | |||
325 | /** |
||
326 | * @return bool |
||
327 | */ |
||
328 | public function isDisplayable() |
||
332 | |||
333 | /** |
||
334 | * @return $this |
||
335 | */ |
||
336 | public function disableDisplay() |
||
342 | |||
343 | /** |
||
344 | * @return bool |
||
345 | */ |
||
346 | public function isCreatable() |
||
354 | |||
355 | /** |
||
356 | * @return $this |
||
357 | */ |
||
358 | public function disableCreating() |
||
364 | |||
365 | /** |
||
366 | * @param Model $model |
||
367 | * |
||
368 | * @return bool |
||
369 | */ |
||
370 | public function isEditable(Model $model) |
||
378 | |||
379 | /** |
||
380 | * @return $this |
||
381 | */ |
||
382 | public function disableEditing() |
||
388 | |||
389 | /** |
||
390 | * @param Model $model |
||
391 | * |
||
392 | * @return bool |
||
393 | */ |
||
394 | public function isDeletable(Model $model) |
||
398 | |||
399 | /** |
||
400 | * @return $this |
||
401 | */ |
||
402 | public function disableDeleting() |
||
408 | |||
409 | /** |
||
410 | * @param Model $model |
||
411 | * |
||
412 | * @return bool |
||
413 | */ |
||
414 | public function isDestroyable(Model $model) |
||
418 | |||
419 | /** |
||
420 | * @return $this |
||
421 | */ |
||
422 | public function disableDestroying() |
||
428 | |||
429 | /** |
||
430 | * @param Model $model |
||
431 | * |
||
432 | * @return bool |
||
433 | */ |
||
434 | public function isRestorable(Model $model) |
||
438 | |||
439 | /** |
||
440 | * @return bool |
||
441 | */ |
||
442 | public function isRestorableModel() |
||
446 | |||
447 | /** |
||
448 | * @return $this |
||
449 | */ |
||
450 | public function disableRestoring() |
||
456 | |||
457 | /** |
||
458 | * @return $this |
||
459 | */ |
||
460 | public function enableAccessCheck() |
||
466 | |||
467 | /** |
||
468 | * @return $this |
||
469 | */ |
||
470 | public function disableAccessCheck() |
||
476 | |||
477 | /** |
||
478 | * @return DisplayInterface|mixed |
||
479 | */ |
||
480 | public function fireDisplay() |
||
494 | |||
495 | /** |
||
496 | * @return mixed|void |
||
497 | */ |
||
498 | public function fireCreate() |
||
519 | |||
520 | /** |
||
521 | * @param $id |
||
522 | * |
||
523 | * @return mixed|void |
||
524 | */ |
||
525 | public function fireEdit($id) |
||
547 | |||
548 | /** |
||
549 | * @param $id |
||
550 | * |
||
551 | * @return mixed |
||
552 | */ |
||
553 | public function fireDelete($id) |
||
559 | |||
560 | /** |
||
561 | * @param $id |
||
562 | * |
||
563 | * @return mixed |
||
564 | */ |
||
565 | public function fireDestroy($id) |
||
571 | |||
572 | /** |
||
573 | * @param $id |
||
574 | * |
||
575 | * @return bool|mixed |
||
576 | */ |
||
577 | public function fireRestore($id) |
||
585 | |||
586 | /** |
||
587 | * @return string |
||
588 | */ |
||
589 | public function getMessageOnCreate() |
||
597 | |||
598 | /** |
||
599 | * @param string $messageOnCreate |
||
600 | */ |
||
601 | public function setMessageOnCreate($messageOnCreate) |
||
605 | |||
606 | /** |
||
607 | * @return string |
||
608 | */ |
||
609 | public function getMessageOnUpdate() |
||
617 | |||
618 | /** |
||
619 | * @param string $messageOnUpdate |
||
620 | * |
||
621 | * @return $this |
||
622 | */ |
||
623 | public function setMessageOnUpdate($messageOnUpdate) |
||
629 | |||
630 | /** |
||
631 | * @return string |
||
632 | */ |
||
633 | public function getMessageOnDelete() |
||
641 | |||
642 | /** |
||
643 | * @return string |
||
644 | */ |
||
645 | public function getMessageOnDestroy() |
||
653 | |||
654 | /** |
||
655 | * @param string $messageOnDelete |
||
656 | * |
||
657 | * @return $this |
||
658 | */ |
||
659 | public function setMessageOnDelete($messageOnDelete) |
||
665 | |||
666 | /** |
||
667 | * @param string $messageOnDestroy |
||
668 | * |
||
669 | * @return $this |
||
670 | */ |
||
671 | public function setMessageOnDestroy($messageOnDestroy) |
||
677 | |||
678 | /** |
||
679 | * @return string |
||
680 | */ |
||
681 | public function getMessageOnRestore() |
||
689 | |||
690 | /** |
||
691 | * @param string $messageOnRestore |
||
692 | * |
||
693 | * @return $this |
||
694 | */ |
||
695 | public function setMessageOnRestore($messageOnRestore) |
||
701 | |||
702 | /** |
||
703 | * @param string $controllerClass |
||
704 | * |
||
705 | * @return $this |
||
706 | */ |
||
707 | public function setControllerClass($controllerClass) |
||
713 | } |
||
714 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.