ShipmentEvent::setTime()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * Slince shipment tracker library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\ShipmentTracking\Foundation;
7
8
use Slince\ShipmentTracking\Foundation\Location\LocationInterface;
9
10
class ShipmentEvent implements \JsonSerializable
11
{
12
    /**
13
     * @var \DateTime
14
     * @deprecated
15
     */
16
    protected $date;
17
18
    /**
19
     * @var string
20
     */
21
    protected $description;
22
23
    /**
24
     * @var string|LocationInterface
25
     */
26
    protected $location;
27
28
    /**
29
     * @var string
30
     */
31
    protected $status;
32
33
    /**
34
     * @var \DateTime
35
     */
36
    protected $time;
37
38
    public function __construct(\DateTime $time = null, $description = null, $location = null)
39
    {
40
        $this->setTime($time);
0 ignored issues
show
Bug introduced by
It seems like $time defined by parameter $time on line 38 can be null; however, Slince\ShipmentTracking\...hipmentEvent::setTime() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
41
        $this->description = $description;
42
        $this->location = $location;
43
    }
44
45
    /**
46
     * @return \DateTime
47
     * @deprecated
48
     */
49
    public function getDate()
50
    {
51
        return $this->date;
0 ignored issues
show
Deprecated Code introduced by
The property Slince\ShipmentTracking\...on\ShipmentEvent::$date has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
52
    }
53
54
    /**
55
     * @param \DateTime $date
56
     * @return ShipmentEvent
57
     * @deprecated
58
     */
59
    public function setDate($date)
60
    {
61
        if (!$date instanceof \DateTime) {
62
            $date = new \DateTime($date);
63
        }
64
        $this->setTime($date);
65
        return $this;
66
    }
67
68
    /**
69
     * @return \DateTime
70
     */
71
    public function getTime()
72
    {
73
        return $this->time;
74
    }
75
76
    /**
77
     * @param \DateTime $time
78
     * @return ShipmentEvent
79
     */
80
    public function setTime($time)
81
    {
82
        $this->time = $time;
83
        if ($time) {
84
            $this->date = $time->format('Y-m-d H:i:s');
0 ignored issues
show
Documentation Bug introduced by
It seems like $time->format('Y-m-d H:i:s') of type string is incompatible with the declared type object<DateTime> of property $date.

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...
Deprecated Code introduced by
The property Slince\ShipmentTracking\...on\ShipmentEvent::$date has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
85
        }
86
        return $this;
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getDescription()
93
    {
94
        return $this->description;
95
    }
96
97
    /**
98
     * @param string $description
99
     * @return ShipmentEvent
100
     */
101
    public function setDescription($description)
102
    {
103
        $this->description = $description;
104
        return $this;
105
    }
106
107
    /**
108
     * @return string|LocationInterface
109
     */
110
    public function getLocation()
111
    {
112
        return $this->location;
113
    }
114
115
    /**
116
     * @param string|LocationInterface $location
117
     * @return ShipmentEvent
118
     */
119
    public function setLocation($location)
120
    {
121
        $this->location = $location;
122
        return $this;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getStatus()
129
    {
130
        return $this->status;
131
    }
132
133
    /**
134
     * @param string $status
135
     * @return ShipmentEvent
136
     */
137
    public function setStatus($status)
138
    {
139
        $this->status = $status;
140
        return $this;
141
    }
142
143
    /**
144
     * Creates an event from an array
145
     * @param array $data
146
     * @return static
147
     */
148
    public static function fromArray($data)
149
    {
150
        $event = new static();
151
        foreach ($data as $property => $value) {
152
            if (method_exists($event, $method = 'set' . ucfirst($property))) {
153
                $event->$method($value);
154
            }
155
        }
156
        return $event;
157
    }
158
159
    /**
160
     * Converts the event to array
161
     * @return array
162
     */
163
    public function toArray()
164
    {
165
        $methods = get_class_methods($this);
166
        $data = [];
167
        foreach ($methods as $method) {
168
            if (substr($method, 0, 3) == 'get') {
169
                $property = lcfirst(substr($method, 3));
170
                $data[$property] = $this->$method();
171
            } elseif (substr($method, 0, 2) == 'is') {
172
                $property = lcfirst(substr($method, 2));
173
                $data[$property] = $this->$method();
174
            }
175
        }
176
        return $data;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function jsonSerialize()
183
    {
184
        return $this->toArray();
185
    }
186
}
187