Conditions | 104 |
Paths | > 20000 |
Total Lines | 243 |
Code Lines | 187 |
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 |
||
169 | public function getAutoRules($rules, $config, $attribute = '') |
||
170 | { |
||
171 | static $columns = null; |
||
172 | |||
173 | $data = []; |
||
174 | |||
175 | switch ($attribute) { |
||
176 | case 'id': |
||
177 | case 'created_at': |
||
178 | case 'createdAt': |
||
179 | case 'modified_at': |
||
180 | case 'modifiedAt': |
||
181 | return $data; |
||
182 | break; |
||
183 | } |
||
184 | |||
185 | $type = (isset($config['active']['type']) ? $config['active']['type'] : ''); |
||
186 | if ($type == InputField::INPUT_STATIC) { |
||
187 | return $data; |
||
188 | } |
||
189 | |||
190 | $columns = ($columns === null ? self::getTableSchema()->columns : $columns); |
||
191 | |||
192 | if (isset($columns[$attribute])) { |
||
193 | $spec = (isset($columns[$attribute]) ? $columns[$attribute] : false); |
||
194 | |||
195 | $max = -1; |
||
196 | $noTrim = false; |
||
197 | $defaultOnEmpty = null; |
||
198 | $isString = true; |
||
199 | $isMultiple = false; |
||
200 | $dateFormat = ''; |
||
201 | $addRules = []; |
||
202 | |||
203 | $isInteger = false; |
||
204 | switch ($spec->type) { |
||
205 | case 'int': |
||
206 | case 'integer': |
||
207 | case 'tinyint': |
||
208 | case 'smallint': |
||
209 | case 'mediumint': |
||
210 | case '__bigint': // not included here as cast as will have been cast as a string |
||
211 | $isInteger = true; |
||
212 | break; |
||
213 | default: |
||
214 | } |
||
215 | |||
216 | if ($type == '') { |
||
217 | $type = InputField::getDefaultInputFieldType($attribute, $spec, (isset($config['active']) ? $config['active'] : null)); |
||
218 | if ($type == InputField::INPUT_STATIC) { |
||
219 | return $data; |
||
220 | } |
||
221 | } |
||
222 | |||
223 | switch ($type) { |
||
224 | case InputField::INPUT_TEXT: |
||
225 | case InputField::INPUT_PASSWORD: |
||
226 | $max = (isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size); |
||
227 | break; |
||
228 | case InputField::INPUT_PASSWORD_STRENGTH: |
||
229 | //wlchere - alternative or implement correctly again |
||
230 | //$max = (isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size); |
||
231 | //$addRules[] = ['check' => [StrengthValidator::className()], 'rule' => [StrengthValidator::className(), 'preset' => 'normal', 'hasUser' => false, 'hasEmail' => false, 'userAttribute' => false]]; |
||
232 | break; |
||
233 | case InputField::INPUT_INTEGER: |
||
234 | if ($spec) { |
||
235 | $max = (isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size); |
||
236 | $unsigned = $spec->unsigned; |
||
237 | } else { |
||
238 | $max = $config['active']['options']['maxlength']; |
||
239 | $unsigned = (!isset($config['active']['options']['data-inputmask']['allowMinus']) || !$config['active']['options']['data-inputmask']['allowMinus'] ? true : false); |
||
240 | } |
||
241 | $maxValue = pow(10, $max) - 1; |
||
242 | $minValue = ($unsigned ? 0 : -1 * $maxValue); |
||
243 | $isString = false; |
||
244 | $defaultOnEmpty = 0; |
||
245 | if ($maxValue > 2147483646) { |
||
246 | // bigint for example is cast as a string |
||
247 | } else { |
||
248 | $addRules[] = [ |
||
249 | 'check' => [FilterValidator::className(), 'filter' => 'intval'], |
||
250 | 'rule' => [FilterValidator::className(), 'filter' => 'intval'], |
||
251 | ]; |
||
252 | } |
||
253 | $addRules[] = ['check' => ['integer'], 'rule' => ['integer', 'max' => $maxValue, 'min' => $minValue]]; |
||
254 | if (!$unsigned) { |
||
255 | $max++; |
||
256 | } |
||
257 | break; |
||
258 | case InputField::INPUT_DECIMAL: |
||
259 | if ($spec) { |
||
260 | $max = (isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size + 1); // allow for decimal point |
||
261 | $unsigned = $spec->unsigned; |
||
262 | $scale = $spec->scale; |
||
263 | } else { |
||
264 | $max = $config['active']['options']['maxlength']; |
||
265 | $unsigned = (!isset($config['active']['options']['data-inputmask']['allowMinus']) || !$config['active']['options']['data-inputmask']['allowMinus'] ? true : false); |
||
266 | $scale = (isset($config['active']['options']['data-inputmask']['digits']) ? $config['active']['options']['data-inputmask']['digits'] : 2); |
||
267 | } |
||
268 | $maxValue = pow(10, ($max - 1)) - 0.01; |
||
269 | $minValue = ($unsigned ? 0 : -1 * $maxValue); |
||
270 | $noTrim = true; |
||
271 | $defaultOnEmpty = '0.00'; |
||
272 | $addRules[] = ['check' => ['double'], 'rule' => ['double', 'max' => $maxValue, 'min' => $minValue]]; |
||
273 | $addRules[] = ['check' => [FilterValidator::className()], 'rule' => [FilterValidator::className(), 'filter' => 'number_format', 'args' => [$scale, '.', '']]]; |
||
274 | if (!$unsigned) { |
||
275 | $max++; |
||
276 | } |
||
277 | break; |
||
278 | case InputField::INPUT_TEXTAREA: |
||
279 | $max = (isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size); |
||
280 | break; |
||
281 | case InputField::INPUT_CHECKBOX: |
||
282 | case InputField::INPUT_CHECKBOX_BASIC: |
||
283 | case InputField::INPUT_CHECKBOX_SWITCH: |
||
284 | case InputField::INPUT_CHECKBOX_ICHECK: |
||
285 | $isString = false; |
||
286 | $addRules[] = ['check' => ['boolean'], 'rule' => ['boolean']]; |
||
287 | $defaultOnEmpty = 0; |
||
288 | if ($spec && $isInteger) { |
||
289 | if ($spec->size == 1) { |
||
290 | $addRules[] = [ |
||
291 | 'check' => [FilterValidator::className(), 'filter' => 'intval'], |
||
292 | 'rule' => [FilterValidator::className(), 'filter' => 'intval'], |
||
293 | ]; |
||
294 | } |
||
295 | } |
||
296 | break; |
||
297 | case InputField::INPUT_DATE: |
||
298 | $defaultOnEmpty = Tools::DATE_DB_EMPTY; |
||
299 | $max = 10; |
||
300 | $dateFormat = 'Y-m-d'; |
||
301 | break; |
||
302 | case InputField::INPUT_DATETIME: |
||
303 | $defaultOnEmpty = Tools::DATE_TIME_DB_EMPTY; |
||
304 | $max = 19; |
||
305 | $dateFormat = 'Y-m-d H:i:s'; |
||
306 | break; |
||
307 | case InputField::INPUT_TIME: |
||
308 | $defaultOnEmpty = Tools::TIME_DB_EMPTY; |
||
309 | $max = 8; |
||
310 | $dateFormat = 'H:i:s'; |
||
311 | break; |
||
312 | case InputField::INPUT_YEAR: |
||
313 | $defaultOnEmpty = Tools::YEAR_DB_EMPTY; |
||
314 | $max = 4; |
||
315 | $dateFormat = 'Y'; |
||
316 | $addRules[] = ['check' => ['integer'], 'rule' => ['integer', 'max' => Tools::YEAR_DB_MAX, 'min' => Tools::YEAR_DB_EMPTY]]; |
||
317 | break; |
||
318 | case InputField::INPUT_COLOR: |
||
319 | $max = (!$spec || $spec->size > 21 ? 21 : $spec->size); |
||
320 | break; |
||
321 | case InputField::INPUT_MINI_COLORS: |
||
322 | $max = (!$spec || $spec->size > 25 ? 25 : $spec->size); |
||
323 | break; |
||
324 | case InputField::INPUT_SELECT_PICKER_MULTI: |
||
325 | case InputField::INPUT_CHECKBOX_LIST: |
||
326 | case InputField::INPUT_CHECKBOX_LIST_ICHECK: |
||
327 | case InputField::INPUT_MULTISELECT: |
||
328 | case InputField::INPUT_SELECT2_MULTI: |
||
329 | $isMultiple = true; |
||
330 | break; |
||
331 | case InputField::INPUT_SELECT2: |
||
332 | case InputField::INPUT_DROPDOWN_LIST: |
||
333 | case InputField::INPUT_LIST_BOX: |
||
334 | case InputField::INPUT_SELECT_PICKER: |
||
335 | case InputField::INPUT_SELECT_SPLITTER: |
||
336 | if (isset($config['active']['options']['multiple']) && $config['active']['options']['multiple']) { |
||
337 | $isMultiple = true; |
||
338 | } else { |
||
339 | if ($spec && $isInteger) { |
||
340 | $defaultOnEmpty = '0'; |
||
341 | $addRules[] = [ |
||
342 | 'check' => [FilterValidator::className(), 'filter' => 'intval'], |
||
343 | 'rule' => [FilterValidator::className(), 'filter' => 'intval'], |
||
344 | ]; |
||
345 | } |
||
346 | } |
||
347 | break; |
||
348 | case InputField::INPUT_SELECT2_TAGS: |
||
349 | // received as a pipe delimited string |
||
350 | $max = (isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size); |
||
351 | break; |
||
352 | case InputField::INPUT_RADIO_LIST: |
||
353 | case InputField::INPUT_RADIO_LIST_ICHECK: |
||
354 | case InputField::INPUT_RADIO: |
||
355 | break; |
||
356 | case InputField::INPUT_EDITOR_CK: |
||
357 | case InputField::INPUT_EDITOR_BS_WYSIHTML5: |
||
358 | case InputField::INPUT_EDITOR_BS_SUMMERNOTE: |
||
359 | break; |
||
360 | default: |
||
361 | break; |
||
362 | } |
||
363 | |||
364 | if ($isMultiple) { |
||
365 | // apply default conversion of multiple selections array into pipe delimited string |
||
366 | $addRules[] = ['check' => [FilterValidator::className()], 'rule' => [FilterValidator::className(), 'filter' => 'implode', 'makeArray' => true, 'argsFirst' => true, 'args' => ['|']]]; |
||
367 | } |
||
368 | |||
369 | if ($defaultOnEmpty !== null) { |
||
370 | if (!$this->checkHasRule($rules, $attribute, 'default' , 'value')) { |
||
371 | $data[] = ['default', 'value' => $defaultOnEmpty]; |
||
372 | } |
||
373 | } |
||
374 | |||
375 | if ($isString && $max != -1) { |
||
376 | if (!$this->checkHasRule($rules, $attribute, 'string', 'max')) { |
||
377 | if (isset($config['active']['options']['maxlength'])) { |
||
378 | $max = $config['active']['options']['maxlength']; |
||
379 | } |
||
380 | $data[] = ['string', 'max' => ($max ? intval($max) : $spec->size)]; |
||
381 | } |
||
382 | if (!$noTrim && !$this->checkHasRule($rules, $attribute, 'filter', 'filter', 'trim')) { |
||
383 | $data[] = ['filter', 'filter' => 'trim']; |
||
384 | } |
||
385 | } |
||
386 | |||
387 | if ($dateFormat != '') { |
||
388 | if (!$this->checkHasRule($rules, $attribute, 'date')) { |
||
389 | $data[] = [DateValidator::className(), 'format' => 'php:' . $dateFormat]; |
||
390 | } |
||
391 | } |
||
392 | |||
393 | if ($addRules) { |
||
394 | foreach ($addRules as $rule) { |
||
395 | $skipRule = false; |
||
396 | if (isset($rule['check']) && $rule['check']) { |
||
397 | if (is_array($rule['check']) && $this->checkHasRule($rules, $attribute, $rule['check'][0] , (isset($rule['check'][1]) ? $rule['check'][1] : ''), (isset($rule['check'][2]) ? $rule['check'][2] : null))) { |
||
398 | $skipRule = true; |
||
399 | } elseif ($this->checkHasRule($rules, $attribute, $rule['check'])) { |
||
400 | $skipRule = true; |
||
401 | } |
||
402 | } |
||
403 | if (!$skipRule) { |
||
404 | $data[] = (isset($rule['rule']) ? $rule['rule'] : $rule); |
||
405 | } |
||
406 | } |
||
407 | } |
||
408 | |||
409 | } |
||
410 | return $data; |
||
411 | } |
||
412 | |||
789 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.