Test Failed
Branch v5 (12d602)
by Alexey
04:51
created

Db   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 137
Duplicated Lines 17.52 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 24
loc 137
rs 8.8
c 0
b 0
f 0
wmc 36
lcom 1
cbo 4

8 Methods

Rating   Name   Duplication   Size   Complexity  
C init() 0 30 8
A loadMigrationsVersion() 0 10 3
C compareMigrations() 0 22 7
A makeMigration() 0 12 3
B __call() 0 16 5
A newQuery() 0 6 2
A __get() 11 11 4
A __set() 11 11 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Inji;
4
class Db extends Module {
5
    public $name = 'Db';
6
    public $connection = null;
7
    public $connect = false;
8
    public $dbConfig = [];
9
    public $curQuery = null;
10
    public $className = '';
11
    public $QueryClassName = '';
12
    public $ResultClassName = '';
13
    public $migrationsVersions = [];
14
15
    public function init($param = 'default') {
16
        if (!$param || $param === 'default') {
17
            $param = isset($this->config['default']) ? $this->config['default'] : 'local';
18
        }
19
        if ($param === 'injiStorage') {
20
            $db = [
21
                'driver' => 'InjiStorage'
22
            ];
23
        } else if (!is_array($param)) {
24
            if (!($dbOption = Db\Options::sharedStorage()->where($param, 'connect_alias')->get(['array' => true]))) {
0 ignored issues
show
Documentation introduced by
'connect_alias' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
25
                return false;
26
            }
27
            $db = $dbOption;
28
        } else {
29
            $db = $param;
30
        }
31
32
        $className = 'Inji\Db\\' . $db['driver'];
33
        $this->connection = new $className();
34
        if (method_exists($className, 'init')) {
35
            $this->connection->init($db);
36
        }
37
        $this->connection->dbInstance = $this;
38
        $this->connect = $this->connection->connect;
39
        $this->dbConfig = $db;
40
41
        $this->className = 'Inji\Db\\' . $this->dbConfig['driver'];
42
        $this->QueryClassName = 'Inji\Db\\' . $this->dbConfig['driver'] . '\\Query';
43
        $this->ResultClassName = 'Inji\Db\\' . $this->dbConfig['driver'] . '\\Result';
44
    }
45
46
    public function loadMigrationsVersion($code) {
47
        if (!$this->connect) {
48
            return false;
49
        }
50
        $version = Db\Migration::getList(['where' => ['code', $code], 'order' => ['date_create', 'desc'], 'limit' => 1, 'key' => false]);
51
        if ($version) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $version of type Inji\Model[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
52
            $this->migrationsVersions[$code] = $version[0]->version;
53
        }
54
        return true;
55
    }
56
57
    public function compareMigrations($code, $migrations) {
58
        if (!isset($this->migrationsVersions[$code])) {
59
            $this->loadMigrationsVersion($code);
60
        }
61
        if (!isset($this->migrationsVersions[$code]) || !isset($migrations[$this->migrationsVersions[$code]])) {
62
            return $migrations;
63
        }
64
        $startVersion = $this->migrationsVersions[$code];
65
        end($migrations);
66
        if ($startVersion == key($migrations)) {
67
            return [];
68
        }
69
        $pos = 0;
70
        foreach ($migrations as $migrationVersion => $migration) {
71
            $pos++;
72
            if ($startVersion == $migrationVersion) {
73
                return array_slice($migrations, $pos, null, true);
74
            }
75
76
        }
77
        return [];
78
    }
79
80
    public function makeMigration($code, $version, $migration) {
81
        if (!isset($migration['up'])) {
82
            return false;
83
        }
84
        if (is_callable($migration['up'])) {
85
            $migration['up']();
86
        }
87
        $this->migrationsVersions[$code] = $version;
88
        $migrationVersion = new Db\Migration(['code' => $code, 'version' => $version]);
89
        $migrationVersion->save();
90
        return true;
91
    }
92
93
    public function __call($name, $params) {
94
        if (!is_object($this->connection)) {
95
            return false;
96
        }
97
        if (method_exists($this->className, $name)) {
98
            return call_user_func_array(array($this->connection, $name), $params);
99
        }
100
        if (method_exists($this->QueryClassName, $name)) {
101
            if (!is_object($this->curQuery)) {
102
                $this->curQuery = new $this->QueryClassName($this->connection);
103
            }
104
            return call_user_func_array(array($this->curQuery, $name), $params);
105
        }
106
107
        return false;
108
    }
109
110
    public function newQuery() {
111
        if ($this->QueryClassName) {
112
            return new $this->QueryClassName($this->connection);
113
        }
114
        return false;
115
    }
116
117 View Code Duplication
    public function __get($name) {
118
        if (isset($this->connection->$name)) {
119
            return $this->connection->$name;
120
        }
121
        if (!is_object($this->curQuery)) {
122
            $this->curQuery = $this->newQuery();
123
        }
124
        if (isset($this->curQuery->$name)) {
125
            return $this->curQuery->$name;
126
        }
127
    }
128
129 View Code Duplication
    public function __set($name, $value) {
130
        if (isset($this->connection->$name)) {
131
            return $this->connection->$name = $value;
132
        }
133
        if (!is_object($this->curQuery)) {
134
            $this->curQuery = $this->newQuery();
135
        }
136
        if (isset($this->curQuery->$name)) {
137
            return $this->curQuery->$name = $value;
138
        }
139
    }
140
}