Conditions | 14 |
Paths | 183 |
Total Lines | 63 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
128 | public function mergeParams(array $params, bool $autoUpdate = false) |
||
129 | { |
||
130 | $paramModel = Yii::$app->get('systemparams')->modelMap['systemParam']['class']; |
||
131 | |||
132 | $arrayOfKeys = $this->getArrayKeysRecursivelyInString($params); |
||
133 | |||
134 | $records = $paramModel::find()->all(); |
||
135 | |||
136 | foreach ($records as $i => $model) { |
||
137 | $param = $model->{$paramModel::attributesMap()['fieldParamKey']}; |
||
138 | if (self::PARAM_IS_NOT_SET !== $this->getParamValueByKey($param, $params)) { |
||
139 | $records[$param] = clone $model; |
||
140 | } else { |
||
141 | $model->delete(); |
||
142 | } |
||
143 | unset($records[$i]); |
||
144 | } |
||
145 | |||
146 | foreach ($arrayOfKeys as $paramKey) { |
||
147 | /** @var ActiveRecord $model */ |
||
148 | $model = new $paramModel(); |
||
149 | if (isset($records[$paramKey])) { |
||
150 | $model->setIsNewRecord(false); |
||
151 | $model->setAttributes($records[$paramKey]->getAttributes(), false); |
||
152 | } |
||
153 | |||
154 | $model->{$paramModel::attributesMap()['fieldParamKey']} = $paramKey; |
||
155 | |||
156 | $value = $this->getParamValueByKey($paramKey, $params); |
||
157 | |||
158 | if (false === empty($value)) { |
||
159 | $model->{$paramModel::attributesMap()['fieldParamValue']} = is_bool($value['value']) ? intval($value['value']) : $value['value']; |
||
160 | $model->{$paramModel::attributesMap()['fieldValidator']} = isset($value['validator']) ? $value['validator'] : 'string'; |
||
161 | $model->{$paramModel::attributesMap()['fieldDescription']} = isset($value['description']) ? $value['description'] : ''; |
||
162 | } else { |
||
163 | $model->{$paramModel::attributesMap()['fieldParamValue']} = is_bool($value) ? intval($value) : $value; |
||
164 | $model->{$paramModel::attributesMap()['fieldValidator']} = 'string'; |
||
165 | $model->{$paramModel::attributesMap()['fieldDescription']} = ''; |
||
166 | } |
||
167 | |||
168 | if ($model->isNewRecord || (count(array_diff( |
||
169 | [ |
||
170 | $model->{$paramModel::attributesMap()['fieldParamValue']}, |
||
171 | $model->{$paramModel::attributesMap()['fieldValidator']}, |
||
172 | $model->{$paramModel::attributesMap()['fieldDescription']}, |
||
173 | ], |
||
174 | [ |
||
175 | $records[$paramKey]->{$paramModel::attributesMap()['fieldParamValue']}, |
||
176 | $records[$paramKey]->{$paramModel::attributesMap()['fieldValidator']}, |
||
177 | $records[$paramKey]->{$paramModel::attributesMap()['fieldDescription']}, |
||
178 | ] |
||
179 | )) && $autoUpdate) |
||
180 | ) { |
||
181 | if (false == $model->save()) { |
||
182 | Yii::error($paramKey . ' ' . Json::encode($model->getErrors()), 'system.systemparam'); |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | |||
187 | self::flushCache(); |
||
188 | |||
189 | return true; |
||
190 | } |
||
191 | |||
282 |
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..