Completed
Push — master ( 198545...b6152d )
by Taosikai
12:30
created

ShipmentEvent::getTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Slince shipment tracker library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\ShipmentTracking\Foundation;
7
8
use Carbon\Carbon;
9
use Slince\ShipmentTracking\Foundation\Location\LocationInterface;
10
11
class ShipmentEvent implements \JsonSerializable
12
{
13
    /**
14
     * @var \DateTime
15
     * @deprecated
16
     */
17
    protected $date;
18
19
    /**
20
     * @var string
21
     */
22
    protected $description;
23
24
    /**
25
     * @var string|LocationInterface
26
     */
27
    protected $location;
28
29
    /**
30
     * @var string
31
     */
32
    protected $status;
33
34
    /**
35
     * @var \DateTime
36
     */
37
    protected $time;
38
39
    public function __construct(\DateTime $time = null, $description = null, $location = null)
40
    {
41
        $this->setTime($time);
0 ignored issues
show
Bug introduced by
It seems like $time defined by parameter $time on line 39 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...
42
        $this->description = $description;
43
        $this->location = $location;
44
    }
45
46
    /**
47
     * @return \DateTime
48
     * @deprecated
49
     */
50
    public function getDate()
51
    {
52
        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...
53
    }
54
55
    /**
56
     * @param \DateTime $date
57
     * @return ShipmentEvent
58
     * @deprecated
59
     */
60
    public function setDate($date)
61
    {
62
        if (!$date instanceof \DateTime) {
63
            $date = Carbon::parse($date);
64
        }
65
        $this->setTime($date);
66
        return $this;
67
    }
68
69
    /**
70
     * @return \DateTime
71
     */
72
    public function getTime()
73
    {
74
        return $this->time;
75
    }
76
77
    /**
78
     * @param \DateTime $time
79
     * @return ShipmentEvent
80
     */
81
    public function setTime($time)
82
    {
83
        $this->time = $time;
84
        if ($time) {
85
            $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...
86
        }
87
        return $this;
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getDescription()
94
    {
95
        return $this->description;
96
    }
97
98
    /**
99
     * @param string $description
100
     * @return ShipmentEvent
101
     */
102
    public function setDescription($description)
103
    {
104
        $this->description = $description;
105
        return $this;
106
    }
107
108
    /**
109
     * @return string|LocationInterface
110
     */
111
    public function getLocation()
112
    {
113
        return $this->location;
114
    }
115
116
    /**
117
     * @param string|LocationInterface $location
118
     * @return ShipmentEvent
119
     */
120
    public function setLocation($location)
121
    {
122
        $this->location = $location;
123
        return $this;
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public function getStatus()
130
    {
131
        return $this->status;
132
    }
133
134
    /**
135
     * @param string $status
136
     * @return ShipmentEvent
137
     */
138
    public function setStatus($status)
139
    {
140
        $this->status = $status;
141
        return $this;
142
    }
143
144
    /**
145
     * Creates an event from an array
146
     * @param array $data
147
     * @return static
148
     */
149
    public static function fromArray($data)
150
    {
151
        $event = new static();
152
        foreach ($data as $property => $value) {
153
            if (method_exists($event, $method = 'set' . ucfirst($property))) {
154
                $event->$method($value);
155
            }
156
        }
157
        return $event;
158
    }
159
160
    /**
161
     * Converts the event to array
162
     * @return array
163
     */
164
    public function toArray()
165
    {
166
        $methods = get_class_methods($this);
167
        $data = [];
168
        foreach ($methods as $method) {
169
            if (substr($method, 0, 3) == 'get') {
170
                $property = lcfirst(substr($method, 3));
171
                $data[$property] = $this->$method();
172
            } elseif (substr($method, 0, 2) == 'is') {
173
                $property = lcfirst(substr($method, 2));
174
                $data[$property] = $this->$method();
175
            }
176
        }
177
        return $data;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function jsonSerialize()
184
    {
185
        return $this->toArray();
186
    }
187
}
188