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 |
||
19 | ComponentInterface, |
||
20 | WithNavigation |
||
21 | { |
||
22 | use HasAccess, HasEvents, HasNavigation; |
||
23 | |||
24 | /** |
||
25 | * @var |
||
26 | */ |
||
27 | protected $name; |
||
28 | |||
29 | /** |
||
30 | * @var \Illuminate\Foundation\Application |
||
31 | */ |
||
32 | protected $app; |
||
33 | |||
34 | /** |
||
35 | * The component display name |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $title; |
||
40 | |||
41 | /** |
||
42 | * @var mixed|\Sco\Admin\Contracts\RepositoryInterface |
||
43 | */ |
||
44 | protected $repository; |
||
45 | |||
46 | /** |
||
47 | * @var \Illuminate\Database\Eloquent\Model |
||
48 | */ |
||
49 | protected $model; |
||
50 | |||
51 | protected static $booted = []; |
||
52 | |||
53 | /** |
||
54 | * @var \Illuminate\Contracts\Events\Dispatcher |
||
55 | */ |
||
56 | protected static $dispatcher; |
||
57 | |||
58 | abstract public function model(); |
||
59 | |||
60 | public function __construct(Application $app, RepositoryInterface $repository) |
||
61 | { |
||
62 | $this->app = $app; |
||
63 | |||
64 | $this->makeModel(); |
||
65 | |||
66 | $this->repository = $repository; |
||
67 | $this->repository->setModel($this->getModel()); |
||
68 | |||
69 | if (!$this->name) { |
||
70 | $this->setDefaultName(); |
||
71 | } |
||
72 | |||
73 | $this->bootIfNotBooted(); |
||
74 | } |
||
75 | |||
76 | protected function setDefaultName() |
||
77 | { |
||
78 | $this->name = $this->getModelClassName(); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return string |
||
83 | */ |
||
84 | protected function getModelClassName() |
||
85 | { |
||
86 | return snake_case( // 蛇形命名 |
||
87 | str_plural( // 复数 |
||
88 | class_basename( |
||
89 | get_class($this->getModel()) |
||
90 | ) |
||
91 | ) |
||
92 | ); |
||
93 | } |
||
94 | |||
95 | public function getModel() |
||
99 | |||
100 | protected function makeModel() |
||
101 | { |
||
102 | $class = $this->model(); |
||
103 | if (empty($class)) { |
||
104 | throw new InvalidArgumentException( |
||
105 | sprintf( |
||
106 | 'The component(%s) method "model()" not found value', |
||
107 | get_class($this) |
||
108 | ) |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | $model = $this->app->make($this->model()); |
||
113 | |||
114 | if (!($model instanceof Model)) { |
||
115 | throw new InvalidArgumentException( |
||
116 | sprintf( |
||
117 | "Class %s must be an instance of %s", |
||
118 | $this->model(), |
||
119 | Model::class |
||
120 | ) |
||
121 | ); |
||
122 | } |
||
123 | |||
124 | return $this->model = $model; |
||
125 | } |
||
126 | |||
127 | public function getName() |
||
128 | { |
||
129 | return $this->name; |
||
130 | } |
||
131 | |||
132 | public function getTitle() |
||
133 | { |
||
134 | return $this->title; |
||
135 | } |
||
136 | |||
137 | public function getRepository() |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function getConfigs() |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | View Code Duplication | final public function fireView() |
|
181 | |||
182 | public function get() |
||
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | View Code Duplication | final public function fireCreate() |
|
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | public function store() |
||
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | final public function fireEdit($id) |
||
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | public function update($id) |
||
260 | |||
261 | public function delete($id) |
||
266 | |||
267 | public function forceDelete($id) |
||
272 | |||
273 | public function restore($id) |
||
278 | |||
279 | protected function bootIfNotBooted() |
||
291 | |||
292 | /** |
||
293 | * The "booting" method of the model. |
||
294 | * |
||
295 | * @return void |
||
296 | */ |
||
297 | protected function boot() |
||
301 | |||
302 | /** |
||
303 | * Boot all of the bootable traits on the model. |
||
304 | * |
||
305 | * @return void |
||
306 | */ |
||
307 | protected function bootTraits() |
||
315 | } |
||
316 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.