Http   B
last analyzed

Complexity

Total Complexity 47

Size/Duplication

Total Lines 310
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 73
c 2
b 0
f 0
dl 0
loc 310
ccs 100
cts 100
cp 1
rs 8.64
wmc 47

26 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 10 6
A __construct() 0 4 1
A getHost() 0 3 1
A getPath() 0 3 1
A createFromComponents() 0 3 1
A withQuery() 0 13 3
A createFromBaseUri() 0 3 1
A __toString() 0 3 1
A withHost() 0 13 3
A createFromServer() 0 3 1
A getUserInfo() 0 3 1
A createFromUri() 0 7 2
A getPort() 0 3 1
A jsonSerialize() 0 3 1
A getQuery() 0 3 1
A getAuthority() 0 3 1
A withPath() 0 8 2
A createFromString() 0 3 1
A withFragment() 0 13 3
A getScheme() 0 3 1
A __set_state() 0 3 1
A getFragment() 0 3 1
A withScheme() 0 13 3
A withPort() 0 8 2
A filterInput() 0 7 4
A withUserInfo() 0 13 3

How to fix   Complexity   

Complex Class

Complex classes like Http 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.

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 Http, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * League.Uri (https://uri.thephpleague.com)
5
 *
6
 * (c) Ignace Nyamagana Butera <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace League\Uri;
15
16
use League\Uri\Contracts\UriInterface;
17
use League\Uri\Exceptions\SyntaxError;
18
use Psr\Http\Message\UriInterface as Psr7UriInterface;
19
use function is_object;
20
use function is_scalar;
21
use function method_exists;
22
use function sprintf;
23
24
final class Http implements Psr7UriInterface, \JsonSerializable
25
{
26
    /**
27
     * @var UriInterface
28
     */
29
    private $uri;
30
31
    /**
32
     * New instance.
33
     */
34 222
    private function __construct(UriInterface $uri)
35
    {
36 222
        $this->validate($uri);
37 222
        $this->uri = $uri;
38 222
    }
39
40
    /**
41
     * Validate the submitted uri against PSR-7 UriInterface.
42
     *
43
     * @throws SyntaxError if the given URI does not follow PSR-7 UriInterface rules
44
     */
45 228
    private function validate(UriInterface $uri): void
46
    {
47 228
        $scheme = $uri->getScheme();
48 228
        if (null === $scheme && '' === $uri->getHost()) {
49 2
            throw new SyntaxError(sprintf('an URI without scheme can not contains a empty host string according to PSR-7: %s', (string) $uri));
50
        }
51
52 228
        $port = $uri->getPort();
53 228
        if (null !== $port && ($port < 0 || $port > 65535)) {
54 2
            throw new SyntaxError(sprintf('The URI port is outside the established TCP and UDP port ranges: %s', (string) $uri->getPort()));
55
        }
56 228
    }
57
58
    /**
59
     * Static method called by PHP's var export.
60
     */
61 18
    public static function __set_state(array $components): self
62
    {
63 18
        return new self($components['uri']);
64
    }
65
66
    /**
67
     * Create a new instance from a string.
68
     *
69
     * @param string|mixed $uri
70
     */
71 200
    public static function createFromString($uri = ''): self
72
    {
73 200
        return new self(Uri::createFromString($uri));
74
    }
75
76
    /**
77
     * Create a new instance from a hash of parse_url parts.
78
     *
79
     * @param array $components a hash representation of the URI similar
80
     *                          to PHP parse_url function result
81
     */
82 2
    public static function createFromComponents(array $components): self
83
    {
84 2
        return new self(Uri::createFromComponents($components));
85
    }
86
87
    /**
88
     * Create a new instance from the environment.
89
     */
90 26
    public static function createFromServer(array $server): self
91
    {
92 26
        return new self(Uri::createFromServer($server));
93
    }
94
95
    /**
96
     * Create a new instance from a URI and a Base URI.
97
     *
98
     * The returned URI must be absolute.
99
     *
100
     * @param mixed $uri      the input URI to create
101
     * @param mixed $base_uri the base URI used for reference
102
     */
103 2
    public static function createFromBaseUri($uri, $base_uri = null): self
104
    {
105 2
        return new self(Uri::createFromBaseUri($uri, $base_uri));
106
    }
107
108
    /**
109
     * Create a new instance from a URI object.
110
     *
111
     * @param Psr7UriInterface|UriInterface $uri the input URI to create
112
     */
113 4
    public static function createFromUri($uri): self
114
    {
115 4
        if ($uri instanceof UriInterface) {
116 2
            return new self($uri);
117
        }
118
119 2
        return new self(Uri::createFromUri($uri));
120
    }
121
122
    /**
123
     * {@inheritDoc}
124
     */
125 186
    public function getScheme(): string
126
    {
127 186
        return (string) $this->uri->getScheme();
128
    }
129
130
    /**
131
     * {@inheritDoc}
132
     */
133 166
    public function getAuthority(): string
134
    {
135 166
        return (string) $this->uri->getAuthority();
136
    }
137
138
    /**
139
     * {@inheritDoc}
140
     */
141 76
    public function getUserInfo(): string
142
    {
143 76
        return (string) $this->uri->getUserInfo();
144
    }
145
146
    /**
147
     * {@inheritDoc}
148
     */
149 196
    public function getHost(): string
150
    {
151 196
        return (string) $this->uri->getHost();
152
    }
153
154
    /**
155
     * {@inheritDoc}
156
     */
157 76
    public function getPort(): ?int
158
    {
159 76
        return $this->uri->getPort();
160
    }
161
162
    /**
163
     * {@inheritDoc}
164
     */
165 170
    public function getPath(): string
166
    {
167 170
        return $this->uri->getPath();
168
    }
169
170
    /**
171
     * {@inheritDoc}
172
     */
173 44
    public function getQuery(): string
174
    {
175 44
        return (string) $this->uri->getQuery();
176
    }
177
178
    /**
179
     * {@inheritDoc}
180
     */
181 24
    public function getFragment(): string
182
    {
183 24
        return (string) $this->uri->getFragment();
184
    }
185
186
    /**
187
     * {@inheritDoc}
188
     */
189 10
    public function withScheme($scheme): self
190
    {
191 10
        $scheme = $this->filterInput($scheme);
192 10
        if ('' === $scheme) {
193 10
            $scheme = null;
194
        }
195
196 10
        $uri = $this->uri->withScheme($scheme);
197 10
        if ($uri->getScheme() === $this->uri->getScheme()) {
198 2
            return $this;
199
        }
200
201 8
        return new self($uri);
202
    }
203
204
    /**
205
     * Safely stringify input when possible.
206
     *
207
     * @param mixed $str the value to evaluate as a string
208
     *
209
     * @throws SyntaxError if the submitted data can not be converted to string
210
     *
211
     * @return string|mixed
212
     */
213 94
    private function filterInput($str)
214
    {
215 94
        if (is_scalar($str) || (is_object($str) && method_exists($str, '__toString'))) {
216 92
            return (string) $str;
217
        }
218
219 2
        return $str;
220
    }
221
222
    /**
223
     * {@inheritDoc}
224
     */
225 8
    public function withUserInfo($user, $password = null): self
226
    {
227 8
        $user = $this->filterInput($user);
228 8
        if ('' === $user) {
229 8
            $user = null;
230
        }
231
232 8
        $uri = $this->uri->withUserInfo($user, $password);
233 8
        if ($uri->getUserInfo() === $this->uri->getUserInfo()) {
234 8
            return $this;
235
        }
236
237 2
        return new self($uri);
238
    }
239
240
    /**
241
     * {@inheritDoc}
242
     */
243 90
    public function withHost($host): self
244
    {
245 90
        $host = $this->filterInput($host);
246 90
        if ('' === $host) {
247 12
            $host = null;
248
        }
249
250 90
        $uri = $this->uri->withHost($host);
251 90
        if ($uri->getHost() === $this->uri->getHost()) {
252 88
            return $this;
253
        }
254
255 8
        return new self($uri);
256
    }
257
258
    /**
259
     * {@inheritDoc}
260
     */
261 8
    public function withPort($port): self
262
    {
263 8
        $uri = $this->uri->withPort($port);
264 8
        if ($uri->getPort() === $this->uri->getPort()) {
265 8
            return $this;
266
        }
267
268 2
        return new self($uri);
269
    }
270
271
    /**
272
     * {@inheritDoc}
273
     */
274 38
    public function withPath($path): self
275
    {
276 38
        $uri = $this->uri->withPath($path);
277 30
        if ($uri->getPath() === $this->uri->getPath()) {
278 24
            return $this;
279
        }
280
281 26
        return new self($uri);
282
    }
283
284
    /**
285
     * {@inheritDoc}
286
     */
287 22
    public function withQuery($query): self
288
    {
289 22
        $query = $this->filterInput($query);
290 22
        if ('' === $query) {
291 22
            $query = null;
292
        }
293
294 22
        $uri = $this->uri->withQuery($query);
295 22
        if ($uri->getQuery() === $this->uri->getQuery()) {
296 22
            return $this;
297
        }
298
299 4
        return new self($uri);
300
    }
301
302
    /**
303
     * {@inheritDoc}
304
     */
305 22
    public function withFragment($fragment): self
306
    {
307 22
        $fragment = $this->filterInput($fragment);
308 22
        if ('' === $fragment) {
309 20
            $fragment = null;
310
        }
311
312 22
        $uri = $this->uri->withFragment($fragment);
313 22
        if ($uri->getFragment() === $this->uri->getFragment()) {
314 22
            return $this;
315
        }
316
317 2
        return new self($uri);
318
    }
319
320
    /**
321
     * {@inheritDoc}
322
     */
323 48
    public function __toString(): string
324
    {
325 48
        return $this->uri->__toString();
326
    }
327
328
    /**
329
     * {@inheritDoc}
330
     */
331 2
    public function jsonSerialize(): string
332
    {
333 2
        return $this->uri->__toString();
334
    }
335
}
336