Passed
Push — master ( af24c2...27a562 )
by kill
04:32
created

Db::connect()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 14
nop 1
dl 0
loc 22
ccs 0
cts 22
cp 0
crap 42
rs 8.6737
c 0
b 0
f 0
1
<?php
2
3
namespace puck;
4
5
/**
6
 * @property  totalCount
7
 */
8
class Db {
9
    protected $db;
10
    protected $field;
11
    protected $table;
12
    protected $limitCount = null;
13
14
    private $connPool;
15
16
    public function connect($conn = null) {
17
        if ($conn === null) {
18
            $conn = 'db.main';
19
        }
20
        $dbConf = [];
0 ignored issues
show
Unused Code introduced by
$dbConf is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
21
        if (is_string($conn)) {
22
            $dbConf = app('config')->get($conn);
23
        } elseif (is_array($conn)) {
24
            $dbConf = $conn;
25
        } else {
26
            throw new \InvalidArgumentException("数据库配置必须为字符串或者数组");
27
        }
28
        if (is_null($dbConf)) {
29
            throw new \InvalidArgumentException("数据库配置异常!");
30
        }
31
        $confMd5 = md5(serialize($dbConf));
32
        if (!isset($this->connPool[$confMd5])) {
33
            $obj = new Mysql($dbConf);
34
            $this->connPool[$confMd5] = $obj;
35
        }
36
        return $this->connPool[$confMd5];
37
    }
38
39
40
    public function __call($method, $arg) {
41
        $ret = $this;
42
        if (method_exists($this->db, $method)) {
43
            $ret = call_user_func_array(array($this->db, $method), $arg);
44
        }
45
        return $ret == $this->db ? $this : $ret;
46
    }
47
48
    public function __get($name) {
49
        if (property_exists($this->db, $name)) {
50
            return $this->db->$name;
51
        }
52
        throw new MemberAccessException('model Property ' . $name . ' not exists');
53
    }
54
}