Completed
Push — master ( bf6574...290f71 )
by Tobias
01:41
created

Report::search()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Billogram\Api;
6
7
use Billogram\Model\Report\ReportCollection;
8
use Billogram\Model\Report\Report as Model;
9
10
class Report extends HttpApi
11
{
12
    /**
13
     * @param string $fileName
14
     *
15
     * @return string|array
16
     *
17
     * @see https://billogram.com/api/documentation#reports
18
     */
19 1
    public function fetch(string $fileName)
20
    {
21 1
        $response = $this->httpGet('/report/'.$fileName);
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\Report::fetch 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 array $param
35
     *
36
     * @return mixed|\Psr\Http\Message\ResponseInterface
37
     *
38
     * @see https://billogram.com/api/documentation#reports
39
     */
40 1
    public function search(array $param = [])
41
    {
42 1
        $param = array_merge(['page' => 1, 'page_size' => 100], $param);
43 1
        $response = $this->httpGet('/report', $param);
44 1
        if (!$this->hydrator) {
45
            return $response;
46
        }
47
        // Use any valid status code here
48 1
        if ($response->getStatusCode() !== 200) {
49
            $this->handleErrors($response);
50
        }
51
52 1
        return $this->hydrator->hydrate($response, ReportCollection::class);
53
    }
54
}
55