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 | public function __construct(Application $app) |
||
58 | { |
||
59 | $this->app = $app; |
||
60 | |||
61 | $this->bootIfNotBooted(); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | public function getName() |
||
68 | { |
||
69 | if (is_null($this->name)) { |
||
70 | $this->setName($this->getDefaultName()); |
||
71 | } |
||
72 | |||
73 | return $this->name; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function setName(string $value) |
||
80 | { |
||
81 | $this->name = $value; |
||
82 | |||
83 | return $this; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public function getTitle() |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | public function setTitle(string $value) |
||
103 | |||
104 | protected function getDefaultName() |
||
108 | |||
109 | /** |
||
110 | * @return string |
||
111 | */ |
||
112 | protected function getModelClassName() |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function setModel(Model $model) |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | public function getModel() |
||
144 | |||
145 | /** |
||
146 | * @return \Illuminate\Database\Eloquent\Model|mixed |
||
147 | */ |
||
148 | protected function makeModel() |
||
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | public function getRepository() |
||
186 | |||
187 | /** |
||
188 | * {@inheritdoc} |
||
189 | */ |
||
190 | public function setRepository(RepositoryInterface $repository) |
||
196 | |||
197 | protected function makeRepository() |
||
204 | |||
205 | /** |
||
206 | * {@inheritdoc} |
||
207 | */ |
||
208 | public function getConfigs() |
||
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | View Code Duplication | final public function fireDisplay() |
|
242 | |||
243 | public function get() |
||
249 | |||
250 | /** |
||
251 | * {@inheritdoc} |
||
252 | */ |
||
253 | View Code Duplication | final public function fireCreate() |
|
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | public function store() |
||
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | final public function fireEdit($id) |
||
310 | |||
311 | /** |
||
312 | * {@inheritdoc} |
||
313 | */ |
||
314 | public function update($id) |
||
319 | |||
320 | public function delete($id) |
||
326 | |||
327 | public function forceDelete($id) |
||
333 | |||
334 | public function restore($id) |
||
340 | |||
341 | protected function bootIfNotBooted() |
||
353 | |||
354 | /** |
||
355 | * The "booting" method of the model. |
||
356 | * |
||
357 | * @return void |
||
358 | */ |
||
359 | protected function boot() |
||
363 | |||
364 | /** |
||
365 | * Boot all of the bootable traits on the model. |
||
366 | * |
||
367 | * @return void |
||
368 | */ |
||
369 | protected function bootTraits() |
||
377 | } |
||
378 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.