Completed
Push — 6.0 ( 3fef99...e5d200 )
by liu
04:53
created

Db::getConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 0
cts 3
cp 0
crap 6
rs 10
1
<?php
2
// +----------------------------------------------------------------------
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
/**
16
 * 数据库管理类
17
 * @package think
0 ignored issues
show
Coding Style introduced by
Package name "think" is not valid; consider "Think" instead
Loading history...
18
 */
19
class Db extends DbManager
20
{
21
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
22
     * @param Event  $event
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
23
     * @param Config $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
24
     * @param Log    $log
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
25
     * @param Cache  $cache
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
26
     * @return Db
27
     * @codeCoverageIgnore
28
     */
29
    public static function __make(Event $event, Config $config, Log $log, Cache $cache)
30
    {
31
        $db = new static();
32
        $db->setConfig($config);
33
        $db->setEvent($event);
34
        $db->setLog($log);
35
        $db->setCache($cache);
36
37
        return $db;
38
    }
39
40
    /**
41
     * 注入模型对象
42
     * @access public
43
     * @return void
44
     */
45 1
    protected function modelMaker()
46
    {
47 1
    }
48
49
    /**
50
     * 设置配置对象
51
     * @access public
52
     * @param Config $config 配置对象
53
     * @return void
54
     */
55
    public function setConfig($config = []): void
56
    {
57
        $this->config = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config can also be of type think\Config. However, the property $config is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
58
    }
59
60
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
61
     * 获取配置参数
62
     * @access public
63
     * @param  string $config 配置参数
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $config does not match actual variable name $name
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
64
     * @param  mixed  $default 默认值
65
     * @return mixed
66
     */
67
    public function getConfig(string $name = '', $default = null)
68
    {
69
        if (!is_null($name)) {
0 ignored issues
show
introduced by
The condition is_null($name) is always false.
Loading history...
70
            return $this->config->get('database.' . $name, $default);
71
        }
72
73
        return $this->config->get('database');
74
    }
75
76
    /**
77
     * 设置Event对象
78
     * @param Event $event
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
79
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
80
    public function setEvent(Event $event): void
81
    {
82
        $this->event = $event;
0 ignored issues
show
Documentation Bug introduced by
It seems like $event of type think\Event is incompatible with the declared type array of property $event.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
83
    }
84
85
    /**
86
     * 注册回调方法
87
     * @access public
88
     * @param string   $event    事件名
89
     * @param callable $callback 回调方法
90
     * @return void
91
     */
92
    public function event(string $event, callable $callback): void
93
    {
94
        if ($this->event) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->event of type array 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...
95
            $this->event->listen('db.' . $event, $callback);
96
        }
97
    }
98
99
    /**
100
     * 触发事件
101
     * @access public
102
     * @param string $event  事件名
103
     * @param mixed  $params 传入参数
104
     * @param bool   $once
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
105
     * @return mixed
106
     */
107
    public function trigger(string $event, $params = null, bool $once = false)
108
    {
109
        if ($this->event) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->event of type array 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...
110
            return $this->event->trigger('db.' . $event, $params, $once);
111
        }
112
    }
113
}
114