Completed
Push — master ( 59b3e2...6dd6b9 )
by Andrii
15:38
created

AbstractRequest   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 248
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 48
lcom 1
cbo 3
dl 0
loc 248
rs 8.4864
c 0
b 0
f 0

33 Methods

Rating   Name   Duplication   Size   Complexity  
send() 0 1 ?
A __construct() 0 5 1
A getDbname() 0 4 1
A getMethod() 0 4 1
A getUri() 0 4 1
A getFullUri() 0 8 2
A createFullUri() 0 4 2
A isFullUri() 0 4 1
A getHeaders() 0 4 1
A getBody() 0 4 1
A getVersion() 0 4 1
A getQuery() 0 4 1
A build() 0 9 3
A updateFromQuery() 0 14 1
A buildDbname() 0 4 1
A buildAuth() 0 4 1
A buildMethod() 0 4 2
A buildUri() 0 4 1
A buildQueryParams() 0 10 3
A buildHeaders() 0 7 2
A buildBody() 0 4 1
A buildFormParams() 0 4 1
A setFormParams() 0 7 3
A buildProtocolVersion() 0 4 2
A serialize() 0 4 1
A unserialize() 0 6 2
A getParts() 0 11 3
A isRaw() 0 4 1
A getHandler() 0 9 2
A createHandler() 0 6 1
A prepareHandlerConfig() 0 4 1
A prepareUserAgent() 0 4 1
A getDb() 0 4 2

How to fix   Complexity   

Complex Class

Complex classes like AbstractRequest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AbstractRequest, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Tools to use API as ActiveRecord for Yii2
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart
6
 * @package   yii2-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart;
12
13
abstract class AbstractRequest implements RequestInterface
14
{
15
    /**
16
     * @var string response implementation to be specified in concrete implementation
17
     */
18
    protected $responseClass;
19
20
    /**
21
     * @var QueryBuilderInterface
22
     */
23
    protected $builder;
24
25
    /**
26
     * @var Query
27
     */
28
    protected $query;
29
30
    /**
31
     * @var string Connection name
32
     */
33
    protected $dbname;
34
35
    /**
36
     * @var array request method
37
     */
38
    protected $method;
39
    protected $uri;
40
    protected $headers = [];
41
    protected $body;
42
    protected $version;
43
44
    protected $isBuilt;
45
    protected $parts = [];
46
    protected $fullUri;
47
48
    abstract public function send($options = []);
49
50
    public function __construct(QueryBuilderInterface $builder, Query $query)
51
    {
52
        $this->builder = $builder;
53
        $this->query = $query;
54
    }
55
56
    public function getDbname()
57
    {
58
        return $this->dbname;
59
    }
60
61
    public function getMethod()
62
    {
63
        return $this->method;
64
    }
65
66
    public function getUri()
67
    {
68
        return $this->uri;
69
    }
70
71
    public function getFullUri()
72
    {
73
        if ($this->fullUri === null) {
74
            $this->fullUri = $this->createFullUri();
75
        }
76
77
        return $this->fullUri;
78
    }
79
80
    public function createFullUri()
81
    {
82
        return ($this->isFullUri($this->uri) ? '' : $this->getDb()->getBaseUri()) . $this->uri;
83
    }
84
85
    public function isFullUri($uri)
86
    {
87
        return preg_match('/^https?:\\/\\//i', $uri);
88
    }
89
90
    public function getHeaders()
91
    {
92
        return $this->headers;
93
    }
94
95
    public function getBody()
96
    {
97
        return $this->body;
98
    }
99
100
    public function getVersion()
101
    {
102
        return $this->version;
103
    }
104
105
    /**
106
     * @return Query
107
     */
108
    public function getQuery()
109
    {
110
        return $this->query;
111
    }
112
113
    protected function build()
114
    {
115
        if ($this->isBuilt === null) {
116
            if (!empty($this->query)) {
117
                $this->updateFromQuery();
118
            }
119
            $this->isBuilt = true;
120
        }
121
    }
122
123
    protected function updateFromQuery()
124
    {
125
        $this->builder->prepare($this->query);
126
127
        $this->buildDbname();
128
        $this->buildAuth();
129
        $this->buildMethod();
130
        $this->buildUri();
131
        $this->buildQueryParams();
132
        $this->buildHeaders();
133
        $this->buildBody();
134
        $this->buildFormParams();
135
        $this->buildProtocolVersion();
136
    }
137
138
    protected function buildDbname()
139
    {
140
        $this->dbname = $this->getDb()->name;
141
    }
142
143
    protected function buildAuth()
144
    {
145
        $this->builder->buildAuth($this->query);
146
    }
147
148
    protected function buildMethod()
149
    {
150
        $this->method = $this->builder->buildMethod($this->query) ?: 'GET';
151
    }
152
153
    protected function buildUri()
154
    {
155
        $this->uri = $this->builder->buildUri($this->query);
156
    }
157
158
    protected function buildQueryParams()
159
    {
160
        $params = $this->builder->buildQueryParams($this->query);
161
        if (is_array($params)) {
162
            $params = http_build_query($params, '', '&');
163
        }
164
        if (!empty($params)) {
165
            $this->uri .= '?' . $params;
166
        }
167
    }
168
169
    protected function buildHeaders()
170
    {
171
        $this->headers = $this->builder->buildHeaders($this->query);
172
        if (empty($this->headers['User-Agent'])) {
173
            $this->headers['User-Agent'] = $this->prepareUserAgent();
174
        }
175
    }
176
177
    protected function buildBody()
178
    {
179
        $this->body = $this->builder->buildBody($this->query);
180
    }
181
182
    protected function buildFormParams()
183
    {
184
        $this->setFormParams($this->builder->buildFormParams($this->query));
185
    }
186
187
    protected function setFormParams($params)
188
    {
189
        if (!empty($params)) {
190
            $this->body = is_array($params) ? http_build_query($params, '', '&') : $params;
191
            $this->headers['Content-Type'] = 'application/x-www-form-urlencoded';
192
        }
193
    }
194
195
    protected function buildProtocolVersion()
196
    {
197
        $this->version = $this->builder->buildProtocolVersion($this->query) ?: '1.1';
198
    }
199
200
    public function serialize()
201
    {
202
        return serialize($this->getParts());
203
    }
204
205
    public function unserialize($string)
206
    {
207
        foreach (unserialize($string) as $key => $value) {
208
            $this->{$key} = $value;
209
        }
210
    }
211
212
    public function getParts()
213
    {
214
        if (empty($this->parts)) {
215
            $this->build();
216
            foreach (['dbname', 'method', 'uri', 'headers', 'body', 'version'] as $key) {
217
                $this->parts[$key] = $this->{$key};
218
            }
219
        }
220
221
        return $this->parts;
222
    }
223
224
    public function isRaw()
225
    {
226
        return !empty($this->query->options['raw']);
227
    }
228
229
    protected function getHandler()
230
    {
231
        $handler = $this->getDb()->getHandler();
232
        if ($handler === null) {
233
            $handler = $this->createHandler();
234
        }
235
236
        return $handler;
237
    }
238
239
    protected function createHandler()
240
    {
241
        $config = $this->prepareHandlerConfig($this->getDb()->config);
242
243
        return new $this->handlerClass($config);
0 ignored issues
show
Bug introduced by
The property handlerClass 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...
244
    }
245
246
    protected function prepareHandlerConfig($config)
247
    {
248
        return $config;
249
    }
250
251
    protected function prepareUserAgent()
252
    {
253
        return $this->getDb()->getUserAgent();
254
    }
255
256
    public function getDb()
257
    {
258
        return isset($this->builder) ? $this->builder->db : AbstractConnection::getDb($this->dbname);
0 ignored issues
show
Bug introduced by
Accessing db on the interface hiqdev\hiart\QueryBuilderInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
259
    }
260
}
261