| Conditions | 9 |
| Paths | 1 |
| Total Lines | 59 |
| Code Lines | 36 |
| 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 |
||
| 45 | public function rules() |
||
| 46 | { |
||
| 47 | return [ |
||
| 48 | [ |
||
| 49 | ['cost', 'days'], |
||
| 50 | 'required', |
||
| 51 | ], |
||
| 52 | [ |
||
| 53 | ['cost'], |
||
| 54 | 'number', |
||
| 55 | 'integerOnly' => true, |
||
| 56 | 'min' => 0, |
||
| 57 | 'max' => SimpleOffer::NUMBER_LIMIT, |
||
| 58 | ], |
||
| 59 | [ |
||
| 60 | ['days'], |
||
| 61 | function ($attribute, $params) { |
||
|
|
|||
| 62 | $value = $this->$attribute; |
||
| 63 | $isBigValue = false; |
||
| 64 | if (!preg_match('/^[\d]+(-[\d]+)?$/', $value)) { |
||
| 65 | $this->addError($attribute, 'Wrong ' . $attribute . ' value'); |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | |||
| 69 | if (strpos($value, '-') !== false) { |
||
| 70 | $parts = explode('-', $value); |
||
| 71 | if (count($parts) != 2) { |
||
| 72 | $this->addError($attribute, 'Wrong parts count in ' . $attribute); |
||
| 73 | return; |
||
| 74 | } |
||
| 75 | |||
| 76 | if ((int) $parts[0] >= $parts[1]) { |
||
| 77 | $this->addError($attribute, 'Wrong ' . $attribute . ' value'); |
||
| 78 | return; |
||
| 79 | } |
||
| 80 | |||
| 81 | if ($parts[0] > 31 || $parts[1] > 31) { |
||
| 82 | $isBigValue = true; |
||
| 83 | } |
||
| 84 | } else { |
||
| 85 | if ($value > 31) { |
||
| 86 | $isBigValue = true; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | if ($isBigValue) { |
||
| 91 | $this->addError($attribute, 'Value ' . $attribute . ' is greater than 31'); |
||
| 92 | } |
||
| 93 | }, |
||
| 94 | ], |
||
| 95 | [ |
||
| 96 | ['orderBefore'], |
||
| 97 | 'number', |
||
| 98 | 'integerOnly' => true, |
||
| 99 | 'min' => 0, |
||
| 100 | 'max' => 24, |
||
| 101 | ], |
||
| 102 | ]; |
||
| 103 | } |
||
| 104 | |||
| 153 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.