Issues (92)

src/Request/RequestAwareTrait.php (2 issues)

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
$request is of type Psr\Http\Message\RequestInterface, but the property $request was declared to be of type Nip\Request|null. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
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();
0 ignored issues
show
Deprecated Code introduced by
The class Nip\Request has been deprecated: use \Nip\Http\Request ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

87
        return /** @scrutinizer ignore-deprecated */ new Request();
Loading history...
88
    }
89
}
90