Test Failed
Pull Request — master (#19)
by Rustam
06:31
created

Message   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 80
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A level() 0 3 1
A context() 0 7 2
A __construct() 0 5 1
A message() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Profiler;
6
7
use Psr\Log\InvalidArgumentException;
8
use Psr\Log\LoggerTrait;
9
use Psr\Log\LogLevel;
10
use Yiisoft\VarDumper\VarDumper;
0 ignored issues
show
Bug introduced by
The type Yiisoft\VarDumper\VarDumper 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...
11
12
use function is_scalar;
13
use function is_object;
14
use function method_exists;
15
use function preg_replace_callback;
16
17
/**
18
 * Message is a data object that stores log message data.
19
 */
20
final class Message
21
{
22
    /**
23
     * @var string Log message level.
24
     *
25
     * @see LogLevel See constants for valid level names.
26
     */
27
    private string $level;
28
29
    /**
30
     * @var string Log message.
31
     */
32
    private string $message;
33
34
    /**
35
     * @var array Log message context.
36
     *
37
     * Message context has a following keys:
38
     *
39
     * - category: string, message category.
40
     * - memory: int, memory usage in bytes, obtained by `memory_get_usage()`.
41
     * - time: float, message timestamp obtained by microtime(true).
42
     * - trace: array, debug backtrace, contains the application code call stacks.
43
     */
44
    private array $context;
45
46
    /**
47
     * @param mixed $level Log message level.
48
     * @param mixed $message Log message.
49
     * @param array $context Log message context.
50
     *
51
     * @throws InvalidArgumentException for invalid log message level.
52
     *
53
     * @see LoggerTrait::log()
54
     * @see LogLevel
55
     */
56
    public function __construct($level, string $message, array $context = [])
57
    {
58
        $this->level = $level;
59
        $this->message = $message;
60
        $this->context = $context;
61
    }
62
63
    /**
64
     * Gets a log message level.
65
     *
66
     * @return string Log message level.
67
     */
68
    public function level(): string
69
    {
70
        return $this->level;
71
    }
72
73
    /**
74
     * Gets a log message.
75
     *
76
     * @return string Log message.
77
     */
78
    public function message(): string
79
    {
80
        return $this->message;
81
    }
82
83
    /**
84
     * Returns a value of the context parameter for the specified name.
85
     *
86
     * If no name is specified, the entire context is returned.
87
     *
88
     * @param string|null $name The context parameter name.
89
     * @param mixed $default If the context parameter does not exist, the `$default` will be returned.
90
     *
91
     * @return mixed The context parameter value.
92
     */
93
    public function context(string $name = null, $default = null)
94
    {
95
        if ($name === null) {
96
            return $this->context;
97
        }
98
99
        return $this->context[$name] ?? $default;
100
    }
101
}
102