Passed
Branch develop (9b7045)
by
unknown
03:42
created

Service   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 64.71%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 53
wmc 6
lcom 1
cbo 4
ccs 11
cts 17
cp 0.6471
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A album() 0 4 1
A song() 0 4 1
A track() 0 4 1
A artist() 0 4 1
A init() 0 15 2
1
<?php
2
namespace Pbxg33k\MusicInfo\Service\VocaDB;
3
4
use Pbxg33k\MusicInfo\Service\BaseService;
5
use Pbxg33k\MusicInfo\Service\VocaDB\Endpoint;
6
use Pbxg33k\VocaDB\Album;
7
use Pbxg33k\VocaDB\Artist;
8
use Pbxg33k\VocaDB\Client as VocaDBClient;
9
use Pbxg33k\VocaDB\Song;
10
11
class Service extends BaseService
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16 7
    public function init($config = null)
17
    {
18 7
        if (empty($config)) {
19 7
            $config = $this->getConfig();
20 7
        }
21
22 7
        $this->setApiClient(new VocaDBClient(['guzzle' => $config]));
23 7
        $this->setInitialized(true);
24
25
        // Override Client endpoints (optional)
26 7
        $this->apiClient->artist = new Endpoint\Artist($this->getApiClient());
27 7
        $this->apiClient->album = new Endpoint\Album($this->getApiClient());
28
29 7
        return $this;
30
    }
31
32
    /**
33
     * @return Artist
34
     */
35 3
    public function artist()
36
    {
37 3
        return $this->getApiClient()->artist;
38
    }
39
40
    /**
41
     * @return Album
42
     */
43
    public function album()
44
    {
45
        return $this->getApiClient()->album;
46
    }
47
48
    /**
49
     * @return Song
50
     */
51
    public function song()
52
    {
53
        return $this->getApiClient()->song;
54
    }
55
56
    /**
57
     * @return Song
58
     */
59
    public function track()
60
    {
61
        return $this->song();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->song(); (Pbxg33k\VocaDB\Song) is incompatible with the return type declared by the interface Pbxg33k\MusicInfo\Model\IMusicService::track of type Pbxg33k\MusicInfo\Model\IMusicServiceEndpoint.

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...
62
    }
63
}