| Conditions | 9 |
| Paths | 72 |
| Total Lines | 57 |
| 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 |
||
| 128 | public function afterSave($event) |
||
| 129 | { |
||
| 130 | if ($this->tagValues === null) { |
||
| 131 | $this->tagValues = $this->owner->{$this->attribute}; |
||
| 132 | } |
||
| 133 | |||
| 134 | if (!$this->owner->getIsNewRecord()) { |
||
| 135 | $this->beforeDelete($event); |
||
| 136 | } |
||
| 137 | |||
| 138 | $names = array_unique(preg_split( |
||
| 139 | '/\s*,\s*/u', |
||
| 140 | preg_replace( |
||
| 141 | '/\s+/u', |
||
| 142 | ' ', |
||
| 143 | is_array($this->tagValues) |
||
| 144 | ? implode(',', $this->tagValues) |
||
| 145 | : $this->tagValues |
||
| 146 | ), |
||
| 147 | -1, |
||
| 148 | PREG_SPLIT_NO_EMPTY |
||
| 149 | )); |
||
| 150 | |||
| 151 | $relation = $this->owner->getRelation($this->relation); |
||
| 152 | $pivot = $relation->via->from[0]; |
||
|
|
|||
| 153 | /** @var ActiveRecord $class */ |
||
| 154 | $class = $relation->modelClass; |
||
| 155 | $rows = []; |
||
| 156 | $updatedTags = []; |
||
| 157 | |||
| 158 | foreach ($names as $name) { |
||
| 159 | $tag = $class::findOne([$this->name => $name]); |
||
| 160 | |||
| 161 | if ($tag === null) { |
||
| 162 | $tag = new $class(); |
||
| 163 | $tag->{$this->name} = $name; |
||
| 164 | } |
||
| 165 | |||
| 166 | if ($this->frequency) { |
||
| 167 | $tag->{$this->frequency}++; |
||
| 168 | } |
||
| 169 | |||
| 170 | if ($tag->save()) { |
||
| 171 | $updatedTags[] = $tag; |
||
| 172 | $rows[] = [$this->owner->getPrimaryKey(), $tag->getPrimaryKey()]; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | if (!empty($rows)) { |
||
| 177 | $this->owner->getDb() |
||
| 178 | ->createCommand() |
||
| 179 | ->batchInsert($pivot, [key($relation->via->link), current($relation->link)], $rows) |
||
| 180 | ->execute(); |
||
| 181 | } |
||
| 182 | |||
| 183 | $this->owner->populateRelation($this->relation, $updatedTags); |
||
| 184 | } |
||
| 185 | |||
| 221 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: