| Conditions | 5 |
| Paths | 2 |
| Total Lines | 85 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 76 | public function rules() |
||
| 77 | { |
||
| 78 | return [ |
||
| 79 | [ |
||
| 80 | [ |
||
| 81 | 'categoryId', |
||
| 82 | 'active', |
||
| 83 | 'alias', |
||
| 84 | 'price', |
||
| 85 | ], |
||
| 86 | 'required' |
||
| 87 | ], |
||
| 88 | [ |
||
| 89 | [ |
||
| 90 | 'categoryId', |
||
| 91 | 'active', |
||
| 92 | ], |
||
| 93 | 'integer' |
||
| 94 | ], |
||
| 95 | [ |
||
| 96 | [ |
||
| 97 | 'created_at', |
||
| 98 | 'updated_at' |
||
| 99 | ], |
||
| 100 | 'safe' |
||
| 101 | ], |
||
| 102 | [ |
||
| 103 | [ |
||
| 104 | 'icon', |
||
| 105 | 'alias', |
||
| 106 | ], |
||
| 107 | 'string', |
||
| 108 | 'max' => 128 |
||
| 109 | ], |
||
| 110 | [ |
||
| 111 | [ |
||
| 112 | 'price' |
||
| 113 | ], |
||
| 114 | 'number' |
||
| 115 | ], |
||
| 116 | [ |
||
| 117 | 'alias', |
||
| 118 | 'filter', |
||
| 119 | 'filter' => function ($value) { |
||
| 120 | return preg_replace( '/[^a-z0-9_]+/', '-', strtolower(trim($value))); |
||
| 121 | } |
||
| 122 | ], |
||
| 123 | [ |
||
| 124 | 'alias', |
||
| 125 | 'unique', |
||
| 126 | 'skipOnError' => true, |
||
| 127 | 'targetClass' => static::class, |
||
| 128 | 'filter' => $this->getScenario() == ModelInterface::SCENARIO_UPDATE ? 'id != '.$this->id : '' |
||
| 129 | ], |
||
| 130 | [ |
||
| 131 | [ |
||
| 132 | 'categoryId' |
||
| 133 | ], |
||
| 134 | 'exist', |
||
| 135 | 'skipOnError' => true, |
||
| 136 | 'targetClass' => Category::class, |
||
| 137 | 'targetAttribute' => ['categoryId' => 'id'] |
||
| 138 | ], |
||
| 139 | [ |
||
| 140 | UploadModelInterface::FILE_TYPE_THUMB, |
||
| 141 | function($attribute){ |
||
| 142 | if (!is_numeric($this->{$attribute}) && !is_string($this->{$attribute})){ |
||
| 143 | $this->addError($attribute, 'Tumbnail content must be a numeric or string.'); |
||
| 144 | } |
||
| 145 | }, |
||
| 146 | 'skipOnError' => false, |
||
| 147 | ], |
||
| 148 | [ |
||
| 149 | UploadModelInterface::FILE_TYPE_IMAGE, |
||
| 150 | function($attribute) { |
||
| 151 | if (!is_array($this->{$attribute})) { |
||
| 152 | $this->addError($attribute, 'Image field content must be an array.'); |
||
| 153 | } |
||
| 154 | }, |
||
| 155 | 'skipOnError' => false, |
||
| 156 | ], |
||
| 157 | [ |
||
| 158 | 'albums', |
||
| 159 | 'each', |
||
| 160 | 'rule' => ['integer'], |
||
| 161 | ], |
||
| 272 |