Passed
Push — 5.2 ( 48fd10...14f2ed )
by liu
02:32
created

Db::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think;
14
15
use think\db\Connection;
16
use think\db\Raw;
17
18
class Db
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
19
{
20
    /**
21
     * 当前数据库连接对象
22
     * @var Connection
23
     */
24
    protected $connection;
25
26
    /**
27
     * 数据库配置
28
     * @var array
29
     */
30
    protected $config = [];
31
32
    /**
33
     * 查询次数
34
     * @var int
35
     */
36
    protected $queryTimes = 0;
37
38
    /**
39
     * 架构函数
40
     * @param  array         $config 连接配置
41
     * @access public
42
     */
43
    public function __construct(array $config = [])
44
    {
45
        if (empty($config['query'])) {
46
            $config['query'] = '\\think\\db\\Query';
47
        }
48
49
        $this->config = $config;
50
51
        $this->connect($config);
52
    }
53
54
    public static function __make(Config $config)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Method name "Db::__make" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
55
    {
56
        return new static($config->get('database'));
0 ignored issues
show
Bug introduced by
It seems like $config->get('database') can also be of type null; however, parameter $config of think\Db::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        return new static(/** @scrutinizer ignore-type */ $config->get('database'));
Loading history...
57
    }
58
59
    /**
60
     * 初始化
61
     * @access public
62
     * @return void
63
     */
64
    public function init(): void
65
    {
66
        $this->queryTimes = 0;
67
    }
68
69
    /**
70
     * 切换数据库连接
71
     * @access public
72
     * @param  mixed         $config 连接配置
73
     * @param  bool|string   $name 连接标识 true 强制重新连接
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
74
     * @return $this|object
75
     * @throws Exception
76
     */
77
    public function connect($config = [], $name = false)
78
    {
79
        $this->connection = Connection::instance($this->parseConfig($config), $name);
80
81
        return $this;
82
    }
83
84
    /**
85
     * 使用表达式设置数据
86
     * @access public
87
     * @param  string $value 表达式
88
     * @return Raw
89
     */
90
    public function raw(string $value): Raw
91
    {
92
        return new Raw($value);
93
    }
94
95
    /**
96
     * 更新查询次数
97
     * @access public
98
     * @return void
99
     */
100
    public function updateQueryTimes(): void
101
    {
102
        $this->queryTimes++;
103
    }
104
105
    /**
106
     * 获得查询次数
107
     * @access public
108
     * @return integer
109
     */
110
    public function getQueryTimes(): int
111
    {
112
        return $this->queryTimes;
113
    }
114
115
    /**
116
     * 数据库连接参数解析
117
     * @access private
118
     * @param  mixed $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
119
     * @return array
120
     */
121
    private function parseConfig($config): array
0 ignored issues
show
Coding Style introduced by
Private method name "Db::parseConfig" must be prefixed with an underscore
Loading history...
122
    {
123
        if (empty($config)) {
124
            $config = $this->config;
125
        } elseif (is_string($config)) {
126
            // 支持读取配置参数
127
            $config = $this->config[$config] ?? null;
128
        }
129
130
        return $config;
131
    }
132
133
    /**
134
     * 获取数据库的配置参数
135
     * @access public
136
     * @param  string $name 参数名称
137
     * @return mixed
138
     */
139
    public function getConfig(string $name = '')
140
    {
141
        return $name ? ($this->config[$name] ?? null) : $this->config;
142
    }
143
144
    /**
145
     * 创建一个新的Connection对象
146
     * @access public
147
     * @param  mixed  $connection   连接配置信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
148
     * @return mixed
149
     */
150
    public function buildConnection($connection)
151
    {
152
        return Connection::instance($this->parseConfig($connection));
153
    }
154
155
    /**
156
     * 创建一个新的查询对象
157
     * @access public
158
     * @param  string $query        查询对象类名
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 8 found
Loading history...
159
     * @param  mixed  $connection   连接配置信息
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
160
     * @return mixed
161
     */
162
    public function buildQuery(string $query, $connection)
163
    {
164
        $connection = $this->buildConnection($connection);
165
        return new $query($connection);
166
    }
167
168
    public function __call($method, $args)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
169
    {
170
        $class = $this->config['query'];
171
172
        $query = new $class($this->connection);
173
174
        return call_user_func_array([$query, $method], $args);
175
    }
176
}
177