Failed Conditions
Pull Request — master (#25)
by Chad
02:50
created

CollectionAdapter::send()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 26
nc 1
nop 1
1
<?php
2
namespace Chadicus\Marvel\Api\Assets;
3
4
use Chadicus\Marvel\Api\Adapter\AdapterInterface;
5
use Chadicus\Marvel\Api\Client;
6
use Chadicus\Marvel\Api\RequestInterface;
7
use Chadicus\Marvel\Api\Response;
8
9
/**
10
 * Adapter that returns multiple items.
11
 */
12
final class CollectionAdapter implements AdapterInterface
13
{
14
    /**
15
     * Simulate sending a request to the API.
16
     *
17
     * @param RequestInterface $request The request.
18
     *
19
     * @return ResponseInterface
20
     */
21
    public function send(RequestInterface $request)
22
    {
23
        $allResults = [
24
            ['id' => 0, 'title' => 'a title for comic 0', 'resourceURI' => Client::BASE_URL . 'comics/0'],
25
            ['id' => 1, 'title' => 'a title for comic 1', 'resourceURI' => Client::BASE_URL . 'comics/1'],
26
            ['id' => 2, 'title' => 'a title for comic 2', 'resourceURI' => Client::BASE_URL . 'comics/2'],
27
            ['id' => 3, 'title' => 'a title for comic 3', 'resourceURI' => Client::BASE_URL . 'comics/3'],
28
            ['id' => 4, 'title' => 'a title for comic 4', 'resourceURI' => Client::BASE_URL . 'comics/4'],
29
        ];
30
31
        $queryString = parse_url($request->getUrl(), PHP_URL_QUERY);
32
        $queryParams = [];
33
        parse_str($queryString, $queryParams);
34
35
        $offset = (int)$queryParams['offset'];
36
        $limit = (int)$queryParams['limit'];
37
        $results = array_slice($allResults, $offset, $limit);
38
        $count = count($results);
39
        return new Response(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Chadicus\Mar...esults' => $results))); (Chadicus\Marvel\Api\Response) is incompatible with the return type declared by the interface Chadicus\Marvel\Api\Adapter\AdapterInterface::send of type Chadicus\Marvel\Api\Adapter\ResponseInterface.

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...
40
            200,
41
            ['Content-type' => 'application/json', 'etag' => 'an etag'],
42
            [
43
                'code' => 200,
44
                'status' => 'ok',
45
                'etag' => 'an etag',
46
                'data' => [
47
                    'offset' => $offset,
48
                    'limit' => $limit,
49
                    'total' => 5,
50
                    'count' => $count,
51
                    'results' => $results,
52
                ],
53
            ]
54
        );
55
    }
56
}
57