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 |
||
31 | abstract class AlbumController extends Controller |
||
32 | { |
||
33 | use MediaFilesTrait; |
||
34 | |||
35 | /** |
||
36 | * Url prefix for redirect and view links. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $urlPrefix = ''; |
||
41 | |||
42 | /** |
||
43 | * Url prefix for redirect and view links of neighbor entity. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $urlPrefixNeighbor = ''; |
||
48 | |||
49 | /** |
||
50 | * Model object record. |
||
51 | * |
||
52 | * @var Album |
||
53 | */ |
||
54 | private $model; |
||
55 | |||
56 | /** |
||
57 | * Returns the name of the base model. |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | abstract protected function getModelName():string; |
||
62 | |||
63 | /** |
||
64 | * Returns the type of album. |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | abstract protected function getAlbumType():string; |
||
69 | |||
70 | /** |
||
71 | * Initializer. |
||
72 | */ |
||
73 | public function init() |
||
74 | { |
||
75 | $this->view->params['user'] = \Yii::$app->user->identity; |
||
76 | |||
77 | $this->viewPath = '@'.Module::MODULE_NAME.'/views/albums'; |
||
78 | |||
79 | parent::init(); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param \yii\base\Action $action |
||
84 | * |
||
85 | * @return bool |
||
86 | */ |
||
87 | public function beforeAction($action) |
||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | View Code Duplication | public function behaviors() |
|
118 | |||
119 | /** |
||
120 | * Set model. |
||
121 | * |
||
122 | * @param Album $model |
||
123 | */ |
||
124 | public function setModel(Album $model): void |
||
128 | |||
129 | /** |
||
130 | * Returns model. |
||
131 | * |
||
132 | * @return Album |
||
133 | */ |
||
134 | public function getModel(): Album |
||
138 | |||
139 | /** |
||
140 | * Give ability of configure view to the module class. |
||
141 | * |
||
142 | * @return \yii\base\View|\yii\web\View |
||
143 | */ |
||
144 | public function getView() |
||
152 | |||
153 | /** |
||
154 | * Lists all Album models. |
||
155 | * |
||
156 | * @return mixed |
||
157 | */ |
||
158 | public function actionIndex() |
||
173 | |||
174 | /** |
||
175 | * Displays a single Album model. |
||
176 | * |
||
177 | * @param integer $id |
||
178 | * |
||
179 | * @throws NotFoundHttpException if the model cannot be found |
||
180 | * |
||
181 | * @return mixed |
||
182 | */ |
||
183 | public function actionView($id) |
||
201 | |||
202 | /** |
||
203 | * Creates a new Album model. |
||
204 | * If creation is successful, the browser will be redirected to the 'view' page. |
||
205 | * |
||
206 | * @return mixed |
||
207 | */ |
||
208 | public function actionCreate() |
||
224 | |||
225 | /** |
||
226 | * Updates an existing Album model. |
||
227 | * If update is successful, the browser will be redirected to the 'view' page. |
||
228 | * |
||
229 | * @param integer $id |
||
230 | * |
||
231 | * @throws NotFoundHttpException if the model cannot be found |
||
232 | * |
||
233 | * @return mixed |
||
234 | */ |
||
235 | public function actionUpdate($id) |
||
270 | |||
271 | /** |
||
272 | * Deletes an existing Album model. |
||
273 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
274 | * |
||
275 | * @param integer $id |
||
276 | * |
||
277 | * @throws NotFoundHttpException if the model cannot be found |
||
278 | * @throws BadRequestHttpException |
||
279 | * |
||
280 | * @return mixed |
||
281 | */ |
||
282 | public function actionDelete($id) |
||
294 | |||
295 | /** |
||
296 | * Finds the Album model based on its primary key value. |
||
297 | * |
||
298 | * @param $key |
||
299 | * |
||
300 | * @throws BadRequestHttpException |
||
301 | * @throws NotFoundHttpException |
||
302 | * |
||
303 | * @return Album |
||
304 | */ |
||
305 | View Code Duplication | protected function findModel($key): Album |
|
330 | |||
331 | /** |
||
332 | * Returns new object of main Album model. |
||
333 | * |
||
334 | * @return Album |
||
335 | */ |
||
336 | protected function getNewModel(): Album |
||
341 | |||
342 | /** |
||
343 | * Returns an intermediate model for working with the main. |
||
344 | * |
||
345 | * @param int|string|null $key |
||
346 | * |
||
347 | * @return void |
||
348 | */ |
||
349 | protected function setModelByConditions($key = null): void |
||
355 | } |
||
356 |
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.