Completed
Push — master ( efca0d...3519af )
by Maxim
01:56
created

PrefixedLogger::info()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Weew\App\Monolog\Loggers;
4
5
use Psr\Log\LoggerInterface;
6
use ReflectionClass;
7
8
class PrefixedLogger implements LoggerInterface {
9
    /**
10
     * @var Logger
11
     */
12
    private $logger;
13
14
    /**
15
     * @var string
16
     */
17
    private $prefix;
18
19
    /**
20
     * PrefixedLogger constructor.
21
     *
22
     * @param LoggerInterface $logger
23
     * @param prefix $prefix
24
     */
25
    public function __construct(LoggerInterface $logger, $prefix = null) {
26
        $this->logger = $logger;
0 ignored issues
show
Documentation Bug introduced by
$logger is of type object<Psr\Log\LoggerInterface>, but the property $logger was declared to be of type object<Weew\App\Monolog\Loggers\Logger>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
27
        $this->prefix = $this->determinePrefix($prefix);
0 ignored issues
show
Bug introduced by
It seems like $prefix defined by parameter $prefix on line 25 can be null; however, Weew\App\Monolog\Loggers...gger::determinePrefix() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
28
    }
29
30
    /**
31
     * @param string $method
32
     * @param array $args
33
     *
34
     * @return mixed
35
     */
36
    public function __call($method, array $args) {
37
        return call_user_func_array([$this->logger, $method], $args);
38
    }
39
40
    /**
41
     * @return Logger
42
     */
43
    public function getLogger() {
44
        return $this->logger;
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function getPrefix() {
51
        return $this->prefix;
52
    }
53
54
    /**
55
     * System is unusable.
56
     *
57
     * @param string $message
58
     * @param array $context
59
     *
60
     * @return null
61
     */
62
    public function emergency($message, array $context = []) {
63
        $this->logger->emergency($this->prefixMessage($message), $context);
64
    }
65
66
    /**
67
     * Action must be taken immediately.
68
     *
69
     * Example: Entire website down, database unavailable, etc. This should
70
     * trigger the SMS alerts and wake you up.
71
     *
72
     * @param string $message
73
     * @param array $context
74
     *
75
     * @return null
76
     */
77
    public function alert($message, array $context = []) {
78
        $this->logger->alert($this->prefixMessage($message), $context);
79
    }
80
81
    /**
82
     * Critical conditions.
83
     *
84
     * Example: Application component unavailable, unexpected exception.
85
     *
86
     * @param string $message
87
     * @param array $context
88
     *
89
     * @return null
90
     */
91
    public function critical($message, array $context = []) {
92
        $this->logger->critical($this->prefixMessage($message), $context);
93
    }
94
95
    /**
96
     * Runtime errors that do not require immediate action but should typically
97
     * be logged and monitored.
98
     *
99
     * @param string $message
100
     * @param array $context
101
     *
102
     * @return null
103
     */
104
    public function error($message, array $context = []) {
105
        $this->logger->error($this->prefixMessage($message), $context);
106
    }
107
108
    /**
109
     * Exceptional occurrences that are not errors.
110
     *
111
     * Example: Use of deprecated APIs, poor use of an API, undesirable things
112
     * that are not necessarily wrong.
113
     *
114
     * @param string $message
115
     * @param array $context
116
     *
117
     * @return null
118
     */
119
    public function warning($message, array $context = []) {
120
        $this->logger->warning($this->prefixMessage($message), $context);
121
    }
122
123
    /**
124
     * Normal but significant events.
125
     *
126
     * @param string $message
127
     * @param array $context
128
     *
129
     * @return null
130
     */
131
    public function notice($message, array $context = []) {
132
        $this->logger->notice($this->prefixMessage($message), $context);
133
    }
134
135
    /**
136
     * Interesting events.
137
     *
138
     * Example: User logs in, SQL logs.
139
     *
140
     * @param string $message
141
     * @param array $context
142
     *
143
     * @return null
144
     */
145
    public function info($message, array $context = []) {
146
        $this->logger->info($this->prefixMessage($message), $context);
147
    }
148
149
    /**
150
     * Detailed debug information.
151
     *
152
     * @param string $message
153
     * @param array $context
154
     *
155
     * @return null
156
     */
157
    public function debug($message, array $context = []) {
158
        $this->logger->debug($this->prefixMessage($message), $context);
159
    }
160
161
    /**
162
     * Logs with an arbitrary level.
163
     *
164
     * @param mixed $level
165
     * @param string $message
166
     * @param array $context
167
     *
168
     * @return null
169
     */
170
    public function log($level, $message, array $context = []) {
171
        $this->logger->log($level, $this->prefixMessage($message), $context);
172
    }
173
174
    /**
175
     * @param string $message
176
     *
177
     * @return string
178
     */
179
    protected function prefixMessage($message) {
180
        if ($this->prefix !== null) {
181
            return s('%s: %s', $this->prefix, $message);
182
        }
183
184
        return $message;
185
    }
186
187
    /**
188
     * @param string|object $prefix
189
     *
190
     * @return string
191
     */
192
    protected function determinePrefix($prefix) {
193
        if (is_object($prefix)) {
194
            $reflector = new ReflectionClass($prefix);
195
196
            return $reflector->getShortName();
197
        }
198
199
        return $prefix;
200
    }
201
}
202