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 |
||
| 13 | abstract class Component implements ComponentInterface |
||
| 14 | { |
||
| 15 | use HasEvents, |
||
| 16 | HasNavigation; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var |
||
| 20 | */ |
||
| 21 | protected $name; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var \Illuminate\Foundation\Application |
||
| 25 | */ |
||
| 26 | protected $app; |
||
| 27 | |||
| 28 | protected $title; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var mixed|\Sco\Admin\Contracts\RepositoryInterface |
||
| 32 | */ |
||
| 33 | protected $repository; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var \Illuminate\Database\Eloquent\Model |
||
| 37 | */ |
||
| 38 | protected $model; |
||
| 39 | |||
| 40 | protected static $booted = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var \Illuminate\Contracts\Events\Dispatcher |
||
| 44 | */ |
||
| 45 | protected static $dispatcher; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $permissionObserver; |
||
| 51 | |||
| 52 | protected $permissions; |
||
| 53 | |||
| 54 | protected $permissionMethods = [ |
||
| 55 | 'view', 'create', 'edit', |
||
| 56 | 'delete', 'destroy', 'restore', |
||
| 57 | ]; |
||
| 58 | |||
| 59 | public function __construct(Application $app, $modelClass) |
||
| 75 | |||
| 76 | protected function setDefaultName() |
||
| 80 | |||
| 81 | protected function getModelClassName() |
||
| 85 | |||
| 86 | public function getName() |
||
| 90 | |||
| 91 | public function getTitle() |
||
| 95 | |||
| 96 | public function getModel() |
||
| 100 | |||
| 101 | public function getRepository() |
||
| 105 | |||
| 106 | public function get() |
||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * {@inheritdoc} |
||
| 120 | */ |
||
| 121 | public function getConfigs() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | public function fireView() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritdoc} |
||
| 147 | */ |
||
| 148 | public function fireCreate() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritdoc} |
||
| 163 | */ |
||
| 164 | public function store() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * {@inheritdoc} |
||
| 173 | */ |
||
| 174 | public function update($id) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritdoc} |
||
| 182 | */ |
||
| 183 | public function fireEdit($id) |
||
| 197 | |||
| 198 | |||
| 199 | protected function bootIfNotBooted() |
||
| 211 | |||
| 212 | public function boot() |
||
| 216 | |||
| 217 | public function isView() |
||
| 221 | |||
| 222 | public function isCreate() |
||
| 226 | |||
| 227 | public function isEdit() |
||
| 231 | |||
| 232 | public function isDelete() |
||
| 236 | |||
| 237 | public function isDestroy() |
||
| 241 | |||
| 242 | public function isRestore() |
||
| 246 | |||
| 247 | protected function isRestorableModel() |
||
| 251 | |||
| 252 | public function registerObserver($class = null) |
||
| 269 | |||
| 270 | public function registerPermission($permission, $callback) |
||
| 274 | |||
| 275 | public function can($permission) |
||
| 282 | |||
| 283 | public function getPermissions() |
||
| 296 | } |
||
| 297 |
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: