Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 ComponentInterface |
||
| 18 | { |
||
| 19 | use HasAccess, HasEvents, HasNavigation; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var |
||
| 23 | */ |
||
| 24 | protected $name; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var \Illuminate\Foundation\Application |
||
| 28 | */ |
||
| 29 | protected $app; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The component display name |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $title; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var mixed|\Sco\Admin\Contracts\RepositoryInterface |
||
| 40 | */ |
||
| 41 | protected $repository; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var \Illuminate\Database\Eloquent\Model |
||
| 45 | */ |
||
| 46 | protected $model; |
||
| 47 | |||
| 48 | protected static $booted = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var \Illuminate\Contracts\Events\Dispatcher |
||
| 52 | */ |
||
| 53 | protected static $dispatcher; |
||
| 54 | |||
| 55 | abstract public function model(); |
||
| 56 | |||
| 57 | 39 | public function __construct(Application $app) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * {@inheritdoc} |
||
| 66 | */ |
||
| 67 | 3 | public function getName() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | */ |
||
| 79 | 3 | public function setName(string $value) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * {@inheritdoc} |
||
| 88 | */ |
||
| 89 | 6 | public function getTitle() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * {@inheritdoc} |
||
| 96 | */ |
||
| 97 | 3 | public function setTitle(string $value) |
|
| 103 | |||
| 104 | 3 | protected function getDefaultName() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | 3 | protected function getModelClassName() |
|
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | 18 | public function setModel(Model $model) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | 24 | public function getModel() |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @return \Illuminate\Database\Eloquent\Model|mixed |
||
| 147 | */ |
||
| 148 | 24 | protected function makeModel() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritdoc} |
||
| 177 | */ |
||
| 178 | 3 | public function getRepository() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * {@inheritdoc} |
||
| 189 | */ |
||
| 190 | 3 | public function setRepository(RepositoryInterface $repository) |
|
| 196 | |||
| 197 | 3 | protected function makeRepository() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * {@inheritdoc} |
||
| 207 | */ |
||
| 208 | 3 | public function getConfigs() |
|
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | */ |
||
| 220 | 12 | View Code Duplication | final public function fireDisplay() |
| 241 | |||
| 242 | public function get() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | 9 | View Code Duplication | final public function fireCreate() |
| 275 | |||
| 276 | /** |
||
| 277 | * {@inheritdoc} |
||
| 278 | */ |
||
| 279 | public function store() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | */ |
||
| 292 | 6 | final public function fireEdit($id) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * {@inheritdoc} |
||
| 318 | */ |
||
| 319 | public function update($id) |
||
| 328 | |||
| 329 | public function delete($id) |
||
| 335 | |||
| 336 | public function forceDelete($id) |
||
| 342 | |||
| 343 | public function restore($id) |
||
| 349 | |||
| 350 | 39 | protected function bootIfNotBooted() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * The "booting" method of the model. |
||
| 365 | * |
||
| 366 | * @return void |
||
| 367 | */ |
||
| 368 | 36 | protected function boot() |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Boot all of the bootable traits on the model. |
||
| 375 | * |
||
| 376 | * @return void |
||
| 377 | */ |
||
| 378 | 36 | protected function bootTraits() |
|
| 386 | } |
||
| 387 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: