1 | <?php |
||
2 | /** |
||
3 | * |
||
4 | */ |
||
5 | |||
6 | namespace Mvc5\Response; |
||
7 | |||
8 | use Mvc5\Event\Event; |
||
9 | use Mvc5\Event\EventModel; |
||
10 | use Mvc5\Http\Request; |
||
11 | use Mvc5\Http\Response; |
||
12 | |||
13 | use function array_filter; |
||
14 | |||
15 | use const Mvc5\{ BODY, CONTROLLER, EVENT, MODEL, REQUEST, RESPONSE }; |
||
16 | |||
17 | final class Dispatch |
||
18 | implements Event |
||
19 | { |
||
20 | /** |
||
21 | * |
||
22 | */ |
||
23 | use EventModel; |
||
24 | |||
25 | /** |
||
26 | * @var Request |
||
27 | */ |
||
28 | protected Request $request; |
||
29 | |||
30 | /** |
||
31 | * @var Response |
||
32 | */ |
||
33 | protected Response $response; |
||
34 | |||
35 | /** |
||
36 | * @param string $event |
||
37 | * @param Request|null $request |
||
38 | * @param Response|null $response |
||
39 | */ |
||
40 | 4 | function __construct(string $event, Request $request = null, Response $response = null) |
|
41 | { |
||
42 | 4 | $this->event = $event; |
|
43 | 4 | $this->request = $request; |
|
0 ignored issues
–
show
|
|||
44 | 4 | $this->response = $response; |
|
0 ignored issues
–
show
It seems like
$response can also be of type Mvc5\Http\Response . However, the property $response is declared as type Mvc5\RESPONSE . Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||
45 | 4 | } |
|
46 | |||
47 | /** |
||
48 | * @return array |
||
49 | */ |
||
50 | 4 | protected function args() : array |
|
51 | { |
||
52 | 4 | return array_filter([ |
|
53 | 4 | CONTROLLER => $this->request[CONTROLLER], |
|
54 | 4 | EVENT => $this, |
|
55 | 4 | MODEL => $this->response[BODY], |
|
56 | 4 | REQUEST => $this->request, |
|
57 | 4 | RESPONSE => $this->response |
|
58 | ]); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param callable $callable |
||
63 | * @param array $args |
||
64 | * @param callable|null $callback |
||
65 | * @return mixed |
||
66 | * @throws \Throwable |
||
67 | */ |
||
68 | 4 | function __invoke(callable $callable, array $args = [], callable $callback = null) |
|
69 | { |
||
70 | 4 | $result = $this->signal($callable, $this->args() + $args, $callback); |
|
71 | |||
72 | 4 | if ($result instanceof Request) { |
|
73 | 1 | return $this->request = $result; |
|
0 ignored issues
–
show
It seems like
$result of type Mvc5\Http\Request is incompatible with the declared type Mvc5\REQUEST 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...
|
|||
74 | } |
||
75 | |||
76 | 3 | if ($result instanceof Response) { |
|
77 | 1 | return $this->response = $result; |
|
0 ignored issues
–
show
It seems like
$result of type Mvc5\Http\Response is incompatible with the declared type Mvc5\RESPONSE of property $response .
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...
|
|||
78 | } |
||
79 | |||
80 | 2 | null !== $result && |
|
81 | 2 | $this->response = $this->response->with(BODY, $result); |
|
82 | |||
83 | 2 | return $result; |
|
84 | } |
||
85 | } |
||
86 |
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 mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.