Completed
Push — 2.1 ( 4d9204...3e6f8b )
by
unknown
11:56
created

SyslogTarget   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 68.75%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 67
ccs 11
cts 16
cp 0.6875
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 2
A export() 0 8 2
A formatMessage() 0 7 1
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\log;
9
10
use Psr\Log\LogLevel;
11
use Yii;
12
13
/**
14
 * SyslogTarget writes log to syslog.
15
 *
16
 * @author miramir <[email protected]>
17
 * @since 2.0
18
 */
19
class SyslogTarget extends Target
20
{
21
    /**
22
     * @var string syslog identity
23
     */
24
    public $identity;
25
    /**
26
     * @var int syslog facility.
27
     */
28
    public $facility = LOG_USER;
29
    /**
30
     * @var array syslog levels
31
     */
32
    private $_syslogLevels = [
33
        LogLevel::EMERGENCY => LOG_EMERG,
34
        LogLevel::ALERT => LOG_ALERT,
35
        LogLevel::CRITICAL => LOG_CRIT,
36
        LogLevel::ERROR => LOG_ERR,
37
        LogLevel::WARNING => LOG_WARNING,
38
        LogLevel::NOTICE => LOG_NOTICE,
39
        LogLevel::INFO => LOG_INFO,
40
        LogLevel::DEBUG => LOG_DEBUG,
41
    ];
42
43
    /**
44
     * @var int openlog options. This is a bitfield passed as the `$option` parameter to [openlog()](http://php.net/openlog).
45
     * Defaults to `null` which means to use the default options `LOG_ODELAY | LOG_PID`.
46
     * @see http://php.net/openlog for available options.
47
     * @since 2.0.11
48
     */
49
    public $options;
50
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function init()
56
    {
57
        parent::init();
58
        if ($this->options === null) {
59
            $this->options = LOG_ODELAY | LOG_PID;
60
        }
61
    }
62
63
    /**
64
     * Writes log messages to syslog
65
     */
66 1
    public function export()
67
    {
68 1
        openlog($this->identity, $this->options, $this->facility);
69 1
        foreach ($this->messages as $message) {
70 1
            syslog($this->_syslogLevels[$message[0]], $this->formatMessage($message));
71
        }
72 1
        closelog();
73 1
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 2
    public function formatMessage($message)
79
    {
80 2
        [$level, $text, $context] = $message;
0 ignored issues
show
Bug introduced by
The variable $level seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
The variable $text does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $context does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
81 2
        $level = Logger::getLevelName($level);
0 ignored issues
show
Bug introduced by
The variable $level seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
82 2
        $prefix = $this->getMessagePrefix($message);
83 2
        return $prefix. '[' . $level . '][' . ($context['category'] ?? '') . '] ' .$text;
84
    }
85
}
86