LastFm   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 93
Duplicated Lines 41.94 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 39
loc 93
rs 10
c 0
b 0
f 0
ccs 0
cts 68
cp 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A connect() 0 6 1
A fetchTopTracks() 15 15 3
A convertToTrackCollection() 0 10 1
A convertToTrack() 0 6 1
A assertValidUsername() 9 9 3
A getTopTracks() 0 8 1
A getLovedTracks() 0 7 1
A fetchLovedTracks() 15 15 3

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 HansOtt\Lastify\Services;
4
5
use Dandelionmood\LastFm\LastFm as LastFmWrapper;
6
use HansOtt\Lastify\TrackInfo;
7
use HansOtt\Lastify\TrackCollection;
8
use HansOtt\Lastify\TrackInfo\Artist;
9
use InvalidArgumentException;
10
11
final class LastFm
12
{
13
    private $api;
14
15
    public function __construct(LastFmWrapper $api)
16
    {
17
        $this->api = $api;
18
    }
19
20
    public static function connect($apiKey)
21
    {
22
        $api = new LastFmWrapper($apiKey);
23
24
        return new static($api);
25
    }
26
27 View Code Duplication
    private function fetchTopTracks($username, $limit)
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...
28
    {
29
        $params = [
30
            'user' => $username,
31
            'limit' => $limit,
32
        ];
33
34
        $response = $this->api->user_getTopTracks($params);
35
36
        $topTracks = isset($response->toptracks) && is_array($response->toptracks->track)
37
            ? $response->toptracks->track
38
            : [];
39
40
        return $topTracks;
41
    }
42
43
    private static function convertToTrackCollection(array $tracks)
44
    {
45
        $convertTrack = function ($track) {
46
            return static::convertToTrack($track);
0 ignored issues
show
Comprehensibility introduced by
Since HansOtt\Lastify\Services\LastFm is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
47
        };
48
49
        $tracks = array_map($convertTrack, $tracks);
50
51
        return new TrackCollection($tracks);
52
    }
53
54
    private static function convertToTrack($track)
55
    {
56
        $artist = new Artist($track->artist->name);
57
58
        return new TrackInfo($track->name, [$artist]);
59
    }
60
61 View Code Duplication
    private function assertValidUsername($username)
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...
62
    {
63
        if (!is_string($username)) {
64
            throw new InvalidArgumentException('The username should be a string, instead got:' . gettype($username));
65
        }
66
        if (empty($username)) {
67
            throw new InvalidArgumentException('The username cannot be empty');
68
        }
69
    }
70
71
    public function getTopTracks($username, $limit = 50)
72
    {
73
        $this->assertValidUsername($username);
74
        $topTracks = $this->fetchTopTracks($username, $limit);
75
        $topTracks = static::convertToTrackCollection($topTracks);
0 ignored issues
show
Comprehensibility introduced by
Since HansOtt\Lastify\Services\LastFm is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
76
77
        return $topTracks;
78
    }
79
80
    public function getLovedTracks($username, $limit = 50)
81
    {
82
        $lovedTracks = $this->fetchLovedTracks($username, $limit);
83
        $lovedTracks = static::convertToTrackCollection($lovedTracks);
0 ignored issues
show
Comprehensibility introduced by
Since HansOtt\Lastify\Services\LastFm is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
84
85
        return $lovedTracks;
86
    }
87
88 View Code Duplication
    private function fetchLovedTracks($username, $limit)
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...
89
    {
90
        $params = [
91
            'user' => $username,
92
            'limit' => $limit,
93
        ];
94
95
        $response = $this->api->user_getLovedTracks($params);
96
97
        $lovedTracks = isset($response->lovedtracks) && is_array($response->lovedtracks->track)
98
            ? $response->lovedtracks->track
99
            : [];
100
101
        return $lovedTracks;
102
    }
103
}
104