Completed
Push — master ( 290f71...5c7d58 )
by Tobias
01:52
created

LogoType::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Billogram\Api;
6
7
use Billogram\Model\LogoType\LogoType as Model;
8
9
/**
10
 * @author Ibrahim Hizeoui <[email protected]>
11
 */
12
class LogoType extends HttpApi
13
{
14
    /**
15
     * @return string|array
16
     *
17
     * @see https://billogram.com/api/documentation#logotype_calls
18
     */
19 1
    public function get()
20
    {
21 1
        $response = $this->httpGet('/logotype');
22 1
        if (!$this->hydrator) {
23
            return $response;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $response; (Psr\Http\Message\ResponseInterface) is incompatible with the return type documented by Billogram\Api\LogoType::get of type string|array.

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...
24
        }
25
        // Use any valid status code here
26 1
        if ($response->getStatusCode() !== 200) {
27
            $this->handleErrors($response);
28
        }
29
30 1
        return $this->hydrator->hydrate($response, Model::class);
31
    }
32
33
    /**
34
     * @param Model $logoType
35
     *
36
     * @return mixed|\Psr\Http\Message\ResponseInterface
37
     *
38
     * @see https://billogram.com/api/documentation#logotype_calls
39
     */
40 1
    public function upload(Model $logoType)
41
    {
42 1
        $response = $this->httpPost('/logotype', $logoType->toArray());
43 1
        if (!$this->hydrator) {
44
            return $response;
45
        }
46
47 1
        return $this->hydrator->hydrate($response, Model::class);
48
    }
49
}
50