Record::hasExtra()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Oqq\Minc\Log;
4
5
/**
6
 * @author Eric Braun <[email protected]>
7
 */
8
class Record
9
{
10
    /** @var string */
11
    protected $channel;
12
13
    /** @var string */
14
    protected $message;
15
16
    /** @var Level */
17
    protected $level;
18
19
    /** @var array */
20
    protected $context = [];
21
22
    /** @var \DateTime */
23
    protected $createdAt;
24
25
    /** @var array */
26
    protected $extras = [];
27
28
    /**
29
     * @param string $channel
30
     * @param string $message
31
     * @param Level $level
32
     */
33 14
    public function __construct($channel, $message, Level $level)
34
    {
35 14
        $this->channel = $channel;
36 14
        $this->message = $message;
37 14
        $this->level = $level;
38
39 14
        $this->createdAt = \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)));
0 ignored issues
show
Documentation Bug introduced by
It seems like \DateTime::createFromFor....6F', microtime(true))) can also be of type false. However, the property $createdAt is declared as type object<DateTime>. 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...
40 14
    }
41
42
    /**
43
     * @return string
44
     */
45 1
    public function getChannel()
46
    {
47 1
        return $this->channel;
48
    }
49
50
    /**
51
     * @return Level
52
     */
53 14
    public function getLevel()
54
    {
55 14
        return $this->level;
56
    }
57
58
    /**
59
     * @return string
60
     */
61 14
    public function getMessage()
62
    {
63 14
        return $this->message;
64
    }
65
66
    /**
67
     * @param string $message
68
     */
69 12
    public function setMessage($message)
70
    {
71 12
        $this->message = $message;
72 12
    }
73
74
    /**
75
     * @return array
76
     */
77 13
    public function getContext()
78
    {
79 13
        return $this->context;
80
    }
81
82
    /**
83
     * @param array $context
84
     */
85 14
    public function setContext(array $context)
86
    {
87 14
        $this->context = $context;
88 14
    }
89
90
    /**
91
     * @return \DateTime
92
     */
93 1
    public function getCreatedAt()
94
    {
95 1
        return $this->createdAt;
96
    }
97
98
    /**
99
     * @param string $name
100
     *
101
     * @return bool
102
     */
103
    public function hasExtra($name)
104
    {
105
        return isset($this->extras[$name]) || array_key_exists($name, $this->extras);
106
    }
107
108
    /**
109
     * @param string $name
110
     *
111
     * @return null|mixed
112
     * @throws \InvalidArgumentException
113
     */
114
    public function getExtra($name)
115
    {
116
        if ($this->hasExtra($name)) {
117
            return $this->extras[$name];
118
        }
119
120
        throw new \InvalidArgumentException(sprintf('Extra with name "%s" is not defined', $name));
121
    }
122
123
    /**
124
     * @param string $name
125
     * @param mixed $value
126
     */
127
    public function setExtra($name, $value)
128
    {
129
        $this->extras[$name] = $value;
130
    }
131
132
    /**
133
     * @return array
134
     */
135 1
    public function getExtras()
136
    {
137 1
        return $this->extras;
138
    }
139
140
    /**
141
     * @param array $extras
142
     */
143 2
    public function setExtras(array $extras)
144
    {
145 2
        $this->extras = $extras;
146 2
    }
147
}
148