| Conditions | 10 |
| Paths | 3 |
| Total Lines | 49 |
| Code Lines | 30 |
| 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 |
||
| 29 | public function addTerm($object_id, $params) |
||
| 30 | { |
||
| 31 | $cachedParents = []; |
||
| 32 | |||
| 33 | $addTerm = function ($parent, $item) use ($object_id, &$cachedParents, &$addTerm) { |
||
| 34 | if ($this->detectLoop($parent, $item)) { |
||
| 35 | throw new InvalidCallException('Loop detected! Cannot add parent as a child!'); |
||
| 36 | } |
||
| 37 | |||
| 38 | $term = $this->getTaxonomyTerm($item); |
||
| 39 | if (array_key_exists($parent, $cachedParents)) { |
||
| 40 | $term->parent_id = $cachedParents[$parent]->id; |
||
|
|
|||
| 41 | } else if (is_string($parent)) { |
||
| 42 | $parentTerm = $this->getTaxonomyTerm($parent); |
||
| 43 | $cachedParents[$parent] = $parentTerm; |
||
| 44 | $term->parent_id = $parentTerm->id; |
||
| 45 | $addTerm(null, $parent); // Assign object id to the parent as well! |
||
| 46 | } |
||
| 47 | |||
| 48 | if ($term->getDirtyAttributes(['parent_id'])) { |
||
| 49 | $term->save(false); |
||
| 50 | } |
||
| 51 | |||
| 52 | if ($object_id) { |
||
| 53 | $data['term_id'] = $term->id; |
||
| 54 | $data['object_id'] = $object_id; |
||
| 55 | |||
| 56 | if (!(new Query())->from($this->table)->where($data)->exists(CategoryTerm::getDb())) { |
||
| 57 | Yii::$app->db->transaction(function() use ($data, $term) { |
||
| 58 | CategoryTerm::getDb()->createCommand()->insert($this->table, $data)->execute(); |
||
| 59 | |||
| 60 | $term->updateCounters(['total_count' => 1]); |
||
| 61 | TaxonomyDef::updateAllCounters(['total_count' => 1], ['id' => $this->id]); |
||
| 62 | }); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | }; |
||
| 66 | |||
| 67 | $params = (array) $params; |
||
| 68 | foreach ($params as $parent => $item) { |
||
| 69 | if (is_array($item)) { |
||
| 70 | foreach ($item as $child) { |
||
| 71 | $addTerm($parent, $child); |
||
| 72 | } |
||
| 73 | } else { |
||
| 74 | $addTerm($parent, $item); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 133 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.