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 | * @return \Sco\Admin\Contracts\ViewInterface |
||
| 133 | */ |
||
| 134 | public function fireView() |
||
| 143 | |||
| 144 | public function fireCreate() |
||
| 154 | |||
| 155 | |||
| 156 | protected function bootIfNotBooted() |
||
| 168 | |||
| 169 | public function boot() |
||
| 173 | |||
| 174 | public function isView() |
||
| 178 | |||
| 179 | public function isCreate() |
||
| 183 | |||
| 184 | public function isEdit() |
||
| 188 | |||
| 189 | public function isDelete() |
||
| 193 | |||
| 194 | public function isDestroy() |
||
| 198 | |||
| 199 | public function isRestore() |
||
| 203 | |||
| 204 | protected function isRestorableModel() |
||
| 208 | |||
| 209 | public function registerObserver($class = null) |
||
| 223 | |||
| 224 | public function registerPermission($permission, $callback) |
||
| 228 | |||
| 229 | public function can($permission) |
||
| 236 | |||
| 237 | public function getPermissions() |
||
| 250 | } |
||
| 251 |
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: