IndexTrait::getCommonType()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 15
ccs 0
cts 13
cp 0
crap 30
rs 9.6111
1
<?php
2
3
namespace CodexShaper\DBM\Database\Drivers\MongoDB\Traits;
4
5
use MongoDB\Model\IndexInfo;
0 ignored issues
show
Bug introduced by
The type MongoDB\Model\IndexInfo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
trait IndexTrait
8
{
9
    /**
10
     * get common types.
11
     *
12
     * @return  string
13
     */
14
    protected static function getCommonType(IndexInfo $index)
15
    {
16
        $type = '';
17
18
        if ($index->isText()) {
19
            $type = 'TEXT';
20
        } elseif ($index->is2dSphere()) {
21
            $type = '2DSPARSE';
22
        } elseif ($index->isTtl()) {
23
            $type = 'TTL';
24
        } elseif ($index->isGeoHaystack()) {
25
            $type = 'GEOHAYSTACK';
26
        }
27
28
        return $type;
29
    }
30
31
    /**
32
     * get special types.
33
     *
34
     * @return  string
35
     */
36
    protected static function getSpecialType(IndexInfo $index)
37
    {
38
        if (static::checkUnique($index)) {
39
            return 'UNIQUE';
40
        }
41
42
        if (static::checkUniqueDesc($index)) {
43
            return 'UNIQUE_DESC';
44
        }
45
46
        if (static::checkSparse($index)) {
47
            return 'SPARSE';
48
        }
49
        if (static::checkSparseUnique($index)) {
50
            return 'SPARSE_UNIQUE';
51
        }
52
        if (static::checkSparseUniqueDesc($index)) {
53
            return 'SPARSE_UNIQUE_DESC';
54
        }
55
56
        if (static::checkSparseDesc($index)) {
57
            return 'SPARSE_DESC';
58
        }
59
60
        return '';
61
    }
62
63
    /**
64
     * get defaults types.
65
     *
66
     * @return  string
67
     */
68
    protected static function getDefaultType(IndexInfo $index)
69
    {
70
        $name = $index->getName();
71
        $partials = explode('_', $name);
72
        $type = end($partials);
73
74
        if ($type == 'asc') {
75
            return 'ASC';
76
        } elseif ($type == 'index') {
77
            return 'INDEX';
78
        } elseif ($type == 'desc') {
79
            return 'DESC';
80
        }
81
82
        return '';
83
    }
84
85
    /**
86
     * check Unique.
87
     *
88
     * @return  bool
89
     */
90
    protected static function checkUnique(IndexInfo $index)
91
    {
92
        return $index->isUnique() && ! $index->isSparse() && ! static::checkDescending($index) ? true : false;
93
    }
94
95
    /**
96
     * check Unique Descending.
97
     *
98
     * @return  bool
99
     */
100
    protected static function checkUniqueDesc(IndexInfo $index)
101
    {
102
        return $index->isUnique() && ! $index->isSparse() && static::checkDescending($index) ? true : false;
103
    }
104
105
    /**
106
     * check Sparse.
107
     *
108
     * @return  bool
109
     */
110
    protected static function checkSparse(IndexInfo $index)
111
    {
112
        return $index->isSparse() && ! static::checkDescending($index) ? true : false;
113
    }
114
115
    /**
116
     * check Sparse Unique.
117
     *
118
     * @return  bool
119
     */
120
    protected static function checkSparseUnique(IndexInfo $index)
121
    {
122
        return $index->isSparse() && $index->isUnique() && ! static::checkDescending($index) ? true : false;
123
    }
124
125
    /**
126
     * check Sparse Unique Descending.
127
     *
128
     * @return  bool
129
     */
130
    protected static function checkSparseUniqueDesc(IndexInfo $index)
131
    {
132
        return $index->isSparse() && $index->isUnique() && static::checkDescending($index) ? true : false;
133
    }
134
135
    /**
136
     * check Sparse Descending.
137
     *
138
     * @return  bool
139
     */
140
    protected static function checkSparseDesc(IndexInfo $index)
141
    {
142
        return $index->isSparse() && static::checkDescending($index) ? true : false;
143
    }
144
145
    /**
146
     * check Descending.
147
     *
148
     * @return  bool
149
     */
150
    protected static function checkDescending(IndexInfo $index)
151
    {
152
        $keys = $index->getKey();
153
154
        foreach ($keys as $key => $value) {
155
            if ($value == -1) {
156
                return true;
157
            }
158
        }
159
160
        return false;
161
    }
162
}
163