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 |
||
19 | class ItemController extends Controller |
||
20 | { |
||
21 | /** |
||
22 | * @var string search class name for auth items search |
||
23 | */ |
||
24 | public $searchClass = [ |
||
25 | 'class' => AuthItemSearch::class, |
||
26 | ]; |
||
27 | |||
28 | /** |
||
29 | * @var int Type of Auth Item |
||
30 | */ |
||
31 | protected $type; |
||
32 | |||
33 | /** |
||
34 | * @var array labels use in view |
||
35 | */ |
||
36 | protected $labels; |
||
37 | |||
38 | /** |
||
39 | * @inheritdoc |
||
40 | */ |
||
41 | public function behaviors(): array |
||
65 | |||
66 | /** |
||
67 | * Lists of all auth items |
||
68 | * |
||
69 | * @return mixed |
||
70 | */ |
||
71 | public function actionIndex() |
||
82 | |||
83 | /** |
||
84 | * Displays a single AuthItem model. |
||
85 | * |
||
86 | * @param string $id |
||
87 | * |
||
88 | * @return mixed |
||
89 | */ |
||
90 | public function actionView(string $id) |
||
96 | |||
97 | /** |
||
98 | * Creates a new AuthItem model. |
||
99 | * |
||
100 | * If creation is successful, the browser will be redirected to the 'view' page. |
||
101 | * |
||
102 | * @return mixed |
||
103 | */ |
||
104 | View Code Duplication | public function actionCreate() |
|
117 | |||
118 | /** |
||
119 | * Updates an existing AuthItem model. |
||
120 | * |
||
121 | * If update is successful, the browser will be redirected to the 'view' page. |
||
122 | * |
||
123 | * @param string $id |
||
124 | * |
||
125 | * @return mixed |
||
126 | */ |
||
127 | View Code Duplication | public function actionUpdate(string $id) |
|
139 | |||
140 | /** |
||
141 | * Deletes an existing AuthItem model. |
||
142 | * |
||
143 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
144 | * |
||
145 | * @param string $id |
||
146 | * |
||
147 | * @return mixed |
||
148 | */ |
||
149 | View Code Duplication | public function actionDelete(string $id) |
|
157 | |||
158 | /** |
||
159 | * Assign items |
||
160 | * |
||
161 | * @param string $id |
||
162 | * |
||
163 | * @return array |
||
164 | */ |
||
165 | View Code Duplication | public function actionAssign(string $id) |
|
173 | |||
174 | /** |
||
175 | * Remove items |
||
176 | * |
||
177 | * @param string $id |
||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | View Code Duplication | public function actionRemove(string $id): array |
|
189 | |||
190 | /** |
||
191 | * @inheritdoc |
||
192 | */ |
||
193 | public function getViewPath(): string |
||
197 | |||
198 | /** |
||
199 | * @return int |
||
200 | */ |
||
201 | public function getType(): int |
||
205 | |||
206 | /** |
||
207 | * @return array |
||
208 | */ |
||
209 | public function getLabels(): array |
||
213 | |||
214 | /** |
||
215 | * Finds the AuthItem model based on its primary key value. |
||
216 | * |
||
217 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
218 | * |
||
219 | * @param string $id |
||
220 | * |
||
221 | * @return AuthItemModel the loaded model |
||
222 | * |
||
223 | * @throws NotFoundHttpException if the model cannot be found |
||
224 | */ |
||
225 | protected function findModel(string $id): AuthItemModel |
||
236 | } |
||
237 |
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.