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

SingleAdapter::send()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 17
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 implementation that only returns a responses with one item.
11
 */
12
final class SingleAdapter implements AdapterInterface
13
{
14
    /**
15
     * Returns an empty Response.
16
     *
17
     * @param RequestInterface $request The request to send.
18
     *
19
     * @return ResponseInterface
20
     */
21
    public function send(RequestInterface $request)
22
    {
23
        $this->request = $request;
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
25
        return new Response(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Chadicus\Mar..._URL . 'comics/0'))))); (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...
26
            200,
27
            ['Content-type' => 'application/json', 'etag' => 'an etag'],
28
            [
29
                'code' => 200,
30
                'status' => 'ok',
31
                'etag' => 'an etag',
32
                'data' => [
33
                    'offset' => 0,
34
                    'limit' => 20,
35
                    'total' => 1,
36
                    'count' => 1,
37
                    'results' => [
38
                        [
39
                            'id' => 0,
40
                            'title' => 'a title for comic 0',
41
                            'resourceURI' => Client::BASE_URL . 'comics/0',
42
                        ],
43
                    ],
44
                ],
45
            ]
46
        );
47
    }
48
}
49