| Total Complexity | 46 |
| Total Lines | 245 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 1 |
Complex classes like FileBehaviour often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FileBehaviour, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class FileBehaviour extends Behavior |
||
| 21 | { |
||
| 22 | /** Массив для хранения файловых атрибутов и их параметров. |
||
| 23 | * Задается через Behaviors в моделе |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | public $attributes = []; |
||
| 27 | |||
| 28 | /** В этот массив помещаются id связанных файлов с текущей моделью для последующейго сохранения. |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private $_values = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Вещаем сохранение данных на события. |
||
| 35 | */ |
||
| 36 | public function events() |
||
| 43 | ]; |
||
| 44 | } |
||
| 45 | |||
| 46 | protected $cachedFiles = []; |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * Метод сохранения в базу связей с файлами. Вызывается после сохранения основной модели AR. |
||
| 51 | * @throws ErrorException |
||
| 52 | * @throws \yii\db\Exception |
||
| 53 | */ |
||
| 54 | |||
| 55 | public function filesSave() |
||
| 56 | { |
||
| 57 | $order = 0; |
||
| 58 | if ($this->_values) { |
||
|
|
|||
| 59 | |||
| 60 | foreach ($this->_values as $field => $ids) { |
||
| 61 | |||
| 62 | Yii::$app->db->createCommand()->update( |
||
| 63 | "{{%file}}", |
||
| 64 | ['object_id' => 0], |
||
| 65 | [ |
||
| 66 | 'class' => $this->owner->className(), |
||
| 67 | 'object_id' => $this->owner->id, |
||
| 68 | 'field' => $field, |
||
| 69 | ] |
||
| 70 | )->execute(); |
||
| 71 | |||
| 72 | if ($ids) foreach ($ids as $id) { |
||
| 73 | if (empty($id)) |
||
| 74 | continue; |
||
| 75 | $file = File::findOne($id); |
||
| 76 | if ($file) { |
||
| 77 | $file->object_id = $this->owner->id; |
||
| 78 | $file->ordering = $order++; |
||
| 79 | $file->save(); |
||
| 80 | if (!$file->save()) { |
||
| 81 | throw new ErrorException('Невозможно обновить объект File.'); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | } |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | public function filesDelete() |
||
| 91 | { |
||
| 92 | File::deleteAll([ |
||
| 93 | 'class' => $this->owner->className(), |
||
| 94 | 'object_id' => $this->owner->id, |
||
| 95 | ]); |
||
| 96 | } |
||
| 97 | |||
| 98 | public function validateRequiredFields() |
||
| 99 | { |
||
| 100 | foreach ($this->attributes as $attributeName => $params) { |
||
| 101 | $attributeIds = $this->getRealAttributeName($attributeName); |
||
| 102 | |||
| 103 | if ( |
||
| 104 | isset($params['required']) && |
||
| 105 | $params['required'] && |
||
| 106 | in_array($this->owner->scenario, $params['requiredOn']) && |
||
| 107 | !in_array($this->owner->scenario, $params['requiredExcept']) && |
||
| 108 | !isset($this->_values[$attributeIds][1]) |
||
| 109 | ) |
||
| 110 | $this->owner->addError($attributeName, $params['requiredMessage']); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Устанавливаем валидаторы. |
||
| 116 | * @param ActiveRecord $owner |
||
| 117 | */ |
||
| 118 | public |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * @inheritdoc |
||
| 180 | */ |
||
| 181 | public function canGetProperty($name, $checkVars = true) |
||
| 182 | { |
||
| 183 | return array_key_exists($name, $this->attributes) ? |
||
| 184 | true : parent::canGetProperty($name, $checkVars); |
||
| 185 | } |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * @inheritdoc |
||
| 190 | */ |
||
| 191 | public function canSetProperty($name, $checkVars = true) |
||
| 197 | } |
||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * @inheritdoc |
||
| 202 | */ |
||
| 203 | public function __get($att_name) |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * @inheritdoc |
||
| 246 | */ |
||
| 247 | public |
||
| 254 | } |
||
| 255 | |||
| 256 | |||
| 257 | /** Отбрасываем постфикс _ids |
||
| 258 | * @param $attribute string |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | private |
||
| 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.