Conditions | 4 |
Paths | 6 |
Total Lines | 27 |
Code Lines | 14 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 20 |
Changes | 0 |
1 | <?php |
||
46 | public function init() |
||
47 | { |
||
48 | $data = (object)ParamsExpander::expand($this, ['keys', 'options']); |
||
49 | |||
50 | $entity = $this->getEntity(); |
||
51 | $name = $entity->name; |
||
52 | if(empty($data->keys)) |
||
53 | { |
||
54 | $keys[$name] = Sort::SortAsc; |
||
|
|||
55 | } |
||
56 | else |
||
57 | { |
||
58 | if(!is_array($data->keys)) |
||
59 | { |
||
60 | $keys[$name] = $data->keys; |
||
61 | } |
||
62 | else |
||
63 | { |
||
64 | $keys = $data->keys; |
||
65 | } |
||
66 | } |
||
67 | if(empty($data->options)) |
||
68 | { |
||
69 | $data->options = []; |
||
70 | } |
||
71 | $entity->index[] = new IndexMeta($keys, $data->options); |
||
72 | } |
||
73 | |||
75 |
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.