Completed
Push — master ( 9e1c2f...16e6fb )
by Bohuslav
03:00
created

Request   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 6.98 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 6
c 3
b 2
f 0
lcom 0
cbo 4
dl 3
loc 43
ccs 13
cts 13
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 3 24 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Kambo\HttpMessage;
3
4
// \Spl
5
use InvalidArgumentException;
6
7
// \Psr
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\UriInterface;
10
11
// \HttpMessage
12
use Kambo\HttpMessage\Message;
13
use Kambo\HttpMessage\Headers;
14
use Kambo\HttpMessage\RequestTrait;
15
use Kambo\HttpMessage\Factories\String\UriFactory;
16
use Kambo\HttpMessage\Stream;
17
18
/**
19
 * Representation of an outgoing, client-side request.
20
 *
21
 * Per the HTTP specification, this interface includes properties for
22
 * each of the following:
23
 *
24
 * - Protocol version
25
 * - HTTP method
26
 * - URI
27
 * - Headers
28
 * - Message body
29
 *
30
 * During construction, implementations attempt to set the Host header from
31
 * a provided URI if no Host header is provided.
32
 *
33
 * Requests are considered immutable; all methods that change state retain
34
 * the internal state of the current message and return an instance that
35
 * contains the changed state.
36
 *
37
 * @package Kambo\HttpMessage
38
 * @author  Bohuslav Simek <[email protected]>
39
 * @license MIT
40
 */
41
class Request extends Message implements RequestInterface
42
{
43
    use RequestTrait;
44
45
    /**
46
     * Create new outgoing HTTP request.
47
     *
48
     * Adds a host header when none was provided and a host is defined in URI.
49
     *
50
     * @param string                      $requestMethod The request method
51
     * @param UriInterface|string         $uri           The request URI object
52
     * @param HeadersInterface            $headers       The request headers collection
53
     * @param StreamInterface|string|null $body          The request body object
54
     * @param string                      $protocol      The request version of the protocol
55
     *
56
     * @throws \InvalidArgumentException If an unsupported argument type is
57
     *                                   provided for URI or body.
58
     */
59 12
    public function __construct(
60
        $requestMethod,
61
        $uri,
62
        $headers = null,
63
        $body = null,
64
        $protocol = '1.1'
65
    ) {
66 12
        parent::__construct($headers, $body, $protocol);
67
68 11
        if (is_string($uri)) {
69 10
            $this->uri = UriFactory::create($uri);
70 11
        } elseif (!($uri instanceof UriInterface)) {
71 1
            throw new InvalidArgumentException(
72
                'URI must be a string or implement Psr\Http\Message\UriInterface'
73 1
            );
74
        }
75
76 10
        $this->validateMethod($requestMethod);
77 10
        $this->requestMethod   = $requestMethod;
78
79 10 View Code Duplication
        if ($this->uri->getHost() !== '' && (!$this->hasHeader('Host') || $this->getHeader('Host') === null)) {
80 9
            $this->headers->set('Host', $this->uri->getHost());
81 9
        }
82 10
    }
83
}
84