Service   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 54
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 16 2
A artist() 0 4 1
A album() 0 4 1
A song() 0 4 1
A track() 0 4 1
1
<?php
2
/*******************************************************************************
3
 * This file is part of the Pbxg33k\MusicInfo package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * (c) 2017 Oguzhan uysal. All rights reserved
9
 ******************************************************************************/
10
11
namespace Pbxg33k\MusicInfo\Service\VocaDB;
12
13
use Pbxg33k\MusicInfo\Service\BaseService;
14
use Pbxg33k\MusicInfo\Service\VocaDB\Endpoint;
15
use Pbxg33k\VocaDB\Album;
16
use Pbxg33k\VocaDB\Artist;
17
use Pbxg33k\VocaDB\Client as VocaDBClient;
18
use Pbxg33k\VocaDB\Song;
19
20
class Service extends BaseService
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 39
    public function init($config = null)
26
    {
27 39
        if (empty($config)) {
28 39
            $config = $this->getConfig();
29 39
        }
30
31 39
        $this->setApiClient(new VocaDBClient(['guzzle' => $config]));
32 39
        $this->setInitialized(true);
33
34
        // Override Client endpoints (optional)
35 39
        $this->apiClient->artist = new Endpoint\Artist($this->getApiClient());
36 39
        $this->apiClient->album = new Endpoint\Album($this->getApiClient());
37 39
        $this->apiClient->song = new Endpoint\Track($this->getApiClient());
38
39 39
        return $this;
40
    }
41
42
    /**
43
     * @return Artist
44
     */
45 6
    public function artist()
46
    {
47 6
        return $this->getApiClient()->artist;
48
    }
49
50
    /**
51
     * @return Album
52
     */
53 1
    public function album()
54
    {
55 1
        return $this->getApiClient()->album;
56
    }
57
58
    /**
59
     * @return Song
60
     */
61 2
    public function song()
62
    {
63 2
        return $this->getApiClient()->song;
64
    }
65
66
    /**
67
     * @return Song
68
     */
69 1
    public function track()
70
    {
71 1
        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...
72
    }
73
}