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 Closure; |
||
6 | use Illuminate\Database\Connection; |
||
7 | use Illuminate\Support\Fluent; |
||
8 | use Illuminate\Support\Traits\Macroable; |
||
9 | |||
10 | /** |
||
11 | * Class Blueprint. |
||
12 | * |
||
13 | * The Schema blueprint works differently from the standard Illuminate version: |
||
14 | * 1) ArangoDB is schemaless: we don't need to (and can't) create columns |
||
15 | * 2) ArangoDB doesn't allow DB schema actions within AQL nor within a transaction. |
||
16 | * |
||
17 | * This means that: |
||
18 | * 1) We catch column related methods silently for backwards compatibility and ease of migrating from one DB type to another |
||
19 | * 2) We don't need to compile AQL for transactions within the accompanying schema grammar. (for now) |
||
20 | * 3) We can just execute each command on order. We will gather them first for possible future optimisations. |
||
21 | */ |
||
22 | class Blueprint |
||
23 | { |
||
24 | use Macroable; |
||
25 | |||
26 | /** |
||
27 | * The connection that is used by the blueprint. |
||
28 | * |
||
29 | * @var Connection |
||
30 | */ |
||
31 | protected $connection; |
||
32 | |||
33 | /** |
||
34 | * The grammar that is used by the blueprint. |
||
35 | * |
||
36 | * @var \LaravelFreelancerNL\Aranguent\Schema\Grammars\Grammar |
||
37 | */ |
||
38 | protected $grammar; |
||
39 | |||
40 | /** |
||
41 | * The collection the blueprint describes. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $collection; |
||
46 | |||
47 | /** |
||
48 | * The handler for collection manipulation. |
||
49 | */ |
||
50 | protected $collectionHandler; |
||
51 | |||
52 | /** |
||
53 | * The prefix of the collection. |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $prefix; |
||
58 | |||
59 | /** |
||
60 | * The commands that should be run for the collection. |
||
61 | * |
||
62 | * @var Fluent[] |
||
63 | */ |
||
64 | protected $commands = []; |
||
65 | |||
66 | /** |
||
67 | * Catching attributes to be able to add fluent indexes. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $attributes = []; |
||
72 | |||
73 | /** |
||
74 | * Whether to make the collection temporary. |
||
75 | * |
||
76 | * @var bool |
||
77 | */ |
||
78 | public $temporary = false; |
||
79 | |||
80 | /** |
||
81 | * Detect if _key (and thus proxy _id) should autoincrement. |
||
82 | * |
||
83 | * @var bool |
||
84 | */ |
||
85 | protected $autoIncrement = false; |
||
86 | |||
87 | /** |
||
88 | * Create a new schema blueprint. |
||
89 | * |
||
90 | * Blueprint constructor. |
||
91 | * @param string $collection |
||
92 | * @param \ArangoDBClient\CollectionHandler $collectionHandler |
||
93 | * @param Closure|null $callback |
||
94 | * @param string $prefix |
||
95 | */ |
||
96 | public function __construct($collection, $collectionHandler, Closure $callback = null, $prefix = '') |
||
97 | { |
||
98 | $this->collection = $collection; |
||
99 | |||
100 | $this->collectionHandler = $collectionHandler; |
||
101 | |||
102 | $this->prefix = $prefix; |
||
103 | |||
104 | if (! is_null($callback)) { |
||
105 | $callback($this); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Execute the blueprint against the database. |
||
111 | * |
||
112 | * @param Connection $connection |
||
113 | * @param Grammar $grammar |
||
114 | * @return void |
||
115 | */ |
||
116 | public function build(Connection $connection, Grammar $grammar) |
||
117 | { |
||
118 | $this->connection = $connection; |
||
119 | $this->grammar = $connection->getSchemaGrammar(); |
||
120 | |||
121 | foreach ($this->commands as $command) { |
||
122 | if ($command->handler == 'aql') { |
||
123 | $command = $this->compileAqlCommand($command); |
||
124 | } |
||
125 | |||
126 | $this->executeCommand($command); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Generate the compilation method name and call it if method exists in the Grammar object. |
||
132 | * |
||
133 | * @param $command |
||
134 | * @return mixed |
||
135 | */ |
||
136 | public function compileAqlCommand($command) |
||
137 | { |
||
138 | $compileMethod = 'compile'.ucfirst($command->name); |
||
139 | if (method_exists($this->grammar, $compileMethod)) { |
||
140 | return $this->grammar->$compileMethod($this->collection, $command); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Generate the execution method name and call it if the method exists. |
||
146 | * |
||
147 | * @param $command |
||
148 | */ |
||
149 | public function executeCommand($command) |
||
150 | { |
||
151 | $executeNamedMethod = 'execute'.ucfirst($command->name).'Command'; |
||
152 | $executeHandlerMethod = 'execute'.ucfirst($command->handler).'Command'; |
||
153 | if (method_exists($this, $executeNamedMethod)) { |
||
154 | $this->$executeNamedMethod($command); |
||
155 | } elseif (method_exists($this, $executeHandlerMethod)) { |
||
156 | $this->$executeHandlerMethod($command); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Execute an AQL statement. |
||
162 | * |
||
163 | * @param $command |
||
164 | */ |
||
165 | public function executeAqlCommand($command) |
||
166 | { |
||
167 | $this->connection->statement($command->aqb->query, $command->aqb->binds); |
||
168 | } |
||
169 | |||
170 | public function executeCollectionCommand($command) |
||
171 | { |
||
172 | if ($this->connection->pretending()) { |
||
173 | $this->connection->logQuery('/* '.$command->explanation." */\n", []); |
||
174 | |||
175 | return; |
||
176 | } |
||
177 | |||
178 | if (method_exists($this->collectionHandler, $command->method)) { |
||
179 | $this->collectionHandler->{$command->method}($command->parameters); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Drop the index by first getting all the indexes on the collection; then selecting the matching one |
||
185 | * by type and attributes. |
||
186 | * @param $command |
||
187 | * @throws \ArangoDBClient\Exception |
||
188 | */ |
||
189 | public function executeDropIndexCommand($command) |
||
190 | { |
||
191 | if ($this->connection->pretending()) { |
||
192 | $this->connection->logQuery('/* '.$command->explanation." */\n", []); |
||
193 | |||
194 | return; |
||
195 | } |
||
196 | $attributes = $command->attributes; |
||
197 | if (is_string($attributes)) { |
||
198 | $attributes = [$attributes]; |
||
199 | } |
||
200 | $indexId = null; |
||
201 | $indexData = $this->collectionHandler->getIndexes($this->collection); |
||
202 | foreach ($indexData['indexes'] as $key => $index) { |
||
203 | if ( |
||
204 | $index['type'] === $command->type |
||
205 | && empty(array_diff($index['fields'], $attributes)) |
||
206 | && empty(array_diff($attributes, $index['fields'])) |
||
207 | ) { |
||
208 | $this->collectionHandler->dropIndex($index['id']); |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @param $command |
||
215 | * @throws \ArangoDBClient\Exception |
||
216 | */ |
||
217 | public function executeIndexCommand($command) |
||
218 | { |
||
219 | if ($this->connection->pretending()) { |
||
220 | $this->connection->logQuery('/* '.$command->explanation." */\n", []); |
||
221 | |||
222 | return; |
||
223 | } |
||
224 | |||
225 | $this->collectionHandler->index($this->collection, $command->type, $command->attributes, $command->unique, $command->indexOptions); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Solely provides feedback to the developer in pretend mode. |
||
230 | * |
||
231 | * @param $command |
||
232 | * @return null |
||
233 | */ |
||
234 | public function executeIgnoreCommand($command) |
||
235 | { |
||
236 | if ($this->connection->pretending()) { |
||
237 | $this->connection->logQuery('/* '.$command->explanation." */\n", []); |
||
238 | |||
239 | return; |
||
240 | } |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Determine if the blueprint has a create command. |
||
245 | * |
||
246 | * @return bool |
||
247 | */ |
||
248 | protected function creating() |
||
249 | { |
||
250 | return collect($this->commands)->contains(function ($command) { |
||
251 | return $command->name === 'create'; |
||
252 | }); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Indicate that the collection needs to be created. |
||
257 | * |
||
258 | * @param array $options |
||
259 | * @return Fluent |
||
260 | */ |
||
261 | public function create($options = []) |
||
262 | { |
||
263 | $parameters['options'] = $options; |
||
0 ignored issues
–
show
|
|||
264 | $parameters['explanation'] = "Create '{$this->collection}' collection."; |
||
265 | $parameters['handler'] = 'collection'; |
||
266 | |||
267 | return $this->addCommand('create', $parameters); |
||
268 | } |
||
269 | |||
270 | public function executeCreateCommand($command) |
||
271 | { |
||
272 | if ($this->connection->pretending()) { |
||
273 | $this->connection->logQuery('/* '.$command->explanation." */\n", []); |
||
274 | |||
275 | return; |
||
276 | } |
||
277 | $options = $command->options; |
||
278 | if ($this->temporary === true) { |
||
279 | $options['isVolatile'] = true; |
||
280 | } |
||
281 | if ($this->autoIncrement === true) { |
||
282 | $options['keyOptions']['autoincrement'] = true; |
||
283 | } |
||
284 | |||
285 | $collections = $this->collectionHandler->getAllCollections(['excludeSystem' => true]); |
||
286 | if (! isset($collections[$this->collection])) { |
||
287 | $this->collectionHandler->create($this->collection, $options); |
||
288 | } |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Indicate that the collection should be dropped. |
||
293 | * |
||
294 | * @return Fluent |
||
295 | */ |
||
296 | public function drop() |
||
297 | { |
||
298 | $parameters['explanation'] = "Drop the '{$this->collection}' collection."; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$parameters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $parameters = array(); before regardless.
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: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array 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. ![]() |
|||
299 | $parameters['handler'] = 'collection'; |
||
300 | |||
301 | return $this->addCommand('drop', $parameters); |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * Indicate that the collection should be dropped if it exists. |
||
306 | * |
||
307 | * @return Fluent |
||
308 | */ |
||
309 | public function dropIfExists() |
||
310 | { |
||
311 | $parameters['explanation'] = "Drop the '{$this->collection}' collection."; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$parameters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $parameters = array(); before regardless.
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: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array 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. ![]() |
|||
312 | $parameters['handler'] = 'collection'; |
||
313 | |||
314 | return $this->addCommand('dropIfExists'); |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Indicate that the given attribute(s) should be dropped. |
||
319 | * |
||
320 | * @param array|mixed $attributes |
||
321 | * @return Fluent |
||
322 | */ |
||
323 | public function dropColumn($attributes) |
||
324 | { |
||
325 | $attributes = is_array($attributes) ? $attributes : func_get_args(); |
||
326 | |||
327 | $parameters['handler'] = 'aql'; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$parameters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $parameters = array(); before regardless.
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: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array 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. ![]() |
|||
328 | $parameters['attributes'] = $attributes; |
||
329 | $parameters['explanation'] = 'Drop the following attribute(s): '.implode(',', $attributes).'.'; |
||
330 | |||
331 | return $this->addCommand('dropAttribute', compact('parameters')); |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Check if any document within the collection has the attribute. |
||
336 | * |
||
337 | * @param string|array $attribute |
||
338 | * @return Fluent |
||
339 | */ |
||
340 | public function hasAttribute($attribute) |
||
341 | { |
||
342 | $parameters['handler'] = 'aql'; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$parameters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $parameters = array(); before regardless.
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: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array 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. ![]() |
|||
343 | $parameters['explanation'] = "Checking if any document within the collection has the '".implode(', ', (array) $attribute)."' attribute(s)."; |
||
344 | $parameters['attribute'] = $attribute; |
||
345 | |||
346 | return $this->addCommand('hasAttribute', $parameters); |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Indicate that the given attributes should be renamed. |
||
351 | * |
||
352 | * @param string $from |
||
353 | * @param string $to |
||
354 | * @return Fluent |
||
355 | */ |
||
356 | public function renameColumn($from, $to) |
||
357 | { |
||
358 | $parameters['handler'] = 'aql'; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$parameters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $parameters = array(); before regardless.
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: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array 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. ![]() |
|||
359 | $parameters['explanation'] = "Rename the attribute '$from' to '$to'."; |
||
360 | $parameters['from'] = $from; |
||
361 | $parameters['to'] = $to; |
||
362 | |||
363 | return $this->addCommand('renameAttribute', $parameters); |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * Indicate that the given index should be dropped. |
||
368 | * |
||
369 | * @param string|array $attributes |
||
370 | * @param string $type |
||
371 | * @return Fluent |
||
372 | */ |
||
373 | public function dropIndex($attributes, $type) |
||
374 | { |
||
375 | $parameters['attributes'] = $attributes; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$parameters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $parameters = array(); before regardless.
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: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array 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. ![]() |
|||
376 | $parameters['type'] = $type; |
||
377 | $parameters['explanation'] = "Drop the '".$type."' index on [".implode(', ', (array) $attributes).'].'; |
||
378 | $parameters['handler'] = 'collection'; |
||
379 | |||
380 | return $this->addCommand('dropIndex', $parameters); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Rename the collection to a given name. |
||
385 | * |
||
386 | * @param string $to |
||
387 | * @return Fluent |
||
388 | */ |
||
389 | public function rename($to) |
||
390 | { |
||
391 | return $this->addCommand('rename', compact('to')); |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Specify an index for the table. |
||
396 | * |
||
397 | * @param string|array $columns |
||
398 | * @param string $name |
||
399 | * @param string|null $algorithm |
||
400 | * @return Fluent |
||
401 | */ |
||
402 | public function index($columns = null, $name = null, $algorithm = null) |
||
403 | { |
||
404 | $type = $this->mapIndexType($algorithm); |
||
405 | |||
406 | return $this->indexCommand($type, $columns); |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * Create a hash index for fast exact matching. |
||
411 | * Hash ! has ;). |
||
412 | * |
||
413 | * @param null $attributes |
||
414 | * @param array $indexOptions |
||
415 | * @return Fluent |
||
416 | */ |
||
417 | public function hashIndex($attributes = null, $indexOptions = []) |
||
418 | { |
||
419 | return $this->indexCommand('hash', $attributes, $indexOptions); |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * @param null|string $attribute |
||
424 | * @param array $indexOptions |
||
425 | * @return Fluent |
||
426 | */ |
||
427 | public function fulltextIndex($attribute = null, $indexOptions = []) |
||
428 | { |
||
429 | return $this->indexCommand('fulltext', $attribute, $indexOptions); |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * Specify a spatial index for the collection. |
||
434 | * |
||
435 | * @param $attributes |
||
436 | * @param array $indexOptions |
||
437 | * @return Fluent |
||
438 | */ |
||
439 | public function geoIndex($attributes, $indexOptions = []) |
||
440 | { |
||
441 | return $this->indexCommand('geo', $attributes, $indexOptions); |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * Specify a spatial index for the table. |
||
446 | * Alias for geoIndex(). |
||
447 | * @param string|array $columns |
||
448 | * @param string $name |
||
449 | * @return Fluent |
||
450 | */ |
||
451 | public function spatialIndex($columns, $name = null) |
||
452 | { |
||
453 | return $this->geoIndex($columns); |
||
454 | } |
||
455 | |||
456 | public function skiplistIndex($attributes, $indexOptions = []) |
||
457 | { |
||
458 | return $this->indexCommand('skiplist', $attributes, $indexOptions); |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * Create a TTL index for the table. |
||
463 | * |
||
464 | * @param $attributes |
||
465 | * @param array $indexOptions |
||
466 | * @return \Illuminate\Support\Fluent |
||
467 | */ |
||
468 | public function ttlIndex($attributes, $indexOptions = []) |
||
469 | { |
||
470 | return $this->indexCommand('ttl', $attributes, $indexOptions); |
||
471 | } |
||
472 | |||
473 | /** |
||
474 | * Specify a unique index for the table. |
||
475 | * |
||
476 | * @param string|array $columns |
||
477 | * @param string $name |
||
478 | * @param string|null $algorithm |
||
479 | * @return Fluent |
||
480 | */ |
||
481 | public function unique($columns = null, $name = null, $algorithm = null) |
||
482 | { |
||
483 | $type = $this->mapIndexType($algorithm); |
||
484 | |||
485 | $indexOptions['unique'] = true; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$indexOptions was never initialized. Although not strictly required by PHP, it is generally a good practice to add $indexOptions = array(); before regardless.
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: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array 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. ![]() |
|||
486 | |||
487 | return $this->indexCommand($type, $columns, $indexOptions); |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Add a new index command to the blueprint. |
||
492 | * |
||
493 | * @param string $type |
||
494 | * @param string|array $attributes |
||
495 | * @param array $indexOptions |
||
496 | * @return Fluent |
||
497 | */ |
||
498 | protected function indexCommand($type = '', $attributes = [], $indexOptions = []) |
||
499 | { |
||
500 | if ($type == '') { |
||
501 | $type = 'skiplist'; |
||
502 | } |
||
503 | |||
504 | if ($attributes === null) { |
||
505 | $attributes = end($this->attributes); |
||
506 | } |
||
507 | |||
508 | if (is_string($attributes)) { |
||
509 | $attributes = [$attributes]; |
||
510 | } |
||
511 | |||
512 | $unique = false; |
||
513 | if (isset($indexOptions['unique'])) { |
||
514 | $unique = $indexOptions['unique']; |
||
515 | unset($indexOptions['unique']); |
||
516 | } |
||
517 | |||
518 | return $this->addCommand('index', compact('type', 'attributes', 'unique', 'indexOptions')); |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * Add a new command to the blueprint. |
||
523 | * |
||
524 | * @param string $name |
||
525 | * @param array $parameters |
||
526 | * @return Fluent |
||
527 | */ |
||
528 | protected function addCommand($name, array $parameters = []) |
||
529 | { |
||
530 | $this->commands[] = $command = $this->createCommand($name, $parameters); |
||
531 | |||
532 | return $command; |
||
533 | } |
||
534 | |||
535 | /** |
||
536 | * Create a new Fluent command. |
||
537 | * |
||
538 | * @param string $name |
||
539 | * @param array $parameters |
||
540 | * @return Fluent |
||
541 | */ |
||
542 | protected function createCommand($name, array $parameters = []) |
||
543 | { |
||
544 | return new Fluent(array_merge(compact('name'), $parameters)); |
||
545 | } |
||
546 | |||
547 | /** |
||
548 | * Get the collection the blueprint describes. |
||
549 | * |
||
550 | * @return string |
||
551 | */ |
||
552 | public function getCollection() |
||
553 | { |
||
554 | return $this->collection; |
||
555 | } |
||
556 | |||
557 | /** |
||
558 | * Alias for getCollection. |
||
559 | * |
||
560 | * @return string |
||
561 | */ |
||
562 | public function getTable() |
||
563 | { |
||
564 | return $this->getCollection(); |
||
565 | } |
||
566 | |||
567 | /** |
||
568 | * Get the commands on the blueprint. |
||
569 | * |
||
570 | * @return Fluent[] |
||
571 | */ |
||
572 | public function getCommands() |
||
573 | { |
||
574 | return $this->commands; |
||
575 | } |
||
576 | |||
577 | /** |
||
578 | * Silently catch unsupported schema methods. Store attributes for backwards compatible fluent index creation. |
||
579 | * |
||
580 | * @param $method |
||
581 | * @param $args |
||
582 | * @return Blueprint |
||
583 | */ |
||
584 | public function __call($method, $args) |
||
585 | { |
||
586 | $columnMethods = [ |
||
587 | 'bigIncrements', 'bigInteger', 'binary', 'boolean', 'char', 'date', 'dateTime', 'dateTimeTz', 'decimal', |
||
588 | 'double', 'enum', 'float', 'geometry', 'geometryCollection', 'increments', 'integer', 'ipAddress', 'json', |
||
589 | 'jsonb', 'lineString', 'longText', 'macAddress', 'mediumIncrements', 'mediumInteger', 'mediumText', |
||
590 | 'morphs', 'uuidMorphs', 'multiLineString', 'multiPoint', 'multiPolygon', |
||
591 | 'nullableMorphs', 'nullableUuidMorphs', 'nullableTimestamps', 'point', 'polygon', 'rememberToken', |
||
592 | 'set', 'smallIncrements', 'smallInteger', 'softDeletes', 'softDeletesTz', 'string', |
||
593 | 'text', 'time', 'timeTz', 'timestamp', 'timestampTz', 'timestamps', 'tinyIncrements', 'tinyInteger', |
||
594 | 'unsignedBigInteger', 'unsignedDecimal', 'unsignedInteger', 'unsignedMediumInteger', 'unsignedSmallInteger', |
||
595 | 'unsignedTinyInteger', 'uuid', 'year', |
||
596 | ]; |
||
597 | |||
598 | if (in_array($method, $columnMethods)) { |
||
599 | if (isset($args)) { |
||
600 | $this->attributes[] = $args; |
||
601 | } |
||
602 | } |
||
603 | |||
604 | $autoIncrementMethods = ['increments', 'autoIncrement']; |
||
605 | if (in_array($method, $autoIncrementMethods)) { |
||
606 | $this->autoIncrement = true; |
||
607 | } |
||
608 | |||
609 | $info['method'] = $method; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$info was never initialized. Although not strictly required by PHP, it is generally a good practice to add $info = array(); before regardless.
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: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array 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. ![]() |
|||
610 | $info['explanation'] = "'$method' is ignored; Aranguent Schema Blueprint doesn't support it."; |
||
611 | $this->addCommand('ignore', $info); |
||
612 | |||
613 | return $this; |
||
614 | } |
||
615 | |||
616 | public function mapIndexType($algorithm) |
||
617 | { |
||
618 | $typeConversion = [ |
||
619 | 'HASH' => 'hash', |
||
620 | 'BTREE' => 'skiplist', |
||
621 | 'RTREE' => 'geo', |
||
622 | 'TTL' => 'ttl', |
||
623 | ]; |
||
624 | $algorithm = strtoupper($algorithm); |
||
625 | |||
626 | return (isset($typeConversion[$algorithm])) ? $typeConversion[$algorithm] : 'skiplist'; |
||
627 | } |
||
628 | } |
||
629 |
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.