compare_array()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 3
b 0
f 0
nc 4
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
use Doctrine\DBAL\Schema\Index;
4
use Illuminate\Database\Schema\Blueprint;
5
use Illuminate\Support\Facades\Schema;
6
7
/*
8
 * Create an index if it doesn't exist yet
9
 */
10
Blueprint::macro('createIndexIfNotExists', function ($columns, $name = null, $connection = null) {
11
    return $this->hasIndex($columns, $connection)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
12
        ? $this
13
        : $this->index($columns, $name ?: $this->createIndexNameByColumns($columns));
14
});
15
16
/*
17
 * Check if an index exists
18
 */
19
Blueprint::macro('hasIndex', function ($columns) {
20
    return $this->getIndexNameByColumns($columns) !== null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
21
});
22
23
/*
24
 * Get an index name by columns
25
 */
26
Blueprint::macro('getIndexNameByColumns', function ($columns) {
27
    if (is_string($columns)) {
28
        $columns = [$columns];
29
    }
30
31
    $schemaManager = Schema::getConnection()->getDoctrineSchemaManager();
32
    $indices = $schemaManager->listTableIndexes($this->getTable());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
33
    $filteredIndices = collect($indices)->filter(function (Index $index) use ($columns) {
34
        return compare_array($index->getColumns(), $columns);
35
    });
36
37
    if ($filteredIndices->isNotEmpty()) {
38
        return $filteredIndices->keys()->first();
39
    }
40
41
    return null;
42
});
43
44
/*
45
 * Create an index name based on columns
46
 */
47
Blueprint::macro('createIndexNameByColumns', function ($columns) {
48
    if (is_string($columns)) {
49
        $columns = [$columns];
50
    }
51
52
    $index = $this->createIndexName('index', $columns);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
53
54
    if (strlen($index) > 64) {
55
        $index = implode('_', $columns);
56
    }
57
58
    return $index;
59
});
60
61
/*
62
 * Drop an index if it exists
63
 */
64
Blueprint::macro('dropIndexIfExists', function ($columns) {
65
    if ($this->hasIndex($columns)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
66
        return $this->dropIndex($this->getIndexNameByColumns($columns));
67
    }
68
69
    return $this;
70
});
71
72
if (! function_exists('compare_array')) {
73
    function compare_array($arrayA, $arrayB): bool
74
    {
75
        return
76
            is_array($arrayA)
77
            && is_array($arrayB)
78
            && count($arrayA) == count($arrayB)
79
            && array_diff($arrayA, $arrayB) === array_diff($arrayB, $arrayA);
80
    }
81
}
82