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

CharacterAdapter::send()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 196
Code Lines 157

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 196
rs 8.2857
c 0
b 0
f 0
cc 1
eloc 157
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 CharacterAdapter implements AdapterInterface
13
{
14
    /**
15
     * The parameters sent with the last request.
16
     *
17
     * @var array
18
     */
19
    public $parameters = [];
20
21
    /**
22
     * Simulate sending a request to the API.
23
     *
24
     * @param RequestInterface $request The request.
25
     *
26
     * @return ResponseInterface
27
     */
28
    public function send(RequestInterface $request)
29
    {
30
        $allResults = [
31
            [
32
                'id' => 0,
33
                'name' => 'a name for character 0',
34
                'description' => 'a description for character 0',
35
                'modified' => '2014-01-21T18:01:51-0500',
36
                'resourceURI' => Client::BASE_URL . 'characters/0',
37
                'urls' => [['type' => 'a type', 'url' => 'a url']],
38
                'thumbnail' => ['path' => 'a path', 'extension' => 'an extension'],
39
                'comics' => [
40
                    'available' => 2,
41
                    'returned' => 1,
42
                    'collectionURI' => 'a comics collection uri',
43
                    'items' => [['resourceURI' => 'a comics resource uri', 'name' => 'a comics name']],
44
                ],
45
                'stories' => [
46
                    'available' => 2,
47
                    'returned' => 1,
48
                    'collectionURI' => 'a stories collection uri',
49
                    'items' => [['resourceURI' => 'a stories resource uri', 'name' => 'a stories name']],
50
                ],
51
                'events' => [
52
                    'available' => 2,
53
                    'returned' => 1,
54
                    'collectionURI' => 'a events collection uri',
55
                    'items' => [['resourceURI' => 'a events resource uri', 'name' => 'a events name']],
56
                ],
57
                'series' => [
58
                    'available' => 2,
59
                    'returned' => 1,
60
                    'collectionURI' => 'a series collection uri',
61
                    'items' => [['resourceURI' => 'a series resource uri', 'name' => 'a series name']],
62
                ],
63
            ],
64
            [
65
                'id' => 1,
66
                'name' => 'a name for character 1',
67
                'description' => 'a description for character 1',
68
                'modified' => '2014-01-21T18:01:51-0500',
69
                'resourceURI' => Client::BASE_URL . 'characters/1',
70
                'urls' => [['type' => 'a type', 'url' => 'a url']],
71
                'thumbnail' => ['path' => 'a path', 'extension' => 'an extension'],
72
                'comics' => [
73
                    'available' => 2,
74
                    'returned' => 1,
75
                    'collectionURI' => 'a comics collection uri',
76
                    'items' => [['resourceURI' => 'a comics resource uri', 'name' => 'a comics name']],
77
                ],
78
                'stories' => [
79
                    'available' => 2,
80
                    'returned' => 1,
81
                    'collectionURI' => 'a stories collection uri',
82
                    'items' => [['resourceURI' => 'a stories resource uri', 'name' => 'a stories name']],
83
                ],
84
                'events' => [
85
                    'available' => 2,
86
                    'returned' => 1,
87
                    'collectionURI' => 'a events collection uri',
88
                    'items' => [['resourceURI' => 'a events resource uri', 'name' => 'a events name']],
89
                ],
90
                'series' => [
91
                    'available' => 2,
92
                    'returned' => 1,
93
                    'collectionURI' => 'a series collection uri',
94
                    'items' => [['resourceURI' => 'a series resource uri', 'name' => 'a series name']],
95
                ],
96
            ],
97
            [
98
                'id' => 2,
99
                'name' => 'a name for character 2',
100
                'description' => 'a description for character 2',
101
                'modified' => '2014-01-21T18:01:51-0500',
102
                'resourceURI' => Client::BASE_URL . 'characters/2',
103
                'urls' => [['type' => 'a type', 'url' => 'a url']],
104
                'thumbnail' => ['path' => 'a path', 'extension' => 'an extension'],
105
                'comics' => [
106
                    'available' => 2,
107
                    'returned' => 1,
108
                    'collectionURI' => 'a comics collection uri',
109
                    'items' => [['resourceURI' => 'a comics resource uri', 'name' => 'a comics name']],
110
                ],
111
                'stories' => [
112
                    'available' => 2,
113
                    'returned' => 1,
114
                    'collectionURI' => 'a stories collection uri',
115
                    'items' => [['resourceURI' => 'a stories resource uri', 'name' => 'a stories name']],
116
                ],
117
                'events' => [
118
                    'available' => 2,
119
                    'returned' => 1,
120
                    'collectionURI' => 'a events collection uri',
121
                    'items' => [['resourceURI' => 'a events resource uri', 'name' => 'a events name']],
122
                ],
123
                'series' => [
124
                    'available' => 2,
125
                    'returned' => 1,
126
                    'collectionURI' => 'a series collection uri',
127
                    'items' => [['resourceURI' => 'a series resource uri', 'name' => 'a series name']],
128
                ],
129
            ],
130
            [
131
                'id' => 3,
132
                'name' => 'a name for character 3',
133
                'description' => 'a description for character 3',
134
                'modified' => '2014-01-21T18:01:51-0500',
135
                'resourceURI' => Client::BASE_URL . 'characters/3',
136
                'urls' => [['type' => 'a type', 'url' => 'a url']],
137
                'thumbnail' => ['path' => 'a path', 'extension' => 'an extension'],
138
                'comics' => [
139
                    'available' => 2,
140
                    'returned' => 1,
141
                    'collectionURI' => 'a comics collection uri',
142
                    'items' => [['resourceURI' => 'a comics resource uri', 'name' => 'a comics name']],
143
                ],
144
                'stories' => [
145
                    'available' => 2,
146
                    'returned' => 1,
147
                    'collectionURI' => 'a stories collection uri',
148
                    'items' => [['resourceURI' => 'a stories resource uri', 'name' => 'a stories name']],
149
                ],
150
                'events' => [
151
                    'available' => 2,
152
                    'returned' => 1,
153
                    'collectionURI' => 'a events collection uri',
154
                    'items' => [['resourceURI' => 'a events resource uri', 'name' => 'a events name']],
155
                ],
156
                'series' => [
157
                    'available' => 2,
158
                    'returned' => 1,
159
                    'collectionURI' => 'a series collection uri',
160
                    'items' => [['resourceURI' => 'a series resource uri', 'name' => 'a series name']],
161
                ],
162
            ],
163
            [
164
                'id' => 4,
165
                'name' => 'a name for character 4',
166
                'description' => 'a description for character 4',
167
                'modified' => '2014-01-21T18:01:51-0500',
168
                'resourceURI' => Client::BASE_URL . 'characters/4',
169
                'urls' => [['type' => 'a type', 'url' => 'a url']],
170
                'thumbnail' => ['path' => 'a path', 'extension' => 'an extension'],
171
                'comics' => [
172
                    'available' => 2,
173
                    'returned' => 1,
174
                    'collectionURI' => 'a comics collection uri',
175
                    'items' => [['resourceURI' => 'a comics resource uri', 'name' => 'a comics name']],
176
                ],
177
                'stories' => [
178
                    'available' => 2,
179
                    'returned' => 1,
180
                    'collectionURI' => 'a stories collection uri',
181
                    'items' => [['resourceURI' => 'a stories resource uri', 'name' => 'a stories name']],
182
                ],
183
                'events' => [
184
                    'available' => 2,
185
                    'returned' => 1,
186
                    'collectionURI' => 'a events collection uri',
187
                    'items' => [['resourceURI' => 'a events resource uri', 'name' => 'a events name']],
188
                ],
189
                'series' => [
190
                    'available' => 2,
191
                    'returned' => 1,
192
                    'collectionURI' => 'a series collection uri',
193
                    'items' => [['resourceURI' => 'a series resource uri', 'name' => 'a series name']],
194
                ],
195
            ],
196
        ];
197
198
        $queryString = parse_url($request->getUrl(), PHP_URL_QUERY);
199
        $queryParams = [];
200
        parse_str($queryString, $queryParams);
201
        $this->parameters = $queryParams;
0 ignored issues
show
Documentation Bug introduced by
It seems like $queryParams can be null. However, the property $parameters is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
202
203
        $offset = (int)$queryParams['offset'];
204
        $limit = (int)$queryParams['limit'];
205
        $results = array_slice($allResults, $offset, $limit);
206
        $count = count($results);
207
        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...
208
            200,
209
            ['Content-type' => 'application/json', 'etag' => 'an etag'],
210
            [
211
                'code' => 200,
212
                'status' => 'ok',
213
                'etag' => 'an etag',
214
                'data' => [
215
                    'offset' => $offset,
216
                    'limit' => $limit,
217
                    'total' => 5,
218
                    'count' => $count,
219
                    'results' => $results,
220
                ],
221
            ]
222
        );
223
    }
224
}
225