| Conditions | 15 |
| Paths | 6 |
| Total Lines | 56 |
| Code Lines | 29 |
| 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 |
||
| 118 | public |
||
| 119 | function attach($owner) |
||
| 120 | { |
||
| 121 | parent::attach($owner); |
||
| 122 | |||
| 123 | // Получаем валидаторы AR |
||
| 124 | $validators = $owner->validators; |
||
| 125 | |||
| 126 | // Пробегаемся по валидаторам и вычисляем, какие из них касаются наших файл-полей |
||
| 127 | if ($validators) |
||
| 128 | foreach ($validators as $key => $validator) { |
||
| 129 | |||
| 130 | // Сначала пробегаемся по файловым валидаторам |
||
| 131 | if ($validator::className() == 'yii\validators\FileValidator' || $validator::className() == 'floor12\files\components\ReformatValidator') { |
||
| 132 | foreach ($this->attributes as $field => $params) { |
||
| 133 | |||
| 134 | if (is_string($params)) { |
||
| 135 | $field = $params; |
||
| 136 | $params = []; |
||
| 137 | } |
||
| 138 | |||
| 139 | $index = array_search($field, $validator->attributes); |
||
| 140 | if ($index !== false) { |
||
| 141 | $this->attributes[$field]['validator'][$validator::className()] = $validator; |
||
| 142 | unset($validator->attributes[$index]); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | |||
| 148 | if ($validator::className() == 'yii\validators\RequiredValidator') { |
||
| 149 | foreach ($this->attributes as $field => $params) { |
||
| 150 | |||
| 151 | if (is_string($params)) { |
||
| 152 | $field = $params; |
||
| 153 | $params = []; |
||
| 154 | } |
||
| 155 | |||
| 156 | $index = array_search($field, $validator->attributes); |
||
| 157 | if ($index !== false) { |
||
| 158 | unset($validator->attributes[$index]); |
||
| 159 | $this->attributes[$field]['required'] = true; |
||
| 160 | $this->attributes[$field]['requiredExcept'] = $validator->except; |
||
| 161 | $this->attributes[$field]['requiredOn'] = sizeof($validator->on) ? $validator->on : [ActiveRecord::SCENARIO_DEFAULT]; |
||
| 162 | $this->attributes[$field]['requiredMessage'] = str_replace("{attribute}", $this->owner->getAttributeLabel($field), $validator->message); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | |||
| 168 | } |
||
| 169 | |||
| 170 | // Добавляем дефолтный валидатор для прилетающих айдишников |
||
| 171 | if ($this->attributes) foreach ($this->attributes as $fieldName => $fieldParams) { |
||
| 172 | $validator = Validator::createValidator('safe', $owner, ["{$fieldName}_ids"]); |
||
| 173 | $validators->append($validator); |
||
| 174 | } |
||
| 267 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.