Complex classes like Component 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 Component, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | abstract class Component implements |
||
18 | ComponentInterface, |
||
19 | WithNavigation |
||
20 | { |
||
21 | use HasEvents, |
||
22 | HasNavigation; |
||
23 | |||
24 | /** |
||
25 | * @var |
||
26 | */ |
||
27 | protected $name; |
||
28 | |||
29 | /** |
||
30 | * @var \Illuminate\Foundation\Application |
||
31 | */ |
||
32 | protected $app; |
||
33 | |||
34 | protected $title; |
||
35 | |||
36 | /** |
||
37 | * @var mixed|\Sco\Admin\Contracts\RepositoryInterface |
||
38 | */ |
||
39 | protected $repository; |
||
40 | |||
41 | /** |
||
42 | * @var \Illuminate\Database\Eloquent\Model |
||
43 | */ |
||
44 | protected $model; |
||
45 | |||
46 | protected static $booted = []; |
||
47 | |||
48 | /** |
||
49 | * @var \Illuminate\Contracts\Events\Dispatcher |
||
50 | */ |
||
51 | protected static $dispatcher; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $permissionObserver; |
||
57 | |||
58 | protected $permissions; |
||
59 | |||
60 | protected $permissionMethods = [ |
||
61 | 'view', 'create', 'edit', |
||
62 | 'delete', 'destroy', 'restore', |
||
63 | ]; |
||
64 | |||
65 | public function __construct(Application $app, $modelClass) |
||
81 | |||
82 | protected function setDefaultName() |
||
86 | |||
87 | protected function getModelClassName() |
||
91 | |||
92 | public function getName() |
||
96 | |||
97 | public function getTitle() |
||
101 | |||
102 | public function getModel() |
||
106 | |||
107 | public function getRepository() |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | public function getConfigs() |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | public function fireView() |
||
143 | |||
144 | public function get() |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function fireCreate() |
||
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | public function store() |
||
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | */ |
||
187 | public function fireEdit($id) |
||
205 | |||
206 | /** |
||
207 | * {@inheritdoc} |
||
208 | */ |
||
209 | public function update($id) |
||
214 | |||
215 | public function delete($id) |
||
220 | |||
221 | public function forceDelete($id) |
||
226 | |||
227 | public function restore($id) |
||
232 | |||
233 | protected function bootIfNotBooted() |
||
245 | |||
246 | public function boot() |
||
250 | |||
251 | public function isView() |
||
255 | |||
256 | public function isCreate() |
||
260 | |||
261 | public function isEdit() |
||
265 | |||
266 | public function isDelete() |
||
270 | |||
271 | public function isDestroy() |
||
275 | |||
276 | public function isRestore() |
||
280 | |||
281 | protected function isRestorableModel() |
||
285 | |||
286 | public function registerObserver($class = null) |
||
303 | |||
304 | public function registerPermission($permission, $callback) |
||
308 | |||
309 | public function can($permission) |
||
316 | |||
317 | public function getPermissions() |
||
330 | } |
||
331 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.