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:
1 | <?php |
||
18 | abstract class Component implements ComponentInterface, WithNavigation |
||
19 | { |
||
20 | use HasAccess, HasEvents, HasNavigation; |
||
21 | |||
22 | /** |
||
23 | * @var |
||
24 | */ |
||
25 | protected $name; |
||
26 | |||
27 | /** |
||
28 | * @var \Illuminate\Foundation\Application |
||
29 | */ |
||
30 | protected $app; |
||
31 | |||
32 | /** |
||
33 | * The component display name |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $title; |
||
38 | |||
39 | /** |
||
40 | * @var mixed|\Sco\Admin\Contracts\RepositoryInterface |
||
41 | */ |
||
42 | protected $repository; |
||
43 | |||
44 | /** |
||
45 | * @var \Illuminate\Database\Eloquent\Model |
||
46 | */ |
||
47 | protected $model; |
||
48 | |||
49 | protected static $booted = []; |
||
50 | |||
51 | /** |
||
52 | * @var \Illuminate\Contracts\Events\Dispatcher |
||
53 | */ |
||
54 | protected static $dispatcher; |
||
55 | |||
56 | abstract public function model(); |
||
57 | |||
58 | public function __construct(Application $app, RepositoryInterface $repository) |
||
59 | { |
||
60 | $this->app = $app; |
||
61 | |||
62 | $this->makeModel(); |
||
63 | |||
64 | $this->repository = $repository; |
||
65 | $this->repository->setModel($this->getModel()); |
||
66 | |||
67 | if (! $this->name) { |
||
68 | $this->setDefaultName(); |
||
69 | } |
||
70 | |||
71 | $this->bootIfNotBooted(); |
||
72 | } |
||
73 | |||
74 | protected function setDefaultName() |
||
75 | { |
||
76 | $this->name = $this->getModelClassName(); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @return string |
||
81 | */ |
||
82 | protected function getModelClassName() |
||
83 | { |
||
84 | return snake_case( // 蛇形命名 |
||
85 | str_plural( // 复数 |
||
86 | class_basename( |
||
87 | get_class($this->getModel()) |
||
88 | ) |
||
89 | ) |
||
90 | ); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public function getModel() |
||
100 | |||
101 | /** |
||
102 | * @return \Illuminate\Database\Eloquent\Model|mixed |
||
103 | */ |
||
104 | protected function makeModel() |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function getName() |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | public function getTitle() |
||
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | public function getRepository() |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function getConfigs() |
||
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | View Code Duplication | final public function fireDisplay() |
|
192 | |||
193 | public function get() |
||
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | */ |
||
203 | View Code Duplication | final public function fireCreate() |
|
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | public function store() |
||
233 | |||
234 | /** |
||
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | final public function fireEdit($id) |
||
260 | |||
261 | /** |
||
262 | * {@inheritdoc} |
||
263 | */ |
||
264 | public function update($id) |
||
269 | |||
270 | public function delete($id) |
||
276 | |||
277 | public function forceDelete($id) |
||
283 | |||
284 | public function restore($id) |
||
290 | |||
291 | protected function bootIfNotBooted() |
||
303 | |||
304 | /** |
||
305 | * The "booting" method of the model. |
||
306 | * |
||
307 | * @return void |
||
308 | */ |
||
309 | protected function boot() |
||
313 | |||
314 | /** |
||
315 | * Boot all of the bootable traits on the model. |
||
316 | * |
||
317 | * @return void |
||
318 | */ |
||
319 | protected function bootTraits() |
||
327 | } |
||
328 |
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.