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 ( 625674...909c28 )
by Christian
01:42
created

TagService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\Client\ApiClientInterface;
15
use Core23\LastFm\Model\Album;
16
use Core23\LastFm\Model\Artist;
17
use Core23\LastFm\Model\Chart;
18
use Core23\LastFm\Model\Song;
19
use Core23\LastFm\Model\Tag;
20
use Core23\LastFm\Model\TagInfo;
21
use Core23\LastFm\Util\ApiHelper;
22
23
final class TagService implements TagServiceInterface
24
{
25
    /**
26
     * @var ApiClientInterface
27
     */
28
    private $client;
29
30
    /**
31
     * @param ApiClientInterface $client
32
     */
33
    public function __construct(ApiClientInterface $client)
34
    {
35
        $this->client = $client;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getInfo(string $tag, string $lang = null): ?TagInfo
42
    {
43
        $response = $this->client->unsignedCall('tag.getInfo', [
44
            'tag'  => $tag,
45
            'lang' => $lang,
46
        ]);
47
48
        if (!isset($response['tag'])) {
49
            return null;
50
        }
51
52
        return TagInfo::fromApi($response['tag']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Core23\LastFm\Mo...mApi($response['tag']); (self) is incompatible with the return type declared by the interface Core23\LastFm\Service\TagServiceInterface::getInfo of type Core23\LastFm\Model\TagInfo|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...
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getSimilar(string $tag): array
59
    {
60
        $response = $this->client->unsignedCall('tag.getSimilar', [
61
            'tag' => $tag,
62
        ]);
63
64
        if (!isset($response['similartags']['tag'])) {
65
            return [];
66
        }
67
68
        return ApiHelper::mapList(
69
            static function ($data) {
70
                return Tag::fromApi($data);
71
            },
72
            $response['similartags']['tag']
73
        );
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getTopAlbums(string $tag, int $limit = 50, int $page = 1): array
80
    {
81
        $response = $this->client->unsignedCall('tag.getTopAlbums', [
82
            'tag'   => $tag,
83
            'limit' => $limit,
84
            'page'  => $page,
85
        ]);
86
87
        if (!isset($response['albums']['album'])) {
88
            return [];
89
        }
90
91
        return ApiHelper::mapList(
92
            static function ($data) {
93
                return Album::fromApi($data);
94
            },
95
            $response['albums']['album']
96
        );
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getTopArtists(string $tag, int $limit = 50, int $page = 1): array
103
    {
104
        $response = $this->client->unsignedCall('tag.getTopArtists', [
105
            'tag'   => $tag,
106
            'limit' => $limit,
107
            'page'  => $page,
108
        ]);
109
110
        if (!isset($response['topartists']['artist'])) {
111
            return [];
112
        }
113
114
        return ApiHelper::mapList(
115
            static function ($data) {
116
                return Artist::fromApi($data);
117
            },
118
            $response['topartists']['artist']
119
        );
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function getTopTags(): array
126
    {
127
        $response = $this->client->unsignedCall('tag.getTopTags');
128
129
        if (!isset($response['toptags']['tag'])) {
130
            return [];
131
        }
132
133
        return ApiHelper::mapList(
134
            static function ($data) {
135
                return Tag::fromApi($data);
136
            },
137
            $response['toptags']['tag']
138
        );
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function getTopTracks(string $tag, int $limit = 50, int $page = 1): array
145
    {
146
        $response = $this->client->unsignedCall('tag.getTopTracks', [
147
            'tag'   => $tag,
148
            'limit' => $limit,
149
            'page'  => $page,
150
        ]);
151
152
        if (!isset($response['tracks']['track'])) {
153
            return [];
154
        }
155
156
        return ApiHelper::mapList(
157
            static function ($data) {
158
                return Song::fromApi($data);
159
            },
160
            $response['tracks']['track']
161
        );
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function getWeeklyChartList(string $tag): array
168
    {
169
        $response = $this->client->unsignedCall('tag.getWeeklyChartList', [
170
            'tag' => $tag,
171
        ]);
172
173
        if (!isset($response['weeklychartlist']['chart'])) {
174
            return [];
175
        }
176
177
        return ApiHelper::mapList(
178
            static function ($data) {
179
                return Chart::fromApi($data);
180
            },
181
            $response['weeklychartlist']['chart']
182
        );
183
    }
184
}
185