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 ArangoDBClient\View; |
||
6 | use Closure; |
||
7 | use Illuminate\Support\Fluent; |
||
8 | use LaravelFreelancerNL\Aranguent\Connection; |
||
9 | |||
10 | class Builder |
||
11 | { |
||
12 | /** |
||
13 | * The database connection instance. |
||
14 | * |
||
15 | * @var \Illuminate\Database\Connection |
||
16 | */ |
||
17 | protected $connection; |
||
18 | |||
19 | protected $collectionHandler; |
||
20 | |||
21 | protected $graphHandler; |
||
22 | |||
23 | protected $viewHandler; |
||
24 | |||
25 | /** |
||
26 | * The schema grammar instance. |
||
27 | * |
||
28 | * @var Grammar |
||
29 | */ |
||
30 | protected $grammar; |
||
31 | |||
32 | /** |
||
33 | * The Blueprint resolver callback. |
||
34 | * |
||
35 | * @var \Closure |
||
36 | */ |
||
37 | protected $resolver; |
||
38 | |||
39 | /** |
||
40 | * Create a new database Schema manager. |
||
41 | * |
||
42 | * Builder constructor. |
||
43 | * @param Connection $connection |
||
44 | */ |
||
45 | public function __construct(Connection $connection) |
||
46 | { |
||
47 | $this->connection = $connection; |
||
48 | |||
49 | $this->grammar = $connection->getSchemaGrammar(); |
||
50 | |||
51 | $this->collectionHandler = $connection->getCollectionHandler(); |
||
52 | |||
53 | $this->graphHandler = $connection->getGraphHandler(); |
||
54 | |||
55 | $this->viewHandler = $connection->getViewHandler(); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Create a new collection on the schema. |
||
60 | * |
||
61 | * @param string $collection |
||
62 | * @param Closure $callback |
||
63 | * @param array $config |
||
64 | * @return void |
||
65 | */ |
||
66 | public function create($collection, Closure $callback, $config = []) |
||
67 | { |
||
68 | $this->build(tap($this->createBlueprint($collection), function ($blueprint) use ($callback, $config) { |
||
69 | $blueprint->create($config); |
||
70 | |||
71 | $callback($blueprint); |
||
72 | })); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Create a new command set with a Closure. |
||
77 | * |
||
78 | * @param string $collection |
||
79 | * @param \Closure|null $callback |
||
80 | * @return Blueprint |
||
81 | */ |
||
82 | protected function createBlueprint($collection, Closure $callback = null) |
||
83 | { |
||
84 | //Prefixes are unnamed in ArangoDB |
||
85 | $prefix = null; |
||
86 | |||
87 | if (isset($this->resolver)) { |
||
88 | return call_user_func($this->resolver, $collection, $callback, $prefix); |
||
89 | } |
||
90 | |||
91 | return new Blueprint($collection, $this->collectionHandler, $callback, $prefix); |
||
92 | } |
||
93 | |||
94 | protected function build(Blueprint $blueprint) |
||
95 | { |
||
96 | $blueprint->build($this->connection, $this->grammar); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Set the Schema Blueprint resolver callback. |
||
101 | * |
||
102 | * @param Closure $resolver |
||
103 | * @return void |
||
104 | */ |
||
105 | public function blueprintResolver(Closure $resolver) |
||
106 | { |
||
107 | $this->resolver = $resolver; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Alias for table. |
||
112 | * |
||
113 | * @param string $collection |
||
114 | * @param Closure $callback |
||
115 | * @return void |
||
116 | */ |
||
117 | public function collection($collection, Closure $callback) |
||
118 | { |
||
119 | $this->collection($collection, $callback); |
||
120 | |||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Modify a table's schema. |
||
125 | * |
||
126 | * @param string $table |
||
127 | * @param Closure $callback |
||
128 | * @return void |
||
129 | */ |
||
130 | public function table($table, Closure $callback) |
||
131 | { |
||
132 | $this->build($this->createBlueprint($table, $callback)); |
||
133 | |||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Drop a collection from the schema. |
||
138 | * |
||
139 | * @param string $collection |
||
140 | * @return void |
||
141 | * @throws \ArangoDBClient\Exception |
||
142 | */ |
||
143 | public function drop($collection) |
||
144 | { |
||
145 | $this->collectionHandler->drop($collection); |
||
146 | } |
||
147 | |||
148 | public function dropAllCollections() |
||
149 | { |
||
150 | $collections = $this->getAllCollections(); |
||
151 | |||
152 | foreach ($collections as $key => $name) { |
||
153 | $this->collectionHandler->drop($name); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Alias for dropAllCollections. |
||
159 | * |
||
160 | * @return void |
||
161 | */ |
||
162 | public function dropAllTables() |
||
163 | { |
||
164 | $this->dropAllCollections(); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param $collection |
||
169 | * @throws \ArangoDBClient\Exception |
||
170 | */ |
||
171 | public function dropIfExists($collection) |
||
172 | { |
||
173 | if ($collections = $this->hasCollection($collection)) { |
||
0 ignored issues
–
show
|
|||
174 | $this->drop($collection); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Get all of the collection names for the database. |
||
180 | * |
||
181 | * @param array $options |
||
182 | * @return array |
||
183 | * @throws \ArangoDBClient\ClientException |
||
184 | * @throws \ArangoDBClient\Exception |
||
185 | */ |
||
186 | protected function getAllCollections(array $options = []) |
||
187 | { |
||
188 | if (! isset($options['excludeSystem'])) { |
||
189 | $options['excludeSystem'] = true; |
||
190 | } |
||
191 | |||
192 | return $this->collectionHandler->getAllCollections($options); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Get all of the table names for the database. |
||
197 | * Alias for getAllCollections(). |
||
198 | * |
||
199 | * @param array $options |
||
200 | * @return array |
||
201 | * @throws \ArangoDBClient\ClientException |
||
202 | * @throws \ArangoDBClient\Exception |
||
203 | */ |
||
204 | protected function getAllTables(array $options = []) |
||
205 | { |
||
206 | return $this->getAllCollections($options); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @param string $collection |
||
211 | * @return \ArangoDBClient\Collection |
||
212 | * @throws \ArangoDBClient\Exception |
||
213 | */ |
||
214 | protected function getCollection($collection) |
||
215 | { |
||
216 | return $this->collectionHandler->get($collection); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Rename a collection. |
||
221 | * |
||
222 | * @param $from |
||
223 | * @param $to |
||
224 | * @return bool |
||
225 | * @throws \ArangoDBClient\Exception |
||
226 | */ |
||
227 | public function rename($from, $to) |
||
228 | { |
||
229 | return $this->collectionHandler->rename($from, $to); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Determine if the given collection exists. |
||
234 | * |
||
235 | * @param string $collection |
||
236 | * @return bool |
||
237 | * @throws \ArangoDBClient\Exception |
||
238 | */ |
||
239 | public function hasCollection(string $collection) |
||
240 | { |
||
241 | return $this->collectionHandler->has($collection); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Alias for hasCollection. |
||
246 | * |
||
247 | * @param string $table |
||
248 | * @return bool |
||
249 | * @throws \ArangoDBClient\Exception |
||
250 | */ |
||
251 | public function hasTable($table) |
||
252 | { |
||
253 | return $this->hasCollection($table); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Check if any document in the collection has the attribute. |
||
258 | * |
||
259 | * @param string $collection |
||
260 | * @param string $attribute |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function hasAttribute($collection, $attribute) |
||
264 | { |
||
265 | if (is_string($attribute)) { |
||
266 | $attribute = [$attribute]; |
||
267 | } |
||
268 | |||
269 | return $this->hasAttributes($collection, $attribute); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Check if any document in the collection has the attribute. |
||
274 | * |
||
275 | * @param string $collection |
||
276 | * @param array $attributes |
||
277 | * @return bool |
||
278 | */ |
||
279 | public function hasAttributes($collection, $attributes) |
||
280 | { |
||
281 | $parameters['name'] = 'hasAttribute'; |
||
282 | $parameters['handler'] = 'aql'; |
||
283 | $parameters['attribute'] = $attributes; |
||
284 | |||
285 | $command = new Fluent($parameters); |
||
286 | $compilation = $this->grammar->compileHasAttribute($collection, $command); |
||
287 | |||
288 | return $this->connection->statement($compilation['aql']); |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Alias for hasAttribute. |
||
293 | * |
||
294 | * @param string $table |
||
295 | * @param string $column |
||
296 | * @return bool |
||
297 | */ |
||
298 | public function hasColumn($table, $column) |
||
299 | { |
||
300 | return $this->hasAttribute($table, $column); |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Alias for hasAttributes. |
||
305 | * |
||
306 | * @param string $table |
||
307 | * @param array $columns |
||
308 | * @return bool |
||
309 | */ |
||
310 | public function hasColumns($table, $columns) |
||
311 | { |
||
312 | return $this->hasAttributes($table, $columns); |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * get information about the collection. |
||
317 | * |
||
318 | * @param $collection |
||
319 | * @return mixed |
||
320 | */ |
||
321 | public function getCollectionInfo($collection) |
||
322 | { |
||
323 | return $this->figures($collection); |
||
324 | } |
||
325 | |||
326 | |||
327 | /** |
||
328 | * @param string $name |
||
329 | * @param array $properties |
||
330 | * @param string $type |
||
331 | * @throws \ArangoDBClient\ClientException |
||
332 | * @throws \ArangoDBClient\Exception |
||
333 | */ |
||
334 | public function createView($name, array $properties, $type = 'arangosearch') |
||
335 | { |
||
336 | $view = new View($name, $type); |
||
337 | |||
338 | $this->viewHandler->create($view); |
||
339 | $this->viewHandler->setProperties($view, $properties); |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * @param string $name |
||
344 | * @return mixed |
||
345 | * @throws \ArangoDBClient\Exception |
||
346 | */ |
||
347 | public function getView(string $name) |
||
348 | { |
||
349 | return $this->viewHandler->properties($name); |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @param string $name |
||
354 | * @param array $properties |
||
355 | * @throws \ArangoDBClient\ClientException |
||
356 | * @throws \ArangoDBClient\Exception |
||
357 | */ |
||
358 | public function editView($name, array $properties) |
||
359 | { |
||
360 | $this->viewHandler->setProperties($name, $properties); |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * @param string $from |
||
365 | * @param string $to |
||
366 | * @throws \ArangoDBClient\Exception |
||
367 | */ |
||
368 | public function renameView(string $from, string $to) |
||
369 | { |
||
370 | $this->viewHandler->rename($from, $to); |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * @param string $name |
||
375 | * @throws \ArangoDBClient\ClientException |
||
376 | * @throws \ArangoDBClient\Exception |
||
377 | */ |
||
378 | public function dropView(string $name) |
||
379 | { |
||
380 | $this->viewHandler->drop($name); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Get the database connection instance. |
||
385 | * |
||
386 | * @return Connection |
||
387 | */ |
||
388 | public function getConnection() |
||
389 | { |
||
390 | return $this->connection; |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Silently catch the use of unsupported builder methods. |
||
395 | * |
||
396 | * @param $method |
||
397 | * @param $args |
||
398 | * @return null |
||
399 | */ |
||
400 | public function __call($method, $args) |
||
401 | { |
||
402 | error_log("The Aranguent Schema Builder doesn't support method '$method'\n"); |
||
403 | } |
||
404 | } |
||
405 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.