Video::onPostDeserialize()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 8.8571
cc 5
eloc 5
nc 16
nop 0
1
<?php
2
3
namespace Realshadow\Redtube\Entities;
4
5
use Illuminate\Support\Collection;
6
use JMS\Serializer\Annotation as JMS;
7
8
9
/**
10
 * Video
11
 *
12
 * @package Realshadow\Redtube\Entities
13
 * @author Lukáš Homza <[email protected]>
14
 *
15
 * @JMS\XmlRoot("video")
16
 */
17
class Video
18
{
19
20
    /**
21
     * @var int $id
22
     *
23
     * @JMS\XmlAttribute()
24
     * @JMS\Type("integer")
25
     * @JMS\SerializedName("video_id")
26
     */
27
    private $id;
28
29
    /**
30
     * @var string $duration
31
     *
32
     * @JMS\XmlAttribute()
33
     * @JMS\Type("string")
34
     * @JMS\SerializedName("duration")
35
     */
36
    private $duration;
37
38
    /**
39
     * @var int $views
40
     *
41
     * @JMS\XmlAttribute()
42
     * @JMS\Type("integer")
43
     * @JMS\SerializedName("views")
44
     */
45
    private $views;
46
47
    /**
48
     * @var int $rating
49
     *
50
     * @JMS\XmlAttribute()
51
     * @JMS\Type("double")
52
     * @JMS\SerializedName("rating")
53
     */
54
    private $rating;
55
56
    /**
57
     * @var int $ratings
58
     *
59
     * @JMS\XmlAttribute()
60
     * @JMS\Type("integer")
61
     * @JMS\SerializedName("ratings")
62
     */
63
    private $ratings;
64
65
    /**
66
     * @var string $url
67
     *
68
     * @JMS\XmlAttribute()
69
     * @JMS\Type("string")
70
     * @JMS\SerializedName("url")
71
     */
72
    private $url;
73
74
    /**
75
     * @var string $embedUrl
76
     *
77
     * @JMS\XmlAttribute()
78
     * @JMS\Type("string")
79
     * @JMS\SerializedName("embed_url")
80
     */
81
    private $embedUrl;
82
83
    /**
84
     * @var null|\DateTimeImmutable $publishDate
85
     *
86
     * @JMS\XmlAttribute()
87
     * @JMS\Type("string")
88
     * @JMS\SerializedName("publish_date")
89
     */
90
    private $publishDate;
91
92
    /**
93
     * @var string $title
94
     *
95
     * @JMS\XmlElement(cdata=true)
96
     * @JMS\Type("string")
97
     * @JMS\SerializedName("title")
98
     */
99
    private $title;
100
101
    /**
102
     * @var string $thumb
103
     *
104
     * @JMS\XmlAttribute()
105
     * @JMS\Type("string")
106
     * @JMS\SerializedName("thumb")
107
     */
108
    private $thumb;
109
110
    /**
111
     * @var string $defaultThumb
112
     *
113
     * @JMS\XmlAttribute()
114
     * @JMS\Type("string")
115
     * @JMS\SerializedName("default_thumb")
116
     */
117
    private $defaultThumb;
118
119
    /**
120
     * @var Collection $tags
121
     *
122
     * @JMS\Type("Realshadow\Redtube\Entities\Tags")
123
     * @JMS\SerializedName("tags")
124
     */
125
    private $tags;
126
127
    /**
128
     * @var Collection $stars
129
     *
130
     * @JMS\Type("Realshadow\Redtube\Entities\Stars")
131
     * @JMS\SerializedName("stars")
132
     */
133
    private $stars;
134
135
    /**
136
     * @var Collection $thumbnails
137
     *
138
     * @JMS\Type("Realshadow\Redtube\Entities\Thumbnails")
139
     * @JMS\SerializedName("thumbs")
140
     */
141
    private $thumbnails;
142
143
    /**
144
     * Normalizes nested data
145
     *
146
     * @JMS\PostDeserialize()
147
     */
148
    public function onPostDeserialize()
149
    {
150
        $this->publishDate = $this->publishDate !== '' ? new \DateTime($this->publishDate) : null;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->publishDate !== '...is->publishDate) : null can also be of type object<DateTime>. However, the property $publishDate is declared as type null|object<DateTimeImmutable>. 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...
151
        $this->tags = $this->tags ? $this->tags->flatten() : new Collection;
152
        $this->stars = $this->stars ? $this->stars->flatten() : new Collection;
153
        $this->thumbnails = $this->thumbnails ? $this->thumbnails->flatten() : new Collection;
154
    }
155
156
    /**
157
     * @return int
158
     */
159
    public function getId()
160
    {
161
        return $this->id;
162
    }
163
164
    /**
165
     * @return string
166
     */
167
    public function getDuration()
168
    {
169
        return $this->duration;
170
    }
171
172
    /**
173
     * @return int
174
     */
175
    public function getViews()
176
    {
177
        return $this->views;
178
    }
179
180
    /**
181
     * @return int
182
     */
183
    public function getRating()
184
    {
185
        return $this->rating;
186
    }
187
188
    /**
189
     * @return int
190
     */
191
    public function getRatings()
192
    {
193
        return $this->ratings;
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    public function getUrl()
200
    {
201
        return $this->url;
202
    }
203
204
    /**
205
     * @return string
206
     */
207
    public function getEmbedUrl()
208
    {
209
        return $this->embedUrl;
210
    }
211
212
    /**
213
     * @return \DateTimeImmutable|null
214
     */
215
    public function getPublishDate()
216
    {
217
        return $this->publishDate;
218
    }
219
220
    /**
221
     * @return string
222
     */
223
    public function getTitle()
224
    {
225
        return $this->title;
226
    }
227
228
    /**
229
     * @return string
230
     */
231
    public function getThumb()
232
    {
233
        return $this->thumb;
234
    }
235
236
    /**
237
     * @return string
238
     */
239
    public function getDefaultThumb()
240
    {
241
        return $this->defaultThumb;
242
    }
243
244
    /**
245
     * @return Collection
246
     */
247
    public function getTags()
248
    {
249
        return $this->tags;
250
    }
251
252
    /**
253
     * @return Collection
254
     */
255
    public function getStars()
256
    {
257
        return $this->stars;
258
    }
259
260
    /**
261
     * @return Collection
262
     */
263
    public function getThumbnails()
264
    {
265
        return $this->thumbnails;
266
    }
267
268
}
269