Conditions | 4 |
Paths | 7 |
Total Lines | 23 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
18 | public function addTerm($object_id, $params) |
||
19 | { |
||
20 | foreach($params as $item) { |
||
21 | $term = $this->getTaxonomyTerm($item); |
||
22 | $data['term_id'] = $term->id; |
||
|
|||
23 | $data['object_id'] = $object_id; |
||
24 | |||
25 | $query = new Query(); |
||
26 | if (!$query->from($this->table)->where($data)->exists($this->getDb())) { |
||
27 | $transaction = $this->getDb()->beginTransaction(); |
||
28 | try { |
||
29 | $this->getDb()->createCommand()->insert($this->table, $data)->execute(); |
||
30 | |||
31 | $term->updateCounters(['total_count' => 1]); |
||
32 | TaxonomyDef::updateAllCounters(['total_count' => 1], [ 'id' => $this->id ]); |
||
33 | |||
34 | $transaction->commit(); |
||
35 | } catch (Exception $e) { |
||
36 | $transaction->rollBack(); |
||
37 | } |
||
38 | } |
||
39 | } |
||
40 | } |
||
41 | } |
||
42 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.