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 |
||
7 | class Actions extends AbstractDisplayer |
||
8 | { |
||
9 | /** |
||
10 | * @var array |
||
11 | */ |
||
12 | protected $appends = []; |
||
13 | |||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $prepends = []; |
||
18 | |||
19 | /** |
||
20 | * Default actions. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $actions = ['view', 'edit', 'delete']; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $resource; |
||
30 | |||
31 | /** |
||
32 | * Append a action. |
||
33 | * |
||
34 | * @param $action |
||
35 | * |
||
36 | * @return $this |
||
37 | */ |
||
38 | public function append($action) |
||
44 | |||
45 | /** |
||
46 | * Prepend a action. |
||
47 | * |
||
48 | * @param $action |
||
49 | * |
||
50 | * @return $this |
||
51 | */ |
||
52 | public function prepend($action) |
||
58 | |||
59 | /** |
||
60 | * Get route key name of current row. |
||
61 | * |
||
62 | * @return mixed |
||
63 | */ |
||
64 | public function getRouteKey() |
||
68 | |||
69 | /** |
||
70 | * Disable view action. |
||
71 | * |
||
72 | * @return $this |
||
73 | */ |
||
74 | View Code Duplication | public function disableView(bool $disable = true) |
|
84 | |||
85 | /** |
||
86 | * Disable delete. |
||
87 | * |
||
88 | * @return $this. |
||
89 | */ |
||
90 | View Code Duplication | public function disableDelete(bool $disable = true) |
|
100 | |||
101 | /** |
||
102 | * Disable edit. |
||
103 | * |
||
104 | * @return $this. |
||
105 | */ |
||
106 | View Code Duplication | public function disableEdit(bool $disable = true) |
|
116 | |||
117 | /** |
||
118 | * Set resource of current resource. |
||
119 | * |
||
120 | * @param $resource |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function setResource($resource) |
||
130 | |||
131 | /** |
||
132 | * Get resource of current resource. |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getResource() |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function display($callback = null) |
||
161 | |||
162 | /** |
||
163 | * Render view action. |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | protected function renderView() |
||
175 | |||
176 | /** |
||
177 | * Render edit action. |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | protected function renderEdit() |
||
189 | |||
190 | /** |
||
191 | * Render delete action. |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | protected function renderDelete() |
||
205 | |||
206 | protected function setupDeleteScript() |
||
259 | } |
||
260 |
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.