Passed
Push — v5 ( 6adbf6...c0775f )
by Alexey
06:03
created

InjiStorage::checkWhere()   C

Complexity

Conditions 15
Paths 17

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 18.5156

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 24
nc 17
nop 5
dl 0
loc 34
rs 5.0504
c 1
b 0
f 0
ccs 18
cts 24
cp 0.75
crap 18.5156

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Inji\Db;
4
5
use Inji\Config;
6
7
class InjiStorage {
8
    public $connect = true;
9
    /**
10
     * @var \Inji\Db
11
     */
12
    public $dbInstance;
13
14 1
    public function addItem($file, $values, $options) {
15 1
        $path = $this->getStoragePath($file, $options);
16 1
        $storage = Config::custom($path);
17 1
        if (empty($storage['schema']['autoincrement'])) {
18 1
            $storage['schema']['autoincrement'] = 0;
19
        }
20 1
        $storage['schema']['autoincrement']++;
21 1
        $values['id'] = $storage['schema']['autoincrement'];
22 1
        $storage['items'][$storage['schema']['autoincrement']] = $values;
23 1
        Config::save($path, $storage);
24 1
        return $storage['schema']['autoincrement'];
25
    }
26
27 1
    public function deleteItems($file, $where, $options) {
28 1
        $items = $this->getItems($file, [], $where, $options);
29 1
        if ($items) {
30 1
            $path = $this->getStoragePath($file, $options);
31 1
            $storage = Config::custom($path);
32 1
            foreach ($items as $key => $item) {
33 1
                unset($storage['items'][$key]);
34
            }
35 1
            Config::save($path, $storage);
36
        }
37 1
        return count($items);
38
    }
39
40 2
    public function updateItems($file, $where, $values, $options) {
41 2
        $items = $this->getItems($file, [], $where, $options);
42 2
        if ($items) {
43 2
            foreach ($items as &$item) {
44 2
                $item = array_replace_recursive($item, $values);
45
            }
46 2
            $path = $this->getStoragePath($file, $options);
47 2
            $storage = Config::custom($path);
48
49 2
            $storage['items'] = array_replace_recursive($storage['items'], $items);
50 2
            Config::save($path, $storage);
51
        }
52 2
        return count($items);
53
    }
54
55 4
    public function getItems($file, $cols, $where, $options) {
56 4
        $path = $this->getStoragePath($file, $options);
57 4
        $storage = Config::custom($path);
58 4
        if (empty($storage['items'])) {
59 2
            return [];
60
        }
61 4
        return $this->filterItems($storage['items'], $cols, $where);
62
    }
63
64 4
    public function filterItems($items, $cols, $where) {
65 4
        $colsMap = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $colsMap is dead and can be removed.
Loading history...
66 4
        $count = false;
67 4
        if ($cols) {
68
            foreach ($cols as $col) {
69
                if (($asIndex = stripos($col, ' as ')) !== false) {
70
                    $origCol = substr($col, 0, $asIndex);
71
                    $newCol = substr($col, $asIndex + 4);
72
                } else {
73
                    $newCol = $origCol = $col;
74
                }
75
                preg_match('!count\((.*)\)!i', $origCol, $match);
76
                if (!empty($match[1])) {
77
                    $count = ['counted' => $match[1], 'as' => $newCol, 'result' => 0];
78
                }
79
            }
80
        }
81 4
        foreach ($items as $key => $item) {
82 4
            if (!$this->checkWhere($item, $where)) {
83
                unset($items[$key]);
84
                continue;
85
            }
86 4
            if ($count) {
87 4
                $count['result']++;
88
            }
89
        }
90 4
        if ($count) {
91
            return [[$count['as'] => $count['result']]];
92
        }
93 4
        return $items;
94
    }
95
96 4
    public function checkWhere($item = [], $where = '', $value = '', $operation = '=', $concatenation = 'AND') {
97 4
        if (!$where) {
98 1
            return true;
99
        }
100 4
        if (is_array($where)) {
101 4
            if (is_array($where[0])) {
102 4
                $result = true;
103 4
                foreach ($where as $key => $whereItem) {
104 4
                    $concatenation = empty($whereItem[3]) ? 'AND' : strtoupper($whereItem[3]);
105 4
                    switch ($concatenation) {
106
                        case 'AND':
107 4
                            $result = $result && call_user_func_array([$this, 'checkWhere'], [$item, $whereItem]);
108 4
                            break;
109
                        case 'OR':
110
                            $result = $result || call_user_func_array([$this, 'checkWhere'], [$item, $whereItem]);
111 4
                            break;
112
                    }
113
                }
114
115 4
                return $result;
116
            } else {
117 4
                return call_user_func_array([$this, 'checkWhere'], array_merge([$item], $where));
118
            }
119
        }
120 4
        if (!isset($item[$where]) && !$value) {
121
            return true;
122
        }
123 4
        if (!isset($item[$where]) && $value) {
124
            return false;
125
        }
126 4
        if ($item[$where] == $value) {
127 4
            return true;
128
        }
129
        return false;
130
    }
131
132 4
    public function getStoragePath($file, $options) {
133 4
        $root = !empty($options['share']) ? INJI_PROGRAM_DIR : $this->dbInstance->app->path;
134 4
        return $root . '/InjiStorage/' . $file . '.php';
135
    }
136
}