Passed
Push — develop ( 5fc28f...93d639 )
by Michał
16:38
created

DefaultJsonMessage::createMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 16
rs 9.4285
ccs 8
cts 10
cp 0.8
crap 2.032
1
<?php
2
3
namespace SimpleLog\Message;
4
5
class DefaultJsonMessage extends DefaultMessage
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $message = [
11
        'date' => '',
12
        'time' => '',
13
        'data' => '',
14
    ];
15
16
    /**
17
     * @var array
18
     */
19
    protected $context = [];
20
21
    /**
22
     * @param string|array|object $message
23
     * @param array $context
24
     * @return $this
25
     */
26 4
    public function createMessage($message, array $context)
27
    {
28 4
        $this->context = $context;
29
30 4
        list($date, $time) = explode(';', strftime('%d-%m-%Y;%H:%M:%S', time()));
31
32 4
        $this->message['date'] = $date;
33 4
        $this->message['time'] = $time;
34
35 4
        if (method_exists($message, '__toString')) {
36
            $message = (string)$message;
37
        }
38
39 4
        $this->message['data'] = $message;
40
41 4
        return $this;
42
    }
43
44
    /**
45
     * @return string
46
     */
47 4
    public function getMessage()
48
    {
49 4
        $this->message = json_encode($this->message);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_encode($this->message) of type string is incompatible with the declared type array of property $message.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50 4
        $this->buildContext($this->context);
51
52 4
        return $this->message;
53
    }
54
}
55