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 |
||
28 | abstract class BaseController extends Controller |
||
29 | { |
||
30 | /** |
||
31 | * Watch or not created record. |
||
32 | * |
||
33 | * @var bool |
||
34 | */ |
||
35 | protected $viewCreated = false; |
||
36 | |||
37 | /** |
||
38 | * Addition fields for the template. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $additionFields = []; |
||
43 | |||
44 | /** |
||
45 | * Addition attributes with values for the model. |
||
46 | * [ |
||
47 | * 'key1' => 'value1', |
||
48 | * 'key2' => 'value2', |
||
49 | * ] |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $additionAttributes = []; |
||
54 | |||
55 | /** |
||
56 | * Model object record. |
||
57 | * |
||
58 | * @var ModelInterface |
||
59 | */ |
||
60 | private $model; |
||
61 | |||
62 | /** |
||
63 | * Search new model object. |
||
64 | * |
||
65 | * @var ActiveRecordInterface |
||
66 | */ |
||
67 | private $searchModel; |
||
68 | |||
69 | /** |
||
70 | * Validate component. |
||
71 | * |
||
72 | * @var ValidateComponentInterface |
||
73 | */ |
||
74 | private $validateComponent = null; |
||
75 | |||
76 | /** |
||
77 | * Returns the name of the base model. |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | abstract protected function getModelName():string; |
||
82 | |||
83 | /** |
||
84 | * Returns the name of the model to search it. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | abstract protected function getSearchModelName():string; |
||
89 | |||
90 | /** |
||
91 | * @inheritdoc |
||
92 | */ |
||
93 | public function behaviors() |
||
115 | |||
116 | /** |
||
117 | * Initializer. |
||
118 | */ |
||
119 | public function init() |
||
123 | |||
124 | /** |
||
125 | * Give ability of configure view to the module class. |
||
126 | * |
||
127 | * @return \yii\base\View|\yii\web\View |
||
128 | */ |
||
129 | public function getView() |
||
137 | |||
138 | /** |
||
139 | * Set model. |
||
140 | * |
||
141 | * @param $model ModelInterface |
||
142 | */ |
||
143 | public function setModel(ModelInterface $model): void |
||
147 | |||
148 | /** |
||
149 | * Set search model. |
||
150 | * |
||
151 | * @param $model ActiveRecordInterface |
||
152 | */ |
||
153 | public function setSearchModel(ActiveRecordInterface $model): void |
||
157 | |||
158 | /** |
||
159 | * Set validate component for main model. |
||
160 | * |
||
161 | * @param $component ValidateComponentInterface |
||
162 | */ |
||
163 | public function setValidateComponent(ValidateComponentInterface $component): void |
||
167 | |||
168 | /** |
||
169 | * Returns model. |
||
170 | * |
||
171 | * @return ModelInterface |
||
172 | */ |
||
173 | public function getModel(): ModelInterface |
||
177 | |||
178 | /** |
||
179 | * Returns search model. |
||
180 | * |
||
181 | * @return ActiveRecordInterface |
||
182 | */ |
||
183 | public function getSearchModel(): ActiveRecordInterface |
||
187 | |||
188 | /** |
||
189 | * Get validate component for main model. |
||
190 | * |
||
191 | * @return ValidateComponentInterface |
||
192 | */ |
||
193 | public function getValidateComponent(): ValidateComponentInterface |
||
197 | |||
198 | /** |
||
199 | * List of records. |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | public function actionIndex() |
||
219 | |||
220 | /** |
||
221 | * Displays one model entry. |
||
222 | * |
||
223 | * @param int|string $id |
||
224 | * |
||
225 | * @return mixed |
||
226 | */ |
||
227 | public function actionView($id) |
||
238 | |||
239 | /** |
||
240 | * Creates a new model record. |
||
241 | * If the result of creation is successful, there will be a redirect to the 'view' or 'index' page. |
||
242 | * |
||
243 | * @return string|\yii\web\Response |
||
244 | */ |
||
245 | public function actionCreate() |
||
277 | |||
278 | /** |
||
279 | * Updates the current model entry. |
||
280 | * If the result of creation is successful, there will be a redirect to the 'view' or 'index' page. |
||
281 | * |
||
282 | * @param int|string $id |
||
283 | * |
||
284 | * @return string|\yii\web\Response |
||
285 | */ |
||
286 | public function actionUpdate($id) |
||
310 | |||
311 | /** |
||
312 | * Deletes the current model entry. |
||
313 | * If the result of the deletion is successful, there will be a redirect to the 'index' page. |
||
314 | * |
||
315 | * @param int|string $id |
||
316 | * |
||
317 | * @return \yii\web\Response |
||
318 | * |
||
319 | * @throws ConflictHttpException |
||
320 | */ |
||
321 | public function actionDelete($id) |
||
333 | |||
334 | /** |
||
335 | * Set addition attributes for model. |
||
336 | * |
||
337 | * @return bool |
||
338 | */ |
||
339 | protected function setAdditionAttributes(): bool |
||
345 | |||
346 | /** |
||
347 | * Get addition fields for the view template. |
||
348 | * @return array |
||
349 | */ |
||
350 | protected function getAdditionFields(): array |
||
354 | |||
355 | /** |
||
356 | * Returns new object of main model. |
||
357 | * |
||
358 | * @return mixed |
||
359 | */ |
||
360 | protected function getNewModel() |
||
365 | |||
366 | /** |
||
367 | * Returns new object of search main model. |
||
368 | * |
||
369 | * @return mixed |
||
370 | */ |
||
371 | protected function getNewSearchModel() |
||
376 | |||
377 | /** |
||
378 | * Find model record. |
||
379 | * If the model is not found, a 404 HTTP exception will be displayed. |
||
380 | * |
||
381 | * @param int|string $key |
||
382 | * |
||
383 | * |
||
384 | * @throws BadRequestHttpException |
||
385 | * @throws UnknownMethodException |
||
386 | * @throws NotFoundHttpException |
||
387 | * |
||
388 | * @return mixed |
||
389 | */ |
||
390 | private function findModel($key) |
||
414 | |||
415 | /** |
||
416 | * Returns an intermediate model for working with the main. |
||
417 | * |
||
418 | * @param int|string|null $key |
||
419 | * |
||
420 | * @return void |
||
421 | */ |
||
422 | private function setModelByConditions($key = null): void |
||
434 | } |
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..