These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace LaravelFreelancerNL\Aranguent\Schema; |
||
4 | |||
5 | use Illuminate\Database\Schema\Grammars\Grammar as IlluminateGrammar; |
||
6 | use Illuminate\Support\Fluent; |
||
7 | use LaravelFreelancerNL\FluentAQL\Facades\AQB; |
||
8 | |||
9 | class Grammar extends IlluminateGrammar |
||
10 | { |
||
11 | /** |
||
12 | * ArangoDB handles transactions itself. However most schema actions |
||
13 | * are not done through queries but rather through commands. |
||
14 | * So we just run schema actions sequentially. |
||
15 | * |
||
16 | * @var bool |
||
17 | */ |
||
18 | protected $transactions = false; |
||
19 | |||
20 | /** |
||
21 | * Compile AQL to check if an attribute is in use within a document in the collection. |
||
22 | * If multiple attributes are set then all must be set in one document. |
||
23 | * @param string $collection |
||
24 | * @param Fluent $command |
||
25 | * @return Fluent |
||
26 | */ |
||
27 | public function compileHasAttribute($collection, Fluent $command) |
||
28 | { |
||
29 | if (is_string($command->attribute)) { |
||
30 | $command->attribute = [$command->attribute]; |
||
31 | } |
||
32 | |||
33 | $filter = []; |
||
34 | foreach ($command->attribute as $attribute) { |
||
35 | $filter[] = ['doc.'.$attribute, '!=', 'null']; |
||
36 | } |
||
37 | |||
38 | $aqb = AQB::for('doc', $collection) |
||
39 | ->filter($filter) |
||
40 | ->limit(1) |
||
41 | ->return('true') |
||
42 | ->get(); |
||
43 | |||
44 | $command->aqb = $aqb; |
||
45 | |||
46 | return $command; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Compile AQL to rename an attribute, if the new name isn't already in use. |
||
51 | * |
||
52 | * @param string $collection |
||
53 | * @param Fluent $command |
||
54 | * @return Fluent |
||
55 | */ |
||
56 | public function compileRenameAttribute($collection, Fluent $command) |
||
57 | { |
||
58 | $bindings['@collection'] = $collection; |
||
0 ignored issues
–
show
|
|||
59 | |||
60 | $bindings['from'] = $this->wrapBindVar($command->to); |
||
61 | $bindings['to'] = $this->wrapBindVar($command->from); |
||
62 | |||
63 | $filter = [ |
||
64 | ['doc.'.$command->from, '!=', 'null'], |
||
65 | ['doc.'.$command->to], |
||
66 | ]; |
||
67 | |||
68 | $aqb = AQB::for('doc', $collection) |
||
69 | ->filter($filter) |
||
70 | ->update( |
||
71 | 'doc', |
||
72 | [ |
||
73 | $command->from => 'null', |
||
74 | $command->to => 'doc.'.$command->from, |
||
75 | ], |
||
76 | $collection) |
||
77 | ->options(['keepNull' => true]) |
||
78 | ->get(); |
||
79 | |||
80 | $command->aqb = $aqb; |
||
81 | |||
82 | return $command; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Compile AQL to drop one or more attributes. |
||
87 | * |
||
88 | * @param string $collection |
||
89 | * @param Fluent $command |
||
90 | * @return Fluent |
||
91 | */ |
||
92 | public function compileDropAttribute($collection, Fluent $command) |
||
93 | { |
||
94 | $filter = []; |
||
95 | if (is_string($command->attributes)) { |
||
96 | $command->attributes = [$command->attributes]; |
||
97 | } |
||
98 | |||
99 | $data = []; |
||
100 | foreach ($command->attributes as $attribute) { |
||
101 | $filter[] = ['doc.'.$attribute, '!=', 'null', 'OR']; |
||
102 | $data[$attribute] = 'null'; |
||
103 | } |
||
104 | $aqb = AQB::for('doc', $collection) |
||
105 | ->filter($filter) |
||
106 | ->update('doc', $data, $collection) |
||
107 | ->options(['keepNull' => false]) |
||
108 | ->get(); |
||
109 | |||
110 | $command->aqb = $aqb; |
||
111 | |||
112 | return $command; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Prepare a bindVar for inclusion in an AQL query. |
||
117 | * |
||
118 | * @param $attribute |
||
119 | * @return string |
||
120 | */ |
||
121 | public function wrapBindVar($attribute) |
||
122 | { |
||
123 | $attribute = trim($attribute); |
||
124 | if (strpos($attribute, '@') === 0) { |
||
125 | $attribute = "`$attribute`"; |
||
126 | } |
||
127 | |||
128 | return $attribute; |
||
129 | } |
||
130 | } |
||
131 |
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.