Passed
Pull Request — master (#63)
by
unknown
03:31
created

AbstractApiClient::getVideoDownloadUrls()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace MovingImage\Client\VMPro\ApiClient;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use MovingImage\Client\VMPro\Collection\ChannelCollection;
7
use MovingImage\Client\VMPro\Collection\VideoCollection;
8
use MovingImage\Client\VMPro\Entity\Attachment;
9
use MovingImage\Client\VMPro\Entity\Channel;
10
use MovingImage\Client\VMPro\Entity\ChannelsRequestParameters;
11
use MovingImage\Client\VMPro\Entity\EmbedCode;
12
use MovingImage\Client\VMPro\Entity\Keyword;
13
use MovingImage\Client\VMPro\Entity\Video;
14
use MovingImage\Client\VMPro\Entity\VideoDownloadUrl;
15
use MovingImage\Client\VMPro\Entity\VideoManager;
16
use MovingImage\Client\VMPro\Entity\VideoRequestParameters;
17
use MovingImage\Client\VMPro\Entity\VideosRequestParameters;
18
use MovingImage\Client\VMPro\Interfaces\ApiClientInterface;
19
use MovingImage\Client\VMPro\Util\ChannelTrait;
20
use MovingImage\Client\VMPro\Util\Logging\Traits\LoggerAwareTrait;
21
use MovingImage\Client\VMPro\Util\SearchEndpointTrait;
22
23
/**
24
 * Class AbstractApiClient.
25
 *
26
 * @author Ruben Knol <[email protected]>
27
 * @author Omid Rad <[email protected]>
28
 */
29
abstract class AbstractApiClient extends AbstractCoreApiClient implements ApiClientInterface
30
{
31
    use LoggerAwareTrait;
32
    use SearchEndpointTrait;
33
    use ChannelTrait;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getChannels($videoManagerId)
39
    {
40
        $response = $this->makeRequest('GET', 'channels', [
41
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
42
        ]);
43
44
        $rootChannel = $this->deserialize($response->getBody(), Channel::class);
45
        $rootChannel->setChildren($this->sortChannels($rootChannel->getChildren()));
46
47
        return $rootChannel;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $rootChannel; (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...tInterface::getChannels of type MovingImage\Client\VMPro\Entity\Channel.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
48
    }
49
50
    /**
51
     * Since the VMPro API doesn't sort any more the returned channels, we have to do it on our side.
52
     *
53
     * @param ArrayCollection $channels
54
     *
55
     * @return ArrayCollection
56
     */
57
    protected function sortChannels(ArrayCollection $channels)
58
    {
59
        $channels->map(function ($channel) {
60
            $channel->setChildren($this->sortChannels($channel->getChildren()));
61
        });
62
63
        $iterator = $channels->getIterator();
64
        $iterator->uasort(function ($a, $b) {
65
            return $a->getName() > $b->getName();
66
        });
67
68
        return new ArrayCollection(iterator_to_array($iterator));
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function createVideo(
75
        $videoManagerId,
76
        $fileName,
77
        $title = '',
78
        $description = '',
79
        $channel = null,
80
        $group = null,
81
        array $keywords = [],
82
        $autoPublish = null
83
    ) {
84
        $response = $this->makeRequest('POST', 'videos', [
85
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
86
            'json' => $this->buildJsonParameters(
87
                compact('fileName'), // Required parameters
88
                compact('title', 'description', 'channel', 'group', 'keywords', 'autoPublish') // Optional parameters
89
            ),
90
        ]);
91
92
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
93
        // wraps headers in arrays.
94
        $videoLocation = is_array($response->getHeader('location'))
95
            ? $response->getHeader('location')[0]
96
            : $response->getHeader('location');
97
98
        $pieces = explode('/', $videoLocation);
99
100
        return end($pieces);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function getVideos($videoManagerId, VideosRequestParameters $parameters = null)
107
    {
108
        $options = [
109
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
110
        ];
111
112 View Code Duplication
        if ($parameters) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
            $query = http_build_query($parameters->getContainer(), null, '&', PHP_QUERY_RFC3986);
114
            $options['query'] = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $query);
115
        }
116
117
        $response = $this->makeRequest('GET', 'videos', $options);
118
        $response = json_encode(json_decode($response->getBody()->getContents(), true)['videos']);
119
120
        return $this->deserialize($response, 'ArrayCollection<'.Video::class.'>');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deserializ...ty\Video::class . '>'); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...entInterface::getVideos of type MovingImage\Meta\Interfaces\VideoInterface[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getCount($videoManagerId, VideosRequestParameters $parameters = null)
127
    {
128
        $options = [
129
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
130
        ];
131
132 View Code Duplication
        if ($parameters) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
            $query = http_build_query($parameters->getContainer(), null, '&', PHP_QUERY_RFC3986);
134
            $options['query'] = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $query);
135
        }
136
137
        $response = $this->makeRequest('GET', 'videos', $options);
138
139
        return json_decode($response->getBody()->getContents(), true)['total'];
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function getVideoUploadUrl($videoManagerId, $videoId)
146
    {
147
        $response = $this->makeRequest('GET', sprintf('videos/%s/url', $videoId), [
148
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
149
        ]);
150
151
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
152
        // wraps headers in arrays.
153
        return is_array($response->getHeader('location'))
154
            ? $response->getHeader('location')[0]
155
            : $response->getHeader('location');
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function updateVideo($videoManagerId, $videoId, $title, $description)
162
    {
163
        $this->makeRequest('PATCH', sprintf('videos/%s', $videoId), [
164
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
165
            'json' => $this->buildJsonParameters([], compact('title', 'description')),
166
        ]);
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function addVideoToChannel($videoManagerId, $videoId, $channelId)
173
    {
174
        $this->makeRequest('POST', sprintf('channels/%s/videos/%s', $channelId, $videoId), [
175
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
176
        ]);
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function removeVideoFromChannel($videoManagerId, $videoId, $channelId)
183
    {
184
        $this->makeRequest('DELETE', sprintf('channels/%s/videos/%s', $channelId, $videoId), [
185
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
186
        ]);
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192
    public function setCustomMetaData($videoManagerId, $videoId, $metadata)
193
    {
194
        $this->makeRequest('PATCH', sprintf('videos/%s/metadata', $videoId), [
195
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
196
            'json' => $metadata,
197
        ]);
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203
    public function getEmbedCode($videoManagerId, $videoId, $playerDefinitionId, $embedType = 'html')
204
    {
205
        $url = sprintf(
206
            'videos/%s/embed-codes?player_definition_id=%s&embed_type=%s',
207
            $videoId,
208
            $playerDefinitionId,
209
            $embedType
210
        );
211
212
        if ($this->cacheTtl) {
213
            $url = sprintf('%s&token_lifetime_in_seconds=%s', $url, $this->cacheTtl);
214
        }
215
216
        $response = $this->makeRequest('GET', $url, [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]);
217
218
        $data = \json_decode($response->getBody(), true);
219
        $embedCode = new EmbedCode();
220
        $embedCode->setCode($data['embedCode']);
221
222
        return $embedCode;
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228
    public function deleteVideo($videoManagerId, $videoId)
229
    {
230
        $this->makeRequest('DELETE', sprintf('videos/%s', $videoId), [
231
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
232
        ]);
233
    }
234
235
    /**
236
     * {@inheritdoc}
237
     */
238
    public function getVideo($videoManagerId, $videoId, VideoRequestParameters $parameters = null)
239
    {
240
        $options = [
241
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
242
        ];
243
244
        if ($parameters) {
245
            $options['query'] = $parameters->getContainer();
246
        }
247
248
        $response = $this->makeRequest(
249
            'GET',
250
            sprintf('videos/%s', $videoId),
251
            $options
252
        );
253
254
        return $this->deserialize($response->getBody()->getContents(), Video::class);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deserializ...o\Entity\Video::class); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...ientInterface::getVideo of type MovingImage\Client\VMPro\Entity\Video.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
255
    }
256
257
    /**
258
     * {@inheritdoc}
259
     */
260 View Code Duplication
    public function getAttachments($videoManagerId, $videoId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
261
    {
262
        $response = $this->makeRequest(
263
            'GET',
264
            sprintf('videos/%s/attachments', $videoId),
265
            [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]
266
        );
267
268
        return $this->deserialize($response->getBody()->getContents(), 'ArrayCollection<'.Attachment::class.'>');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deserializ...tachment::class . '>'); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...terface::getAttachments of type MovingImage\Client\VMPro\Entity\Attachment[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
269
    }
270
271
    /**
272
     * {@inheritdoc}
273
     */
274 View Code Duplication
    public function getChannelAttachments($videoManagerId, $channelId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
275
    {
276
        $response = $this->makeRequest(
277
            'GET',
278
            sprintf('channels/%s/attachments', $channelId),
279
            [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]
280
        );
281
282
        return $this->deserialize($response->getBody()->getContents(), 'ArrayCollection<'.Attachment::class.'>');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deserializ...tachment::class . '>'); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...::getChannelAttachments of type MovingImage\Client\VMPro\Entity\Attachment[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
283
    }
284
285
    /**
286
     * {@inheritdoc}
287
     */
288 View Code Duplication
    public function getKeywords($videoManagerId, $videoId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
289
    {
290
        $response = $this->makeRequest(
291
            'GET',
292
            sprintf('videos/%s/keywords', $videoId),
293
            [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]
294
        );
295
296
        return $this->deserialize($response->getBody()->getContents(), 'ArrayCollection<'.Keyword::class.'>');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deserializ...\Keyword::class . '>'); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...tInterface::getKeywords of type MovingImage\Client\VMPro\Entity\Keyword[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
297
    }
298
299
    /**
300
     * {@inheritdoc}
301
     */
302
    public function updateKeywords($videoManagerId, $videoId, $keywords)
303
    {
304
        //remove all keywords
305
        foreach ($this->getKeywords($videoManagerId, $videoId) as $keyword) {
0 ignored issues
show
Bug introduced by
The expression $this->getKeywords($videoManagerId, $videoId) of type object|array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
306
            $this->deleteKeyword($videoManagerId, $videoId, $keyword->getId());
307
        }
308
309
        //add new
310
        foreach ($keywords as $keyword) {
311
            $this->makeRequest('POST', sprintf('videos/%s/keywords', $videoId), [
312
                self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
313
                'json' => ['text' => $keyword],
314
            ]);
315
        }
316
    }
317
318
    /**
319
     * {@inheritdoc}
320
     */
321
    public function deleteKeyword($videoManagerId, $videoId, $keywordId)
322
    {
323
        $this->makeRequest('DELETE', sprintf('videos/%s/keywords/%s', $videoId, $keywordId), [
324
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
325
        ]);
326
    }
327
328
    /**
329
     * {@inheritdoc}
330
     */
331
    public function searchVideos($videoManagerId, VideosRequestParameters $parameters = null, $searchQuery = null)
332
    {
333
        $options = $this->getRequestOptionsForSearchVideosEndpoint($videoManagerId, $parameters);
334
        if ($searchQuery) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $searchQuery of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
335
            $options['query'] = sprintf('(%s) AND (%s)', $options['query'], $searchQuery);
336
        }
337
        $response = $this->makeRequest('POST', 'search', ['json' => $options]);
338
        $response = $this->normalizeSearchVideosResponse($response->getBody()->getContents());
339
340
        $collection = $this->deserialize($response, VideoCollection::class);
341
342
        return $collection;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $collection; (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...Interface::searchVideos of type MovingImage\Client\VMPro...lection\VideoCollection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
343
    }
344
345
    /**
346
     * {@inheritdoc}
347
     */
348
    public function searchChannels($videoManagerId, ChannelsRequestParameters $parameters = null)
349
    {
350
        $options = $this->getRequestOptionsForSearchChannelsEndpoint($videoManagerId, $parameters);
351
        $response = $this->makeRequest('POST', 'search', ['json' => $options]);
352
        $response = $this->normalizeSearchChannelsResponse($response->getBody()->getContents());
353
        /** @var ChannelCollection $collection */
354
        $collection = $this->deserialize($response, ChannelCollection::class);
355
356
        //builds parent/children relations on all channels
357
        $channels = $this->setChannelRelations($collection->getChannels());
0 ignored issues
show
Documentation introduced by
$collection->getChannels() is of type array<integer,object<Mov...aces\ChannelInterface>>, but the function expects a array<integer,object<Mov...\VMPro\Entity\Channel>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
358
        $collection->setChannels($channels);
359
360
        return $collection;
361
    }
362
363
    /**
364
     * {@inheritdoc}
365
     */
366
    public function getVideoManagers()
367
    {
368
        $response = $this->makeRequest('GET', '', []);
369
370
        return $this->deserialize($response->getBody()->getContents(), 'ArrayCollection<'.VideoManager::class.'>');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deserializ...oManager::class . '>'); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...rface::getVideoManagers of type MovingImage\Client\VMPro\Entity\VideoManager[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
371
    }
372
373
    /**
374
     * {@inheritdoc}
375
     */
376
    public function getVideoDownloadUrls($videoManagerId, $videoId)
377
    {
378
        $options = [
379
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
380
        ];
381
382
        $response = $this->makeRequest(
383
            'GET',
384
            sprintf('videos/%s/download-urls', $videoId),
385
            $options
386
        );
387
388
        $response = $response->getBody()->getContents();
389
390
        return $this->deserialize($response, VideoDownloadUrl::class);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deserializ...deoDownloadUrl::class); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface MovingImage\Client\VMPro...e::getVideoDownloadUrls of type MovingImage\Client\VMPro\Entity\VideoDownloadUrl[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
391
    }
392
}
393