Completed
Push — master ( 713349...b252c9 )
by CodexShaper
05:16
created

Index   F

Complexity

Total Complexity 62

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 124
c 4
b 0
f 0
dl 0
loc 209
ccs 0
cts 174
cp 0
rs 3.44
wmc 62

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndexes() 0 20 3
A getType() 0 13 3
B getSpecialType() 0 19 7
C setIndexes() 0 72 15
A checkDescending() 0 11 3
A getDefaultType() 0 15 4
A getCommonType() 0 15 5
A checkUniqueDesc() 0 3 4
A checkSparse() 0 3 3
A checkSparseUniqueDesc() 0 3 4
A checkUnique() 0 3 4
A checkSparseDesc() 0 3 3
A checkSparseUnique() 0 3 4

How to fix   Complexity   

Complex Class

Complex classes like Index often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Index, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace CodexShaper\DBM\Database\Drivers\MongoDB;
4
5
use MongoDB\Collection;
0 ignored issues
show
Bug introduced by
The type MongoDB\Collection 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...
Bug introduced by
This use statement conflicts with another class in this namespace, CodexShaper\DBM\Database...vers\MongoDB\Collection. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
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...
7
8
class Index
9
{
10
    public static function getIndexes(Collection $collection)
11
    {
12
        $listIndexes = $collection->listIndexes();
13
        $indexes     = [];
14
        foreach ($listIndexes as $index) {
15
            $indexes[] = [
16
                "name"        => $index->getName(),
17
                "oldName"     => $index->getName(),
18
                "columns"     => array_keys($index->getKey()),
19
                "type"        => static::getType($index),
20
                "isPrimary"   => false,
21
                "isUnique"    => $index->isUnique(),
22
                "isComposite" => (count($index->getKey()) > 1) ? true : false,
23
                "flags"       => [],
24
                "options"     => [],
25
                "namespace"   => $index->getNamespace(),
26
            ];
27
        }
28
29
        return $indexes;
30
    }
31
32
    public static function getType(IndexInfo $index)
33
    {
34
        $type = static::getCommonType($index);
35
36
        if (empty($type)) {
37
            $type = static::getSpecialType($index);
38
        }
39
40
        if (empty($type)) {
41
            $type = static::getDefaultType($index);
42
        }
43
44
        return $type;
45
    }
46
47
    protected static function getCommonType(IndexInfo $index)
48
    {
49
        $type = "";
50
51
        if ($index->isText()) {
52
            $type = "TEXT";
53
        } else if ($index->is2dSphere()) {
54
            $type = "2DSPARSE";
55
        } else if ($index->isTtl()) {
56
            $type = "TTL";
57
        } else if ($index->isGeoHaystack()) {
58
            $type = "GEOHAYSTACK";
59
        }
60
61
        return $type;
62
    }
63
64
    protected static function getSpecialType(IndexInfo $index)
65
    {
66
        $type = "";
67
68
        if (static::checkUnique($index)) {
69
            $type = "UNIQUE";
70
        } else if (static::checkUniqueDesc($index)) {
71
            $type = "UNIQUE_DESC";
72
        } else if (static::checkSparse($index)) {
73
            $type = "SPARSE";
74
        } else if (static::checkSparseUnique($index)) {
75
            $type = "SPARSE_UNIQUE";
76
        } else if (static::checkSparseUniqueDesc($index)) {
77
            $type = "SPARSE_UNIQUE_DESC";
78
        } else if (static::checkSparseDesc($index)) {
79
            $type = "SPARSE_DESC";
80
        }
81
82
        return $type;
83
    }
84
85
    protected static function getDefaultType(IndexInfo $index)
86
    {
87
        $name     = $index->getName();
88
        $partials = explode("_", $name);
89
        $type     = end($partials);
90
91
        if ($type == 'asc') {
92
            return "ASC";
93
        } else if ($type == 'index') {
94
            return "INDEX";
95
        } else if ($type == 'desc') {
96
            return "DESC";
97
        }
98
99
        return "";
100
    }
101
102
    protected static function checkUnique($index)
103
    {
104
        return $index->isUnique() && !$index->isSparse() && !static::checkDescending($index) ? true : false;
105
    }
106
107
    protected static function checkUniqueDesc($index)
108
    {
109
        return $index->isUnique() && !$index->isSparse() && static::checkDescending($index) ? true : false;
110
    }
111
112
    protected static function checkSparse($index)
113
    {
114
        return $index->isSparse() && !static::checkDescending($index) ? true : false;
115
    }
116
117
    protected static function checkSparseUnique($index)
118
    {
119
        return $index->isSparse() && $index->isUnique() && !static::checkDescending($index) ? true : false;
120
    }
121
122
    protected static function checkSparseUniqueDesc($index)
123
    {
124
        return $index->isSparse() && $index->isUnique() && static::checkDescending($index) ? true : false;
125
    }
126
127
    protected static function checkSparseDesc($index)
128
    {
129
        return $index->isSparse() && static::checkDescending($index) ? true : false;
130
    }
131
132
    protected static function checkDescending($index)
133
    {
134
        $keys = $index->getKey();
135
136
        foreach ($keys as $key => $value) {
137
            if ($value == -1) {
138
                return true;
139
            }
140
        }
141
142
        return false;
143
    }
144
145
    public static function setIndexes(Collection $collection, $indexes)
146
    {
147
148
        $collection->dropIndexes();
149
150
        foreach ($indexes as $index) {
151
            $columns = $index['columns'];
152
            $name    = $index['name'];
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
153
            $type    = $index['type'];
154
155
            foreach ($columns as $column) {
156
                if ($column == '_id') {
157
                    continue;
158
                }
159
160
                switch ($type) {
161
162
                    case 'TEXT':
163
                        $indexType = 'text';
164
                        break;
165
                    case 'INDEX':
166
                        $indexType = 1;
167
                        break;
168
                    case 'UNIQUE':
169
                        $indexType         = 1;
170
                        $options['unique'] = true;
171
                        break;
172
                    case 'UNIQUE_DESC':
173
                        $indexType         = -1;
174
                        $options['unique'] = true;
175
                        break;
176
                    case 'TTL':
177
                        $indexType                     = 1;
178
                        $options['expireAfterSeconds'] = 3600;
179
                        break;
180
                    case 'SPARSE':
181
                        $indexType         = 1;
182
                        $options['sparse'] = true;
183
                        break;
184
                    case 'SPARSE_DESC':
185
                        $indexType         = -1;
186
                        $options['sparse'] = true;
187
                        break;
188
                    case 'SPARSE_UNIQUE':
189
                        $indexType         = 1;
190
                        $options['sparse'] = true;
191
                        $options['unique'] = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
192
                    case 'SPARSE_UNIQUE_DESC':
193
                        $indexType         = -1;
194
                        $options['sparse'] = true;
195
                        $options['unique'] = true;
196
                        break;
197
                    case 'ASC':
198
                        $indexType = 1;
199
                        break;
200
                    case 'DESC':
201
                        $indexType = -1;
202
                        break;
203
                    default:
204
                        $indexType = 1;
205
                        break;
206
                }
207
208
                $options['name'] = strtolower($collection->getCollectionName() . "_" . $column . "_" . $type);
209
210
                $options['ns'] = $collection->getNamespace();
211
212
                $collection->createIndex([$column => $indexType], $options);
213
            }
214
        }
215
216
        return true;
217
    }
218
}
219