Passed
Pull Request — 8.0 (#2929)
by
unknown
02:07
created

Channel::record()   C

Complexity

Conditions 13
Paths 25

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 17.299

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 13
eloc 16
c 4
b 0
f 0
nc 25
nop 4
dl 0
loc 31
ccs 12
cts 17
cp 0.7059
crap 17.299
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: yunwuxin <[email protected]>
10
// +----------------------------------------------------------------------
11
declare(strict_types=1);
12
13
namespace think\log;
14
15
use Psr\Log\LoggerInterface;
16
use Psr\Log\LoggerTrait;
17
use Stringable;
18
use think\contract\LogHandlerInterface;
19
use think\Event;
20
use think\event\LogRecord;
21
use think\event\LogWrite;
22
23
class Channel implements LoggerInterface
24
{
25
    use LoggerTrait;
26
27
    /**
28
     * 事件
29
     * @var Event
30
     */
31
    protected $event;
32
33
    /**
34
     * 日志信息
35
     * @var array
36
     */
37
    protected $log = [];
38
39
    /**
40
     * 关闭日志
41
     * @var array
42
     */
43
    protected $close = false;
44
45 9
    public function __construct(protected string $name, protected LogHandlerInterface $logger, protected array $allow = [], protected bool $lazy = true, Event $event = null)
46
    {
47 9
        if($event) {
48 9
            $this->event = $event;
49
        }
50
    }
51
52
    /**
53
     * 关闭通道
54
     */
55 3
    public function close(): void
56
    {
57 3
        $this->clear();
58 3
        $this->close = true;
0 ignored issues
show
Documentation Bug introduced by
It seems like true of type true is incompatible with the declared type array of property $close.

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...
59
    }
60
61
    /**
62
     * 清空日志
63
     */
64 6
    public function clear(): void
65
    {
66 6
        $this->log = [];
67
    }
68
69
    /**
70
     * 记录日志信息
71
     * @access public
72
     * @param mixed  $msg     日志信息
73
     * @param string $type    日志级别
74
     * @param array  $context 替换内容
75
     * @param bool   $lazy
76
     * @return $this
77
     */
78 6
    public function record($msg, string $type = 'info', array $context = [], bool $lazy = true)
79
    {
80 6
        if ($this->close || (!empty($this->allow) && !in_array($type, $this->allow))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->close 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...
81 3
            return $this;
82
        }
83
84 6
        if ($msg instanceof Stringable) {
85
            $msg = $msg->__toString();
86
        }
87
88 6
        if (is_string($msg) && !empty($context)) {
89
            $replace = [];
90
            foreach ($context as $key => $val) {
91
                $replace['{' . $key . '}'] = $val;
92
            }
93
94
            $msg = strtr($msg, $replace);
95
        }
96
97 6
        if (!empty($msg) || 0 === $msg) {
98 6
            $this->log[$type][] = $msg;
99 6
            if ($this->event) {
100 6
                $this->event->trigger(new LogRecord($type, $msg));
101
            }
102
        }
103
104 6
        if (!$this->lazy || !$lazy) {
105 3
            $this->save();
106
        }
107
108 6
        return $this;
109
    }
110
111
    /**
112
     * 实时写入日志信息
113
     * @access public
114
     * @param mixed  $msg     调试信息
115
     * @param string $type    日志级别
116
     * @param array  $context 替换内容
117
     * @return $this
118
     */
119
    public function write($msg, string $type = 'info', array $context = [])
120
    {
121
        return $this->record($msg, $type, $context, false);
122
    }
123
124
    /**
125
     * 获取日志信息
126
     * @return array
127
     */
128 3
    public function getLog(): array
129
    {
130 3
        return $this->log;
131
    }
132
133
    /**
134
     * 保存日志
135
     * @return bool
136
     */
137 6
    public function save(): bool
138
    {
139 6
        $log = $this->log;
140 6
        if ($this->event) {
141 6
            $event = new LogWrite($this->name, $log);
142 6
            $this->event->trigger($event);
143 6
            $log = $event->log;
144
        }
145
146 6
        if ($this->logger->save($log)) {
147 6
            $this->clear();
148 6
            return true;
149
        }
150
151
        return false;
152
    }
153
154
    /**
155
     * Logs with an arbitrary level.
156
     *
157
     * @param mixed  $level
158
     * @param string|Stringable  $message
159
     * @param array  $context
160
     *
161
     * @return void
162
     */
163
    public function log($level, string|Stringable $message, array $context = []): void
164
    {
165
        $this->record($message, $level, $context);
166
    }
167
168
    public function __call($method, $parameters)
169
    {
170
        $this->log($method, ...$parameters);
0 ignored issues
show
Bug introduced by
$parameters is expanded, but the parameter $message of think\log\Channel::log() does not expect variable arguments. ( Ignorable by Annotation )

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

170
        $this->log($method, /** @scrutinizer ignore-type */ ...$parameters);
Loading history...
171
    }
172
}
173