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

ArtistService::getSimilarByMBID()   A

Complexity

Conditions 2
Paths 2

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 2
nc 2
nop 3
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\ArtistInfoBuilder;
15
use Core23\LastFm\Builder\ArtistTagsBuilder;
16
use Core23\LastFm\Builder\ArtistTopAlbumsBuilder;
17
use Core23\LastFm\Builder\ArtistTopTagsBuilder;
18
use Core23\LastFm\Builder\ArtistTopTracksBuilder;
19
use Core23\LastFm\Builder\SimilarArtistBuilder;
20
use Core23\LastFm\Model\Album;
21
use Core23\LastFm\Model\Artist;
22
use Core23\LastFm\Model\ArtistInfo;
23
use Core23\LastFm\Model\Song;
24
use Core23\LastFm\Model\Tag;
25
use Core23\LastFm\Session\SessionInterface;
26
use InvalidArgumentException;
27
28
final class ArtistService extends AbstractService implements ArtistServiceInterface
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function addTags(SessionInterface $session, string $artist, array $tags): void
34
    {
35
        $count = \count($tags);
36
37
        if (0 === $count) {
38
            throw new InvalidArgumentException('No tags given');
39
        }
40
        if ($count > 10) {
41
            throw new InvalidArgumentException('A maximum of 10 tags is allowed');
42
        }
43
44
        array_filter($tags, static function ($tag) {
45
            if (null === $tag || !\is_string($tag)) {
46
                throw new InvalidArgumentException(sprintf('Invalid tag given'));
47
            }
48
        });
49
50
        $this->signedCall('artist.addTags', [
51
            'artist' => $artist,
52
            'tags'   => implode(',', $tags),
53
        ], $session, 'POST');
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getCorrection(string $artist): ?Artist
60
    {
61
        $response = $this->unsignedCall('artist.getCorrection', [
62
            'artist' => $artist,
63
        ]);
64
65
        if (!isset($response['corrections']['correction']['artist'])) {
66
            return null;
67
        }
68
69
        return Artist::fromApi($response['corrections']['correction']['artist']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Core23\LastFm\Mo...orrection']['artist']); (self) is incompatible with the return type declared by the interface Core23\LastFm\Service\Ar...nterface::getCorrection of type Core23\LastFm\Model\Artist|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(ArtistInfoBuilder $builder): ?ArtistInfo
76
    {
77
        $response = $this->unsignedCall('artist.getInfo', $builder->getQuery());
78
79
        if (!isset($response['artist'])) {
80
            return null;
81
        }
82
83
        return ArtistInfo::fromApi($response['artist']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Core23\LastFm\Mo...i($response['artist']); (self) is incompatible with the return type declared by the interface Core23\LastFm\Service\Ar...rviceInterface::getInfo of type Core23\LastFm\Model\ArtistInfo|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(SimilarArtistBuilder $builder): array
90
    {
91
        $response = $this->unsignedCall('artist.getSimilar', $builder->getQuery());
92
93
        if (!isset($response['similarartists']['artist'])) {
94
            return [];
95
        }
96
97
        return array_map(static function ($data) {
98
            return Artist::fromApi($data);
99
        }, $response['similarartists']['artist']);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getTags(ArtistTagsBuilder $builder): array
106
    {
107
        $response = $this->unsignedCall('artist.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 getTopAlbums(ArtistTopAlbumsBuilder $builder): array
122
    {
123
        $response =  $this->unsignedCall('artist.getTopAlbums', $builder->getQuery());
124
125
        if (!isset($response['topalbums']['album'])) {
126
            return [];
127
        }
128
129
        return array_map(static function ($data) {
130
            return Album::fromApi($data);
131
        }, $response['topalbums']['album']);
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function getTopTags(ArtistTopTagsBuilder $builder): array
138
    {
139
        $response = $this->unsignedCall('artist.getTopTags', $builder->getQuery());
140
141
        if (!isset($response['toptags']['tag'])) {
142
            return [];
143
        }
144
145
        return array_map(static function ($data) {
146
            return Tag::fromApi($data);
147
        }, $response['toptags']['tag']);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function getTopTracks(ArtistTopTracksBuilder $builder): array
154
    {
155
        $response = $this->unsignedCall('artist.getTopTracks', $builder->getQuery());
156
157
        if (!isset($response['toptracks']['track'])) {
158
            return [];
159
        }
160
161
        return array_map(static function ($data) {
162
            return Song::fromApi($data);
163
        }, $response['toptracks']['track']);
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function removeTag(SessionInterface $session, string $artist, string $tag): void
170
    {
171
        $this->signedCall('artist.removeTag', [
172
            'artist' => $artist,
173
            'tag'    => $tag,
174
        ], $session, 'POST');
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function search(string $artist, int $limit = 50, int $page = 1): array
181
    {
182
        $response = $this->unsignedCall('artist.search', [
183
            'artist' => $artist,
184
            'limit'  => $limit,
185
            'page'   => $page,
186
        ]);
187
188
        if (!isset($response['results']['artistmatches']['artist'])) {
189
            return [];
190
        }
191
192
        return array_map(static function ($data) {
193
            return Artist::fromApi($data);
194
        }, $response['results']['artistmatches']['artist']);
195
    }
196
}
197