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 |
||
21 | class MultilanguageValidateModel extends Model implements ModelInterface |
||
22 | { |
||
23 | /** |
||
24 | * Dynamic fields from which the translated fields are formed. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | public $dynamicFields = []; |
||
29 | |||
30 | /** |
||
31 | * Basic data model. |
||
32 | * |
||
33 | * @var BaseActiveRecord|MultilanguageTrait |
||
34 | */ |
||
35 | private $mainModel; |
||
36 | |||
37 | /** |
||
38 | * Validation rules for all fields together with dynamic. |
||
39 | * |
||
40 | * @return array |
||
41 | */ |
||
42 | public function rules(): array |
||
43 | { |
||
44 | return ArrayHelper::merge( |
||
45 | $this->getDynamicValidationRules(), |
||
46 | $this->mainModel->rules() |
||
|
|||
47 | ); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Scenarios. |
||
52 | * |
||
53 | * @return array |
||
54 | */ |
||
55 | View Code Duplication | public function scenarios(): array |
|
56 | { |
||
57 | return [ |
||
58 | ModelInterface::SCENARIO_CREATE => $this->attributes(), |
||
59 | ModelInterface::SCENARIO_UPDATE => $this->attributes(), |
||
60 | self::SCENARIO_DEFAULT => $this->attributes(), |
||
61 | ]; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Labels of all fields. |
||
66 | * |
||
67 | * @inheritdoc |
||
68 | */ |
||
69 | public function attributeLabels() |
||
70 | { |
||
71 | $dynamicAttributeLabels = []; |
||
72 | $staticAttributeLabels = []; |
||
73 | |||
74 | $translateAttributeLabels = call_user_func([ |
||
75 | $this->mainModel->getTranslateModelName(), |
||
76 | 'attributeLabels', |
||
77 | ]); |
||
78 | |||
79 | foreach ($this->dynamicFields as $fieldConditions) { |
||
80 | |||
81 | $fieldName = $fieldConditions['name']; |
||
82 | |||
83 | if (array_key_exists($fieldName, $translateAttributeLabels)) { |
||
84 | |||
85 | $staticAttributeLabels[$fieldName] = $translateAttributeLabels[$fieldName]; |
||
86 | |||
87 | foreach ($this->getShortLanguageList() as $language) { |
||
88 | $dynamicAttributeLabels[$fieldName.'_'.$language] = $translateAttributeLabels[$fieldName]; |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | |||
93 | return ArrayHelper::merge( |
||
94 | ArrayHelper::merge( |
||
95 | $dynamicAttributeLabels, |
||
96 | $staticAttributeLabels |
||
97 | ), |
||
98 | $this->mainModel->attributeLabels() |
||
99 | ); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Specifies the value of the field. |
||
104 | * |
||
105 | * @param string $name - name of field. |
||
106 | * @param mixed $value - value to be stored in field. |
||
107 | * |
||
108 | * @return void |
||
109 | */ |
||
110 | public function __set($name, $value) |
||
111 | { |
||
112 | $setter = 'set' . $name; |
||
113 | if (method_exists($this, $setter)) { |
||
114 | $this->$setter($value); |
||
115 | } else { |
||
116 | $this->{$name} = $value; |
||
117 | } |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Gets the value of the field. |
||
122 | * |
||
123 | * @param string $name - field name. |
||
124 | * |
||
125 | * @return mixed |
||
126 | */ |
||
127 | public function __get($name) |
||
128 | { |
||
129 | $getter = 'get' . $name; |
||
130 | if (method_exists($this, $getter)) { |
||
131 | return $this->$getter(); |
||
132 | } |
||
133 | |||
134 | if ($this->mainModel->isNewRecord) { |
||
135 | return $this->{$name} ?? ''; |
||
136 | } else { |
||
137 | return $this->mainModel->{$name} ?? ''; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Setter for main model. |
||
143 | * |
||
144 | * @param BaseActiveRecord $mainModel |
||
145 | */ |
||
146 | public function setMainModel(BaseActiveRecord $mainModel) |
||
150 | |||
151 | /** |
||
152 | * Getter for main model. |
||
153 | * |
||
154 | * @return mixed |
||
155 | */ |
||
156 | public function getMainModel() |
||
160 | |||
161 | /** |
||
162 | * Attributes along with dynamic and from the basic model. |
||
163 | * |
||
164 | * @return array |
||
165 | */ |
||
166 | public function attributes(): array |
||
182 | |||
183 | /** |
||
184 | * Saves data in the main model. |
||
185 | * |
||
186 | * @return bool |
||
187 | */ |
||
188 | public function save(): bool |
||
208 | |||
209 | /** |
||
210 | * Returns the id of the current model. |
||
211 | * |
||
212 | * @return int |
||
213 | */ |
||
214 | public function getId() |
||
218 | |||
219 | /** |
||
220 | * Returns an array of all multilanguage attributes. |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | private function getDynamicAttributes(): array |
||
235 | |||
236 | /** |
||
237 | * Creates validation rules for dynamic fields for all languages. |
||
238 | * |
||
239 | * @return array |
||
240 | */ |
||
241 | private function getDynamicValidationRules(): array |
||
283 | |||
284 | /** |
||
285 | * Returns the list of available languages in the short name format. |
||
286 | * |
||
287 | * @return array |
||
288 | */ |
||
289 | private function getShortLanguageList(): array |
||
293 | } |
||
294 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: