Passed
Push — master ( 48af76...788ee1 )
by Enjoys
08:09
created

Logger::log()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
c 1
b 0
f 1
nc 4
nop 3
dl 0
loc 12
rs 10
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2020 Enjoys.
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
declare(strict_types=1);
28
29
namespace Enjoys\Config;
30
31
/**
32
 * Class Logger
33
 *
34
 * @author Enjoys
35
 */
36
class Logger implements \Psr\Log\LoggerInterface
37
{
38
    use \Psr\Log\LoggerTrait;
39
40
    public function log($level, $message, array $context = [])
41
    {
42
        if ($context) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $context 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...
43
            $message = \strtr($message, \iterator_to_array(self::context2replacements($context), true));
44
        }
45
46
        // В целях отладки приведём XML в читаемый вид, разбив по тегам.
47
        if (\strpos($message, '><') !== false) {
48
            $message = \str_replace('><', ">\n<", $message);
49
        }
50
51
        \fwrite(\STDERR, "\n{$message}\n\n");
52
    }
53
54
    /**
55
     * @param array<string, string> $context
56
     */
57
    private static function context2replacements($context): \Generator
58
    {
59
        foreach ($context as $key => $value) {
60
            yield '{' . $key . '}' => $value;
61
        }
62
    }
63
}
64