Completed
Push — 6.0 ( c13f16...4fc9fc )
by liu
05:12
created

Db::log()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
use think\db\BaseQuery;
16
17
/**
18
 * Class Db
19
 * @package think
0 ignored issues
show
Coding Style introduced by
Package name "think" is not valid; consider "Think" instead
Loading history...
20
 * @mixin BaseQuery
21
 * @mixin Query
22
 */
23
class Db extends DbManager
24
{
25
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
26
     * @param Event  $event
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
27
     * @param Config $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
28
     * @param Log    $log
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
29
     * @param Cache  $cache
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
30
     * @return Db
31
     * @codeCoverageIgnore
32
     */
33
    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...
34
    {
35
        $db = new static();
36
        $db->init($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::init() 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

36
        $db->init(/** @scrutinizer ignore-type */ $config->get('database'));
Loading history...
37
        $db->setEvent($event);
38
        $db->setLog($log);
39
        $db->setCache($cache);
40
41
        return $db;
42
    }
43
44
    /**
45
     * 设置Event对象
46
     * @param Event $event
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
47
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
48
    public function setEvent(Event $event)
49
    {
50
        $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...
51
    }
52
53
    /**
54
     * 设置日志对象
55
     * @param Log $log
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
56
     * @return void
57
     */
58
    public function setLog(Log $log)
59
    {
60
        $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...
61
    }
62
63
    /**
64
     * 记录SQL日志
65
     * @access protected
66
     * @param string $log  SQL日志信息
67
     * @param string $type 日志类型
68
     * @return void
69
     */
70
    public function log($log, $type = 'sql')
71
    {
72
        $this->log->record($log, $type);
73
    }
74
75
    /**
76
     * 注册回调方法
77
     * @access public
78
     * @param string   $event    事件名
79
     * @param callable $callback 回调方法
80
     * @return void
81
     */
82
    public function event(string $event, callable $callback): void
83
    {
84
        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...
85
            $this->event->listen('db.' . $event, $callback);
86
        }
87
    }
88
89
    /**
90
     * 触发事件
91
     * @access public
92
     * @param string $event  事件名
93
     * @param mixed  $params 传入参数
94
     * @param bool   $once
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
95
     * @return mixed
96
     */
97
    public function trigger(string $event, $params = null, bool $once = false)
98
    {
99
        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...
100
            return $this->event->trigger('db.' . $event, $params, $once);
101
        }
102
    }
103
}
104