Completed
Push — master ( 8e197a...53f998 )
by Gabriel
02:44
created

RequestAwareTrait::initRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Nip\Http\Request;
4
5
use Nip\Request;
6
use Psr\Http\Message\RequestInterface;
7
8
/**
9
 * Trait RequestAwareTrait
10
 * @package Nip\Http\Request
11
 */
12
trait RequestAwareTrait
13
{
14
    /**
15
     * @var Request|null
16
     */
17
    protected $request = null;
18
19
    /**
20
     * @var boolean
21
     */
22
    protected $autoInitRequest = false;
23
24
    /**
25
     * Get the Request.
26
     *
27
     * @param bool $autoInit
28
     * @return Request
29
     */
30
    public function getRequest($autoInit = false)
31
    {
32
        $this->setAutoInitRequest($autoInit);
33
        if ($this->request == null && $this->isAutoInitRequest()) {
34
            $this->initRequest();
35
        }
36
37
        return $this->request;
38
    }
39
40
    /**
41
     * Set a container.
42
     *
43
     * @param Request|RequestInterface $request
44
     * @return $this
45
     */
46
    public function setRequest(RequestInterface $request)
47
    {
48
        $this->request = $request;
0 ignored issues
show
Documentation Bug introduced by
It seems like $request of type object<Psr\Http\Message\RequestInterface> is incompatible with the declared type object<Nip\Request>|null of property $request.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
50
        return $this;
51
    }
52
53
    /**
54
     * @return bool
55
     */
56
    public function hasRequest()
57
    {
58
        return $this->request instanceof RequestInterface;
59
    }
60
61
    /**
62
     * @return bool
63
     */
64
    public function isAutoInitRequest(): bool
65
    {
66
        return $this->autoInitRequest;
67
    }
68
69
    /**
70
     * @param bool $autoInitRequest
71
     */
72
    public function setAutoInitRequest(bool $autoInitRequest)
73
    {
74
        $this->autoInitRequest = $autoInitRequest;
75
    }
76
77
    public function initRequest()
78
    {
79
        $this->request = $this->newRequest();
80
    }
81
82
    /**
83
     * @return Request
84
     */
85
    public function newRequest()
86
    {
87
        return new Request();
88
    }
89
}
90