Completed
Push — master ( b252c9...97ec88 )
by CodexShaper
05:03
created

IndexTrait::checkDescending()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
ccs 0
cts 9
cp 0
crap 12
rs 10
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
    protected static function getCommonType(IndexInfo $index)
11
    {
12
        $type = "";
13
14
        if ($index->isText()) {
15
            $type = "TEXT";
16
        } else if ($index->is2dSphere()) {
17
            $type = "2DSPARSE";
18
        } else if ($index->isTtl()) {
19
            $type = "TTL";
20
        } else if ($index->isGeoHaystack()) {
21
            $type = "GEOHAYSTACK";
22
        }
23
24
        return $type;
25
    }
26
27
    protected static function getSpecialType(IndexInfo $index)
28
    {
29
        if (static::checkUnique($index)) {
30
            return "UNIQUE";
31
        }
32
33
        if (static::checkUniqueDesc($index)) {
34
            return "UNIQUE_DESC";
35
        }
36
37
        if (static::checkSparse($index)) {
38
            return "SPARSE";
39
        }
40
        if (static::checkSparseUnique($index)) {
41
            return "SPARSE_UNIQUE";
42
        }
43
        if (static::checkSparseUniqueDesc($index)) {
44
            return "SPARSE_UNIQUE_DESC";
45
        }
46
47
        if (static::checkSparseDesc($index)) {
48
            return "SPARSE_DESC";
49
        }
50
51
        return "";
52
    }
53
54
    protected static function getDefaultType(IndexInfo $index)
55
    {
56
        $name     = $index->getName();
57
        $partials = explode("_", $name);
58
        $type     = end($partials);
59
60
        if ($type == 'asc') {
61
            return "ASC";
62
        } else if ($type == 'index') {
63
            return "INDEX";
64
        } else if ($type == 'desc') {
65
            return "DESC";
66
        }
67
68
        return "";
69
    }
70
71
    protected static function checkUnique($index)
72
    {
73
        return $index->isUnique() && !$index->isSparse() && !static::checkDescending($index) ? true : false;
74
    }
75
76
    protected static function checkUniqueDesc($index)
77
    {
78
        return $index->isUnique() && !$index->isSparse() && static::checkDescending($index) ? true : false;
79
    }
80
81
    protected static function checkSparse($index)
82
    {
83
        return $index->isSparse() && !static::checkDescending($index) ? true : false;
84
    }
85
86
    protected static function checkSparseUnique($index)
87
    {
88
        return $index->isSparse() && $index->isUnique() && !static::checkDescending($index) ? true : false;
89
    }
90
91
    protected static function checkSparseUniqueDesc($index)
92
    {
93
        return $index->isSparse() && $index->isUnique() && static::checkDescending($index) ? true : false;
94
    }
95
96
    protected static function checkSparseDesc($index)
97
    {
98
        return $index->isSparse() && static::checkDescending($index) ? true : false;
99
    }
100
101
    protected static function checkDescending($index)
102
    {
103
        $keys = $index->getKey();
104
105
        foreach ($keys as $key => $value) {
106
            if ($value == -1) {
107
                return true;
108
            }
109
        }
110
111
        return false;
112
    }
113
}
114