GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 981090...e30cbf )
by Christian
01:43
created

TrackService::buildTrackList()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8817
c 0
b 0
f 0
cc 6
nc 8
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\LastFm\Service;
13
14
use Core23\LastFm\Builder\ScrobbeBuilder;
15
use Core23\LastFm\Builder\SimilarTrackBuilder;
16
use Core23\LastFm\Builder\TrackInfoBuilder;
17
use Core23\LastFm\Builder\TrackTagsBuilder;
18
use Core23\LastFm\Builder\TrackTopTagsBuilder;
19
use Core23\LastFm\Model\NowPlaying;
20
use Core23\LastFm\Model\Song;
21
use Core23\LastFm\Model\SongInfo;
22
use Core23\LastFm\Model\Tag;
23
use Core23\LastFm\Session\SessionInterface;
24
use InvalidArgumentException;
25
26
final class TrackService extends AbstractService implements TrackServiceInterface
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function addTags(SessionInterface $session, string $artist, string $track, array $tags): void
32
    {
33
        $count = \count($tags);
34
35
        if (0 === $count) {
36
            throw new InvalidArgumentException('No tags given');
37
        }
38
        if ($count > 10) {
39
            throw new InvalidArgumentException('A maximum of 10 tags is allowed');
40
        }
41
42
        array_filter($tags, static function ($tag) {
43
            if (null === $tag || !\is_string($tag)) {
44
                throw new InvalidArgumentException(sprintf('Invalid tag given'));
45
            }
46
        });
47
48
        $this->signedCall('track.addTags', [
49
            'artist' => $artist,
50
            'track'  => $track,
51
            'tags'   => implode(',', $tags),
52
        ], $session, 'POST');
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getCorrection(string $artist, string $track): ?Song
59
    {
60
        $response = $this->unsignedCall('track.getCorrection', [
61
            'artist' => $artist,
62
            'track'  => $track,
63
        ]);
64
65
        if (!isset($response['corrections']['correction']['track'])) {
66
            return null;
67
        }
68
69
        return Song::fromApi($response['corrections']['correction']['track']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Core23\LastFm\Mo...correction']['track']); (self) is incompatible with the return type declared by the interface Core23\LastFm\Service\Tr...nterface::getCorrection of type Core23\LastFm\Model\Song|null.

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...
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getInfo(TrackInfoBuilder $builder): ?SongInfo
76
    {
77
        $response = $this->unsignedCall('track.getInfo', $builder->getQuery());
78
79
        if (!isset($response['track'])) {
80
            return null;
81
        }
82
83
        return SongInfo::fromApi($response['track']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Core23\LastFm\Mo...pi($response['track']); (self) is incompatible with the return type declared by the interface Core23\LastFm\Service\Tr...rviceInterface::getInfo of type Core23\LastFm\Model\SongInfo|null.

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...
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getSimilar(SimilarTrackBuilder $builder): array
90
    {
91
        $response = $this->unsignedCall('track.getSimilar', $builder->getQuery());
92
93
        if (!isset($response['similartracks']['track'])) {
94
            return [];
95
        }
96
97
        return array_map(static function ($data) {
98
            return SongInfo::fromApi($data);
99
        }, $response['similartracks']['track']);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getTags(TrackTagsBuilder $builder): array
106
    {
107
        $response = $this->unsignedCall('track.getTags', $builder->getQuery());
108
109
        if (!isset($response['tags']['tag'])) {
110
            return [];
111
        }
112
113
        return array_map(static function ($data) {
114
            return Tag::fromApi($data);
115
        }, $response['tags']['tag']);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function getTopTags(TrackTopTagsBuilder $builder): array
122
    {
123
        $response = $this->unsignedCall('track.getTopTags', $builder->getQuery());
124
125
        if (!isset($response['toptags']['tag'])) {
126
            return [];
127
        }
128
129
        return array_map(static function ($data) {
130
            return Tag::fromApi($data);
131
        }, $response['toptags']['tag']);
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function love(SessionInterface $session, string $artist, string $track): void
138
    {
139
        $this->signedCall('track.love', [
140
            'artist' => $artist,
141
            'track'  => $track,
142
        ], $session, 'POST');
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function removeTag(SessionInterface $session, string $artist, string $track, string $tag): void
149
    {
150
        $this->signedCall('track.removeTag', [
151
            'artist' => $artist,
152
            'track'  => $track,
153
            'tag'    => $tag,
154
        ], $session, 'POST');
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function scrobble(SessionInterface $session, ScrobbeBuilder $builder): void
161
    {
162
        $count = $builder->count();
163
164
        if (0 === $count) {
165
            return;
166
        }
167
        if ($count > 10) {
168
            throw new InvalidArgumentException('A maximum of 50 tracks is allowed');
169
        }
170
171
        $this->signedCall('album.scrobble', $builder->getQuery(), $session, 'POST');
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function search(string $track, int $limit = 50, int $page = 1): array
178
    {
179
        $response = $this->unsignedCall('track.search', [
180
            'track' => $track,
181
            'limit' => $limit,
182
            'page'  => $page,
183
        ]);
184
185
        if (!isset($response['results']['trackmatches']['track'])) {
186
            return [];
187
        }
188
189
        return array_map(static function ($data) {
190
            return SongInfo::fromApi($data);
191
        }, $response['results']['trackmatches']['track']);
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function unlove(SessionInterface $session, string $artist, string $track): void
198
    {
199
        $this->signedCall('track.love', [
200
            'artist' => $artist,
201
            'track'  => $track,
202
        ], $session, 'POST');
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208
    public function updateNowPlaying(SessionInterface $session, NowPlaying $nowPlaying): void
209
    {
210
        $this->signedCall('track.updateNowPlaying', $nowPlaying->toArray(), $session, 'POST');
211
    }
212
}
213