Completed
Push — 6.0 ( cb8a0b...bb9f45 )
by yun
06:00
created

Db::event()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 4
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)
2 ignored issues
show
Coding Style introduced by
Method name "Db::__make" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
Coding Style introduced by
Public method name "Db::__make" must not be prefixed with an underscore
Loading history...
30
    {
31
        $db = new static();
32
        $db->setConfig($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\DbManager::setConfig() 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

32
        $db->setConfig(/** @scrutinizer ignore-type */ $config->get('database'));
Loading history...
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
     * 设置Event对象
51
     * @param Event $event
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
52
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
53
    public function setEvent(Event $event)
54
    {
55
        $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...
56
    }
57
58
    /**
59
     * 设置日志对象
60
     * @param Log $log
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
61
     * @return void
62
     */
63
    public function setLog(Log $log)
64
    {
65
        $this->log = $log;
0 ignored issues
show
Documentation Bug introduced by
It seems like $log of type think\Log is incompatible with the declared type array of property $log.

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...
66
    }
67
68
    /**
69
     * 记录SQL日志
70
     * @access protected
71
     * @param string $log  SQL日志信息
72
     * @param string $type 日志类型
73
     * @return void
74
     */
75
    public function log($log, $type = 'sql')
76
    {
77
        $this->log->record($log, $type);
78
    }
79
80
    /**
81
     * 注册回调方法
82
     * @access public
83
     * @param string   $event    事件名
84
     * @param callable $callback 回调方法
85
     * @return void
86
     */
87
    public function event(string $event, callable $callback): void
88
    {
89
        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...
90
            $this->event->listen('db.' . $event, $callback);
91
        }
92
    }
93
94
    /**
95
     * 触发事件
96
     * @access public
97
     * @param string $event  事件名
98
     * @param mixed  $params 传入参数
99
     * @param bool   $once
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
100
     * @return mixed
101
     */
102
    public function trigger(string $event, $params = null, bool $once = false)
103
    {
104
        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...
105
            return $this->event->trigger('db.' . $event, $params, $once);
106
        }
107
    }
108
}
109