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 |
||
| 12 | abstract class Component implements ComponentInterface |
||
| 13 | { |
||
| 14 | use HasEvents, |
||
| 15 | HasNavigation; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var |
||
| 19 | */ |
||
| 20 | protected $name; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var \Illuminate\Foundation\Application |
||
| 24 | */ |
||
| 25 | protected $app; |
||
| 26 | |||
| 27 | protected $title; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var mixed|\Sco\Admin\Contracts\RepositoryInterface |
||
| 31 | */ |
||
| 32 | protected $repository; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var \Illuminate\Database\Eloquent\Model |
||
| 36 | */ |
||
| 37 | protected $model; |
||
| 38 | |||
| 39 | protected static $booted = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \Illuminate\Contracts\Events\Dispatcher |
||
| 43 | */ |
||
| 44 | protected static $dispatcher; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $permissionObserver; |
||
| 50 | |||
| 51 | protected $permissions; |
||
| 52 | |||
| 53 | protected $permissionMethods = [ |
||
| 54 | 'view', 'create', 'edit', |
||
| 55 | 'delete', 'destroy', 'restore', |
||
| 56 | ]; |
||
| 57 | |||
| 58 | public function __construct(Application $app, $modelClass) |
||
| 74 | |||
| 75 | protected function setDefaultName() |
||
| 79 | |||
| 80 | protected function getModelClassName() |
||
| 84 | |||
| 85 | public function getName() |
||
| 89 | |||
| 90 | public function getTitle() |
||
| 94 | |||
| 95 | public function getModel() |
||
| 99 | |||
| 100 | public function getRepository() |
||
| 104 | |||
| 105 | public function get() |
||
| 115 | |||
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritdoc} |
||
| 119 | */ |
||
| 120 | public function getConfigs() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | public function fireView() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * {@inheritdoc} |
||
| 146 | */ |
||
| 147 | public function fireCreate() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritdoc} |
||
| 160 | */ |
||
| 161 | public function fireEdit($id) |
||
| 171 | |||
| 172 | |||
| 173 | protected function bootIfNotBooted() |
||
| 185 | |||
| 186 | public function boot() |
||
| 190 | |||
| 191 | public function isView() |
||
| 195 | |||
| 196 | public function isCreate() |
||
| 200 | |||
| 201 | public function isEdit() |
||
| 205 | |||
| 206 | public function isDelete() |
||
| 210 | |||
| 211 | public function isDestroy() |
||
| 215 | |||
| 216 | public function isRestore() |
||
| 220 | |||
| 221 | protected function isRestorableModel() |
||
| 225 | |||
| 226 | public function registerObserver($class = null) |
||
| 241 | |||
| 242 | public function registerPermission($permission, $callback) |
||
| 246 | |||
| 247 | public function can($permission) |
||
| 254 | |||
| 255 | public function getPermissions() |
||
| 268 | } |
||
| 269 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: