LogEntry::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 11
cts 11
cp 1
rs 9.7666
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
/**
4
 * This file is part of the Apix Project.
5
 *
6
 * (c) Franck Cassedanne <franck at ouarz.net>
7
 *
8
 * @license http://opensource.org/licenses/BSD-3-Clause  New BSD License
9
 */
10
11
namespace Apix\Log;
12
13
use Psr\Log\InvalidArgumentException;
14
15
/**
16
 * Describes a log Entry.
17
 *
18
 * @author Franck Cassedanne <franck at ouarz.net>
19
 */
20
class LogEntry
21
{
22
23
    /**
24
     * Holds this log timestamp.
25
     * @var integer
26
     */
27
    public $timestamp;
28
29
    /**
30
     * Holds this log name.
31
     * @var string
32
     */
33
    public $name;
34
35
    /**
36
     * Holds this log level code.
37
     * @var integer
38
     */
39
    public $level_code;
40
41
    /**
42
     * Holds this log message.
43
     * @var string
44
     */
45
    public $message;
46
47
    /**
48
     * Holds this log context.
49
     * @var array
50
     */
51
    public $context;
52
53
    /**
54
     * Holds this log formatter.
55
     * @var LogFormatter
56
     */
57
    public $formatter;
58
59
    /**
60
     * Constructor.
61
     *
62
     * @param string $name    The level name.
63
     * @param string $message The message for this log entry.
64
     * @param array  $context The contexts for this log entry.
65
     */
66 728
    public function __construct($name, $message, array $context = array())
67
    {
68 728
        $this->timestamp = time();
69
70 728
        $this->name = $name;
71 728
        $this->level_code = Logger::getLevelCode($name);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Apix\Log\Logger::getLevelCode($name) can also be of type string. However, the property $level_code is declared as type integer. 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...
72
73
        // Message is not a string let assume it is a context -- and permute. 
74 712
        if (!is_string($message)) {
75 244
            $context = array('ctx' => $message);
76 244
            $message = '{ctx}';
77 61
        }
78 712
        $this->message = $message;
79 712
        $this->context = $context;
80 178
    }
81
    
82 712
    public function setFormatter(LogFormatter $formatter)
83
    {
84 712
        $this->formatter = $formatter;
85 178
    }
86
87
    /**
88
     * Returns the formated string for this log entry.
89
     *
90
     * @return string
91
     */
92 692
    public function __toString()
93
    {
94 692
        return $this->formatter->format($this);
95
    }
96
97
}