Passed
Push — master ( 9c3b24...f4812f )
by
unknown
49s
created

AbstractApiClient   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 344
Duplicated Lines 11.05 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 9
dl 38
loc 344
rs 10
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A getChannels() 0 11 1
A sortChannels() 0 13 1
A createVideo() 0 28 2
A getVideos() 4 16 2
A getCount() 4 15 2
A getVideoUploadUrl() 0 12 2
A updateVideo() 0 7 1
A addVideoToChannel() 0 6 1
A removeVideoFromChannel() 0 6 1
A setCustomMetaData() 0 7 1
A getEmbedCode() 0 21 2
A deleteVideo() 0 6 1
A getVideo() 0 18 2
A getAttachments() 10 10 1
A getChannelAttachments() 10 10 1
A getKeywords() 10 10 1
A updateKeywords() 0 15 3
A deleteKeyword() 0 6 1
A searchVideos() 0 13 2
A searchChannels() 0 14 1
A getVideoManagers() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Channel;
9
use MovingImage\Client\VMPro\Entity\ChannelsRequestParameters;
10
use MovingImage\Client\VMPro\Entity\EmbedCode;
11
use MovingImage\Client\VMPro\Entity\Keyword;
12
use MovingImage\Client\VMPro\Entity\Video;
13
use MovingImage\Client\VMPro\Entity\Attachment;
14
use MovingImage\Client\VMPro\Entity\VideoManager;
15
use MovingImage\Client\VMPro\Entity\VideoRequestParameters;
16
use MovingImage\Client\VMPro\Entity\VideosRequestParameters;
17
use MovingImage\Client\VMPro\Interfaces\ApiClientInterface;
18
use MovingImage\Client\VMPro\Util\ChannelTrait;
19
use MovingImage\Client\VMPro\Util\Logging\Traits\LoggerAwareTrait;
20
use MovingImage\Client\VMPro\Util\SearchEndpointTrait;
21
22
/**
23
 * Class AbstractApiClient.
24
 *
25
 * @author Ruben Knol <[email protected]>
26
 * @author Omid Rad <[email protected]>
27
 */
28
abstract class AbstractApiClient extends AbstractCoreApiClient implements ApiClientInterface
29
{
30
    use LoggerAwareTrait;
31
    use SearchEndpointTrait;
32
    use ChannelTrait;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getChannels($videoManagerId)
38
    {
39
        $response = $this->makeRequest('GET', 'channels', [
40
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
41
        ]);
42
43
        $rootChannel = $this->deserialize($response->getBody(), Channel::class);
44
        $rootChannel->setChildren($this->sortChannels($rootChannel->getChildren()));
45
46
        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...
47
    }
48
49
    /**
50
     * Since the VMPro API doesn't sort any more the returned channels, we have to do it on our side.
51
     *
52
     * @param ArrayCollection $channels
53
     *
54
     * @return ArrayCollection
55
     */
56
    protected function sortChannels(ArrayCollection $channels)
57
    {
58
        $channels->map(function ($channel) {
59
            $channel->setChildren($this->sortChannels($channel->getChildren()));
60
        });
61
62
        $iterator = $channels->getIterator();
63
        $iterator->uasort(function ($a, $b) {
64
            return $a->getName() > $b->getName();
65
        });
66
67
        return new ArrayCollection(iterator_to_array($iterator));
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function createVideo(
74
        $videoManagerId,
75
        $fileName,
76
        $title = '',
77
        $description = '',
78
        $channel = null,
79
        $group = null,
80
        array $keywords = [],
81
        $autoPublish = null
82
    ) {
83
        $response = $this->makeRequest('POST', 'videos', [
84
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
85
            'json' => $this->buildJsonParameters(
86
                compact('fileName'), // Required parameters
87
                compact('title', 'description', 'channel', 'group', 'keywords', 'autoPublish') // Optional parameters
88
            ),
89
        ]);
90
91
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
92
        // wraps headers in arrays.
93
        $videoLocation = is_array($response->getHeader('location'))
94
            ? $response->getHeader('location')[0]
95
            : $response->getHeader('location');
96
97
        $pieces = explode('/', $videoLocation);
98
99
        return end($pieces);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getVideos($videoManagerId, VideosRequestParameters $parameters = null)
106
    {
107
        $options = [
108
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
109
        ];
110
111 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...
112
            $query = http_build_query($parameters->getContainer(), null, '&', PHP_QUERY_RFC3986);
113
            $options['query'] = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $query);
114
        }
115
116
        $response = $this->makeRequest('GET', 'videos', $options);
117
        $response = json_encode(json_decode($response->getBody()->getContents(), true)['videos']);
118
119
        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...
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function getCount($videoManagerId, VideosRequestParameters $parameters = null)
126
    {
127
        $options = [
128
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
129
        ];
130
131 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...
132
            $query = http_build_query($parameters->getContainer(), null, '&', PHP_QUERY_RFC3986);
133
            $options['query'] = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $query);
134
        }
135
136
        $response = $this->makeRequest('GET', 'videos', $options);
137
138
        return json_decode($response->getBody()->getContents(), true)['total'];
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function getVideoUploadUrl($videoManagerId, $videoId)
145
    {
146
        $response = $this->makeRequest('GET', sprintf('videos/%s/url', $videoId), [
147
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
148
        ]);
149
150
        // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason
151
        // wraps headers in arrays.
152
        return is_array($response->getHeader('location'))
153
            ? $response->getHeader('location')[0]
154
            : $response->getHeader('location');
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function updateVideo($videoManagerId, $videoId, $title, $description)
161
    {
162
        $this->makeRequest('PATCH', sprintf('videos/%s', $videoId), [
163
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
164
            'json' => $this->buildJsonParameters([], compact('title', 'description')),
165
        ]);
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function addVideoToChannel($videoManagerId, $videoId, $channelId)
172
    {
173
        $this->makeRequest('POST', sprintf('channels/%s/videos/%s', $channelId, $videoId), [
174
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
175
        ]);
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function removeVideoFromChannel($videoManagerId, $videoId, $channelId)
182
    {
183
        $this->makeRequest('DELETE', sprintf('channels/%s/videos/%s', $channelId, $videoId), [
184
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
185
        ]);
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public function setCustomMetaData($videoManagerId, $videoId, $metadata)
192
    {
193
        $this->makeRequest('PATCH', sprintf('videos/%s/metadata', $videoId), [
194
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
195
            'json' => $metadata,
196
        ]);
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202
    public function getEmbedCode($videoManagerId, $videoId, $playerDefinitionId, $embedType = 'html')
203
    {
204
        $url = sprintf(
205
            'videos/%s/embed-codes?player_definition_id=%s&embed_type=%s',
206
            $videoId,
207
            $playerDefinitionId,
208
            $embedType
209
        );
210
211
        if ($this->cacheTtl) {
212
            $url = sprintf('%s&token_lifetime_in_seconds=%s', $url, $this->cacheTtl);
213
        }
214
215
        $response = $this->makeRequest('GET', $url, [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]);
216
217
        $data = \json_decode($response->getBody(), true);
218
        $embedCode = new EmbedCode();
219
        $embedCode->setCode($data['embedCode']);
220
221
        return $embedCode;
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227
    public function deleteVideo($videoManagerId, $videoId)
228
    {
229
        $this->makeRequest('DELETE', sprintf('videos/%s', $videoId), [
230
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
231
        ]);
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237
    public function getVideo($videoManagerId, $videoId, VideoRequestParameters $parameters = null)
238
    {
239
        $options = [
240
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
241
        ];
242
243
        if ($parameters) {
244
            $options['query'] = $parameters->getContainer();
245
        }
246
247
        $response = $this->makeRequest(
248
            'GET',
249
            sprintf('videos/%s', $videoId),
250
            $options
251
        );
252
253
        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...
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259 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...
260
    {
261
        $response = $this->makeRequest(
262
            'GET',
263
            sprintf('videos/%s/attachments', $videoId),
264
            [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]
265
        );
266
267
        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...
268
    }
269
270
    /**
271
     * {@inheritdoc}
272
     */
273 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...
274
    {
275
        $response = $this->makeRequest(
276
            'GET',
277
            sprintf('channels/%s/attachments', $channelId),
278
            [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]
279
        );
280
281
        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...
282
    }
283
284
    /**
285
     * {@inheritdoc}
286
     */
287 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...
288
    {
289
        $response = $this->makeRequest(
290
            'GET',
291
            sprintf('videos/%s/keywords', $videoId),
292
            [self::OPT_VIDEO_MANAGER_ID => $videoManagerId]
293
        );
294
295
        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...
296
    }
297
298
    /**
299
     * {@inheritdoc}
300
     */
301
    public function updateKeywords($videoManagerId, $videoId, $keywords)
302
    {
303
        //remove all keywords
304
        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...
305
            $this->deleteKeyword($videoManagerId, $videoId, $keyword->getId());
306
        }
307
308
        //add new
309
        foreach ($keywords as $keyword) {
310
            $this->makeRequest('POST', sprintf('videos/%s/keywords', $videoId), [
311
                self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
312
                'json' => ['text' => $keyword],
313
            ]);
314
        }
315
    }
316
317
    /**
318
     * {@inheritdoc}
319
     */
320
    public function deleteKeyword($videoManagerId, $videoId, $keywordId)
321
    {
322
        $this->makeRequest('DELETE', sprintf('videos/%s/keywords/%s', $videoId, $keywordId), [
323
            self::OPT_VIDEO_MANAGER_ID => $videoManagerId,
324
        ]);
325
    }
326
327
    /**
328
     * {@inheritdoc}
329
     */
330
    public function searchVideos($videoManagerId, VideosRequestParameters $parameters = null, $searchQuery = null)
331
    {
332
        $options = $this->getRequestOptionsForSearchVideosEndpoint($videoManagerId, $parameters);
333
        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...
334
            $options['query'] = sprintf('(%s) AND (%s)', $options['query'], $searchQuery);
335
        }
336
        $response = $this->makeRequest('POST', 'search', ['json' => $options]);
337
        $response = $this->normalizeSearchVideosResponse($response->getBody()->getContents());
338
339
        $collection = $this->deserialize($response, VideoCollection::class);
340
341
        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...
342
    }
343
344
    /**
345
     * {@inheritdoc}
346
     */
347
    public function searchChannels($videoManagerId, ChannelsRequestParameters $parameters = null)
348
    {
349
        $options = $this->getRequestOptionsForSearchChannelsEndpoint($videoManagerId, $parameters);
350
        $response = $this->makeRequest('POST', 'search', ['json' => $options]);
351
        $response = $this->normalizeSearchChannelsResponse($response->getBody()->getContents());
352
        /** @var ChannelCollection $collection */
353
        $collection = $this->deserialize($response, ChannelCollection::class);
354
355
        //builds parent/children relations on all channels
356
        $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...
357
        $collection->setChannels($channels);
358
359
        return $collection;
360
    }
361
362
    /**
363
     * {@inheritdoc}
364
     */
365
    public function getVideoManagers()
366
    {
367
        $response = $this->makeRequest('GET', '', []);
368
369
        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...
370
    }
371
}
372