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

ComicAdapter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 200
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B send() 0 41 2
B getAllResults() 0 136 2
1
<?php
2
namespace Chadicus\Marvel\Api\Assets;
3
4
use Chadicus\Marvel\Api\Adapter\AdapterInterface;
5
use Chadicus\Marvel\Api\Entities;
6
use Chadicus\Marvel\Api\Client;
7
use Chadicus\Marvel\Api\RequestInterface;
8
use Chadicus\Marvel\Api\Response;
9
10
/**
11
 * Adapter that returns multiple items.
12
 */
13
final class ComicAdapter implements AdapterInterface
14
{
15
    /**
16
     * The parameters sent with the last request.
17
     *
18
     * @var array
19
     */
20
    public $parameters = [];
21
22
    /**
23
     * Simulate sending a request to the API.
24
     *
25
     * @param RequestInterface $request The request.
26
     *
27
     * @return ResponseInterface
28
     */
29
    public function send(RequestInterface $request)
30
    {
31
        $allResults = self::getAllResults();
32
        $total = count($allResults);
33
34
        $queryString = parse_url($request->getUrl(), PHP_URL_QUERY);
35
        $queryParams = [];
36
        parse_str($queryString, $queryParams);
37
38
        $path = parse_url($request->getUrl(), PHP_URL_PATH);
39
        if (substr($path, -6) !== 'comics') {
40
            $parts = explode('/', $request->getUrl());
41
            $id = array_pop($parts);
42
            $queryParams['offset'] = $id - 1;
43
            $queryParams['limit'] = 1;
44
            $total = 1;
45
        }
46
47
        $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...
48
49
        $offset = (int)$queryParams['offset'];
50
        $limit = (int)$queryParams['limit'];
51
        $results = array_slice($allResults, $offset, $limit);
52
        $count = count($results);
53
        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...
54
            200,
55
            ['Content-type' => 'application/json', 'etag' => 'an etag'],
56
            [
57
                'code' => 200,
58
                'status' => 'ok',
59
                'etag' => 'an etag',
60
                'data' => [
61
                    'offset' => $offset,
62
                    'limit' => $limit,
63
                    'total' => $total,
64
                    'count' => $count,
65
                    'results' => $results,
66
                ],
67
            ]
68
        );
69
    }
70
71
    /**
72
     * Helper method to get test data
73
     *
74
     * @return array
75
     */
76
    private static function getAllResults()
77
    {
78
        $allResults = [];
79
        for ($i = 1; $i <= 5; $i++) {
80
            $allResults[] = [
81
                'id' => $i,
82
                'digitalId' => $i + 1,
83
                'title' => "a title for comic {$i}",
84
                'issueNumber' => 3,
85
                'variantDescription' => "a variant description for comic {$i}",
86
                'description' => "a description for comic {$i}",
87
                'modified' => 'Fri, 19 Jun 2015 15:54:05 -0400',
88
                'isbn' => "an isbn for comic {$i}",
89
                'upc' => "a upc for comic {$i}",
90
                'diamondCode' => "a diamond code for comic {$i}",
91
                'ean' => "an ean for comic {$i}",
92
                'issn' => "an issn for comic {$i}",
93
                'format' => "a format for comic {$i}",
94
                'pageCount' => 4,
95
                'textObjects' => [
96
                    [
97
                        'type' => "a text object type for comic {$i}",
98
                        'language' => "a language for comic {$i}",
99
                        'text' => 'a text',
100
                    ],
101
                ],
102
                'resourceURI' => Client::BASE_URL . Entities\Comic::API_RESOURCE . "/$i",
103
                'urls' => [['type' => "a url type for comic {$i}", 'url' => 'a url' ]],
104
                'series' => [
105
                    'resourceURI' => "a series resource URI for comic {$i}",
106
                    'name' => "a series name for comic {$i}",
107
                    'type' => "a series type for comic {$i}",
108
                    'role' => "a series role for comic {$i}",
109
                ],
110
                'events' => [
111
                    'available' => 1,
112
                    'returned' => 1,
113
                    'collectionURI' => "an events collection uri for comic {$i}",
114
                    'items' => [
115
                        [
116
                            'resourceURI' => "a event resource URI for comic {$i}",
117
                            'name' => "a event name for comic {$i}",
118
                            'type' => "a event type for comic {$i}",
119
                            'role' => "a event role for comic {$i}",
120
                        ]
121
                    ],
122
                ],
123
                'stories' => [
124
                    'available' => 1,
125
                    'returned' => 1,
126
                    'collectionURI' => "an stories collection uri for comic {$i}",
127
                    'items' => [
128
                        [
129
                            'resourceURI' => "a story resource URI for comic {$i}",
130
                            'name' => "a story name for comic {$i}",
131
                            'type' => "a story type for comic {$i}",
132
                            'role' => "a story role for comic {$i}",
133
                        ]
134
                    ],
135
                ],
136
                'creators' => [
137
                    'available' => 1,
138
                    'returned' => 1,
139
                    'collectionURI' => "an creators collection uri for comic {$i}",
140
                    'items' => [
141
                        [
142
                            'resourceURI' => "a creator resource URI for comic {$i}",
143
                            'name' => "a creator name for comic {$i}",
144
                            'type' => "a creator type for comic {$i}",
145
                            'role' => "a creator role for comic {$i}",
146
                        ]
147
                    ],
148
                ],
149
                'characters' => [
150
                    'available' => 1,
151
                    'returned' => 1,
152
                    'collectionURI' => "an characters collection uri for comic {$i}",
153
                    'items' => [
154
                        [
155
                            'resourceURI' => "a character resource URI for comic {$i}",
156
                            'name' => "a character name for comic {$i}",
157
                            'type' => "a character type for comic {$i}",
158
                            'role' => "a character role for comic {$i}",
159
                        ]
160
                    ],
161
                ],
162
                'variants' => [
163
                    [
164
                        'resourceURI' => "a variant resource URI for comic {$i}",
165
                        'name' => "a variant name for comic {$i}",
166
                        'type' => "a variant type for comic {$i}",
167
                        'role' => "a variant role for comic {$i}",
168
                    ],
169
                ],
170
                'collections' => [
171
                    [
172
                        'resourceURI' => "a collection resource URI for comic {$i}",
173
                        'name' => "a collection name for comic {$i}",
174
                        'type' => "a collection type for comic {$i}",
175
                        'role' => "a collection role for comic {$i}",
176
                    ],
177
                ],
178
                'collectedIssues' => [
179
                    [
180
                        'resourceURI' => "a collected issues resource URI for comic {$i}",
181
                        'name' => "a collected issues name for comic {$i}",
182
                        'type' => "a collected issues type for comic {$i}",
183
                        'role' => "a collected issues role for comic {$i}",
184
                    ],
185
                ],
186
                'dates' => [
187
                    [
188
                        'type' => "a date type for comic {$i}",
189
                        'date' => 'Fri, 31 Jul 2015 08:53:11 -0400',
190
                    ],
191
                ],
192
                'prices' => [
193
                    [
194
                        'type' => "a price type for comic {$i}",
195
                        'price' => 1.1,
196
                    ],
197
                ],
198
                'thumbnail' => [
199
                    'path' => "a thumbnail path for comic {$i}",
200
                    'extension' => "a thumbnail extension for comic {$i}",
201
                ],
202
                'images' => [
203
                    [
204
                        'path' => "an image path for comic {$i}",
205
                        'extension' => "an image extension for comic {$i}",
206
                    ],
207
                ],
208
            ];
209
        }
210
        return $allResults;
211
    }
212
}
213