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 |
||
29 | abstract class BaseController extends Controller |
||
30 | { |
||
31 | /** |
||
32 | * Watch or not created record. |
||
33 | * |
||
34 | * @var bool |
||
35 | */ |
||
36 | protected $viewCreated = false; |
||
37 | |||
38 | /** |
||
39 | * Addition fields for the template. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $additionFields = []; |
||
44 | |||
45 | /** |
||
46 | * Addition attributes with values for the model. |
||
47 | * [ |
||
48 | * 'key1' => 'value1', |
||
49 | * 'key2' => 'value2', |
||
50 | * ] |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $additionAttributes = []; |
||
55 | |||
56 | /** |
||
57 | * Url prefix for redirect and view links. |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $urlPrefix = ''; |
||
61 | |||
62 | /** |
||
63 | * Model object record. |
||
64 | * |
||
65 | * @var ModelInterface |
||
66 | */ |
||
67 | private $model; |
||
68 | |||
69 | /** |
||
70 | * Search new model object. |
||
71 | * |
||
72 | * @var ActiveRecordInterface |
||
73 | */ |
||
74 | private $searchModel; |
||
75 | |||
76 | /** |
||
77 | * Validate component. |
||
78 | * |
||
79 | * @var ValidateComponentInterface |
||
80 | */ |
||
81 | private $validateComponent = null; |
||
82 | |||
83 | /** |
||
84 | * Returns the name of the base model. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | abstract protected function getModelName():string; |
||
89 | |||
90 | /** |
||
91 | * Returns the name of the model to search it. |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | abstract protected function getSearchModelName():string; |
||
96 | |||
97 | /** |
||
98 | * @inheritdoc |
||
99 | */ |
||
100 | public function behaviors() |
||
122 | |||
123 | /** |
||
124 | * Initializer. |
||
125 | */ |
||
126 | public function init() |
||
130 | |||
131 | /** |
||
132 | * @param \yii\base\Action $action |
||
133 | * @return bool |
||
134 | */ |
||
135 | public function beforeAction($action) |
||
141 | |||
142 | /** |
||
143 | * Give ability of configure view to the module class. |
||
144 | * |
||
145 | * @return \yii\base\View|\yii\web\View |
||
146 | */ |
||
147 | public function getView() |
||
155 | |||
156 | /** |
||
157 | * Set model. |
||
158 | * |
||
159 | * @param $model ModelInterface |
||
160 | */ |
||
161 | public function setModel(ModelInterface $model): void |
||
165 | |||
166 | /** |
||
167 | * Set search model. |
||
168 | * |
||
169 | * @param $model ActiveRecordInterface |
||
170 | */ |
||
171 | public function setSearchModel(ActiveRecordInterface $model): void |
||
175 | |||
176 | /** |
||
177 | * Set validate component for main model. |
||
178 | * |
||
179 | * @param $component ValidateComponentInterface |
||
180 | */ |
||
181 | public function setValidateComponent(ValidateComponentInterface $component): void |
||
185 | |||
186 | /** |
||
187 | * Returns model. |
||
188 | * |
||
189 | * @return ModelInterface |
||
190 | */ |
||
191 | public function getModel(): ModelInterface |
||
195 | |||
196 | /** |
||
197 | * Returns search model. |
||
198 | * |
||
199 | * @return ActiveRecordInterface |
||
200 | */ |
||
201 | public function getSearchModel(): ActiveRecordInterface |
||
205 | |||
206 | /** |
||
207 | * Get validate component for main model. |
||
208 | * |
||
209 | * @return ValidateComponentInterface |
||
210 | */ |
||
211 | public function getValidateComponent(): ValidateComponentInterface |
||
215 | |||
216 | /** |
||
217 | * List of records. |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | public function actionIndex() |
||
237 | |||
238 | /** |
||
239 | * Displays one model entry. |
||
240 | * |
||
241 | * @param int|string $id |
||
242 | * |
||
243 | * @return mixed |
||
244 | */ |
||
245 | public function actionView($id) |
||
256 | |||
257 | /** |
||
258 | * Creates a new model record. |
||
259 | * If the result of creation is successful, there will be a redirect to the 'view' or 'index' page. |
||
260 | * |
||
261 | * @return string|\yii\web\Response |
||
262 | */ |
||
263 | public function actionCreate() |
||
295 | |||
296 | /** |
||
297 | * Updates the current model entry. |
||
298 | * If the result of creation is successful, there will be a redirect to the 'view' or 'index' page. |
||
299 | * |
||
300 | * @param int|string $id |
||
301 | * |
||
302 | * @return string|\yii\web\Response |
||
303 | */ |
||
304 | public function actionUpdate($id) |
||
328 | |||
329 | /** |
||
330 | * Deletes the current model entry. |
||
331 | * If the result of the deletion is successful, there will be a redirect to the 'index' page. |
||
332 | * |
||
333 | * @param int|string $id |
||
334 | * |
||
335 | * @return \yii\web\Response |
||
336 | * |
||
337 | * @throws ConflictHttpException |
||
338 | */ |
||
339 | public function actionDelete($id) |
||
351 | |||
352 | /** |
||
353 | * Set addition attributes for model. |
||
354 | * |
||
355 | * @return bool |
||
356 | */ |
||
357 | protected function setAdditionAttributes(): bool |
||
363 | |||
364 | /** |
||
365 | * Get addition fields for the view template. |
||
366 | * @return array |
||
367 | */ |
||
368 | protected function getAdditionFields(): array |
||
372 | |||
373 | /** |
||
374 | * Returns new object of main model. |
||
375 | * |
||
376 | * @return mixed |
||
377 | */ |
||
378 | protected function getNewModel() |
||
383 | |||
384 | /** |
||
385 | * Returns new object of search main model. |
||
386 | * |
||
387 | * @return mixed |
||
388 | */ |
||
389 | protected function getNewSearchModel() |
||
394 | |||
395 | /** |
||
396 | * Find model record. |
||
397 | * If the model is not found, a 404 HTTP exception will be displayed. |
||
398 | * |
||
399 | * @param int|string $key |
||
400 | * |
||
401 | * |
||
402 | * @throws BadRequestHttpException |
||
403 | * @throws UnknownMethodException |
||
404 | * @throws NotFoundHttpException |
||
405 | * |
||
406 | * @return mixed |
||
407 | */ |
||
408 | private function findModel($key) |
||
432 | |||
433 | /** |
||
434 | * Returns an intermediate model for working with the main. |
||
435 | * |
||
436 | * @param int|string|null $key |
||
437 | * |
||
438 | * @return void |
||
439 | */ |
||
440 | private function setModelByConditions($key = null): void |
||
452 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..