Completed
Push — master ( c22a1a...c65aed )
by
unknown
02:00
created

RequestTrait::updateHostFromUri()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.0961

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 9
cts 11
cp 0.8182
rs 9.6
c 0
b 0
f 0
cc 4
nc 5
nop 0
crap 4.0961
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nyholm\Psr7;
6
7
use InvalidArgumentException;
8
use Psr\Http\Message\UriInterface;
9
10
/**
11
 * @author Michael Dowling and contributors to guzzlehttp/psr7
12
 * @author Tobias Nyholm <[email protected]>
13
 */
14
trait RequestTrait
15
{
16
    /** @var string */
17
    private $method;
18
19
    /** @var null|string */
20
    private $requestTarget;
21
22
    /** @var null|UriInterface */
23
    private $uri;
24
25 1
    public function getRequestTarget(): string
26
    {
27 1
        if (null !== $this->requestTarget) {
28 1
            return $this->requestTarget;
29
        }
30
31 1
        if ('' === $target = $this->uri->getPath()) {
32
            $target = '/';
33
        }
34 1
        if ('' !== $this->uri->getQuery()) {
35
            $target .= '?'.$this->uri->getQuery();
36
        }
37
38 1
        return $target;
39
    }
40
41 1
    public function withRequestTarget($requestTarget): self
42
    {
43 1
        if (preg_match('#\s#', $requestTarget)) {
44
            throw new InvalidArgumentException('Invalid request target provided; cannot contain whitespace');
45
        }
46
47 1
        $new = clone $this;
48 1
        $new->requestTarget = $requestTarget;
49
50 1
        return $new;
51
    }
52
53 26
    public function getMethod(): string
54
    {
55 26
        return $this->method;
56
    }
57
58 4
    public function withMethod($method): self
59
    {
60 4
        if (!is_string($method)) {
61 3
            throw new \InvalidArgumentException('Method must be a string');
62
        }
63
64 1
        $new = clone $this;
65 1
        $new->method = $method;
66
67 1
        return $new;
68
    }
69
70 26
    public function getUri(): UriInterface
71
    {
72 26
        return $this->uri;
73
    }
74
75 4
    public function withUri(UriInterface $uri, $preserveHost = false): self
76
    {
77 4
        if ($uri === $this->uri) {
78
            return $this;
79
        }
80
81 4
        $new = clone $this;
82 4
        $new->uri = $uri;
83
84 4
        if (!$preserveHost || !$this->hasHeader('Host')) {
1 ignored issue
show
Bug introduced by
It seems like hasHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
85 4
            $new->updateHostFromUri();
86
        }
87
88 4
        return $new;
89
    }
90
91 79
    private function updateHostFromUri(): void
92
    {
93 79
        if ('' === $host = $this->uri->getHost()) {
94 50
            return;
95
        }
96
97 32
        if (null !== ($port = $this->uri->getPort())) {
98
            $host .= ':'.$port;
99
        }
100
101 32
        if (isset($this->headerNames['host'])) {
102
            $header = $this->headerNames['host'];
1 ignored issue
show
Bug introduced by
The property headerNames 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...
103
        } else {
104 32
            $header = 'Host';
105 32
            $this->headerNames['host'] = 'Host';
106
        }
107
        // Ensure Host is the first header.
108
        // See: http://tools.ietf.org/html/rfc7230#section-5.4
109 32
        $this->headers = [$header => [$host]] + $this->headers;
1 ignored issue
show
Bug introduced by
The property headers 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...
110 32
    }
111
}
112