Issues (148)

src/Logger/Logger.php (3 issues)

1
<?php
2
3
/**
4
 * Bluz Framework Component
5
 *
6
 * @copyright Bluz PHP Team
7
 * @link      https://github.com/bluzphp/framework
8
 */
9
10
declare(strict_types=1);
11
12
namespace Bluz\Logger;
13
14
use Bluz\Common\Options;
15
use Psr\Log\AbstractLogger;
0 ignored issues
show
The type Psr\Log\AbstractLogger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
/**
18
 * Logger
19
 *
20
 * @package  Bluz\Logger
21
 * @author   Taras Omelianenko <[email protected]>
22
 * @link     https://github.com/bluzphp/framework/wiki/Logger
23
 */
24
class Logger extends AbstractLogger
25
{
26
    use Options;
27
28
    /**
29
     * @var float start time
30
     */
31
    protected $startTime;
32
33
    /**
34
     * @var float part time
35
     */
36
    protected $timer;
37
38
    /**
39
     * @var integer
40
     */
41
    protected $memory = 0;
42
43
    /**
44
     * @var array list of alerts
45
     */
46
    protected $alert = [];
47
48
    /**
49
     * @var array list of critical
50
     */
51
    protected $critical = [];
52
53
    /**
54
     * @var array list of debug messages
55
     */
56
    protected $debug = [];
57
58
    /**
59
     * @var array list of emergency
60
     */
61
    protected $emergency = [];
62
63
    /**
64
     * @var array list of errors
65
     */
66
    protected $error = [];
67
68
    /**
69
     * @var array list of info
70
     */
71
    protected $info = [];
72
73
    /**
74
     * @var array list of notices
75
     */
76
    protected $notice = [];
77
78
    /**
79
     * @var array list of warnings
80
     */
81
    protected $warning = [];
82
83
    /**
84
     * Interpolates context values into the message placeholders
85
     *
86
     * @param  string $message
87
     * @param  array  $context
88
     *
89
     * @return string
90
     */
91 587
    protected function interpolate(string $message, array $context = []): string
92
    {
93
        // build a replacement array with braces around the context keys
94 587
        $replace = [];
95 587
        foreach ($context as $key => $val) {
96
            $replace['{' . $key . '}'] = $val;
97
        }
98
99
        // interpolate replacement values into the message and return
100 587
        return strtr($message, $replace);
101
    }
102
103
    /**
104
     * Log info message
105
     *
106
     * @param  string $message
107
     * @param  array  $context
108
     *
109
     * @return void
110
     */
111 587
    public function info($message, array $context = []): void
112
    {
113 587
        $message = $this->interpolate($message, $context);
114
115 587
        if (!$this->startTime) {
116 587
            $this->startTime = $this->timer = $_SERVER['REQUEST_TIME_FLOAT'] ?? microtime(true);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->timer = $_SERVER[...AT'] ?? microtime(true) can also be of type string. However, the property $startTime is declared as type double. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
Documentation Bug introduced by
It seems like $_SERVER['REQUEST_TIME_FLOAT'] ?? microtime(true) can also be of type string. However, the property $timer is declared as type double. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
117
        }
118
119 587
        $curTimer = microtime(true);
120 587
        $curMemory = memory_get_usage();
121
122 587
        $key = sprintf(
123 587
            '%f :: %f :: %d',
124 587
            $curTimer - $this->startTime,
125 587
            $curTimer - $this->timer,
126 587
            $curMemory - $this->memory
127
        );
128
129 587
        $this->info[$key] = $message;
130
131 587
        $this->timer = $curTimer;
132 587
        $this->memory = $curMemory;
133 587
    }
134
135
    /**
136
     * Logs with an arbitrary level
137
     *
138
     * @param  mixed  $level
139
     * @param  string $message
140
     * @param  array  $context
141
     *
142
     * @return void
143
     */
144 6
    public function log($level, $message, array $context = []): void
145
    {
146 6
        $this->{$level}[] = $this->interpolate($message, $context);
147 6
    }
148
149
    /**
150
     * Get logs records by level
151
     *
152
     * @param  $level
153
     *
154
     * @return array
155
     */
156 1
    public function get($level): array
157
    {
158 1
        return $this->{$level};
159
    }
160
}
161