1 | <?php |
||
23 | class Client implements HttpClient, HttpAsyncClient |
||
24 | { |
||
25 | use HttpAsyncClientEmulator; |
||
26 | use VersionBridgeClient; |
||
27 | |||
28 | /** |
||
29 | * @var ResponseFactory |
||
30 | */ |
||
31 | private $responseFactory; |
||
32 | |||
33 | /** |
||
34 | * @var RequestInterface[] |
||
35 | */ |
||
36 | private $requests = []; |
||
37 | |||
38 | /** |
||
39 | * @var ResponseInterface[] |
||
40 | */ |
||
41 | private $responses = []; |
||
42 | |||
43 | /** |
||
44 | * @var ResponseInterface|null |
||
45 | */ |
||
46 | private $defaultResponse; |
||
47 | |||
48 | /** |
||
49 | * @var Exception[] |
||
50 | */ |
||
51 | private $exceptions = []; |
||
52 | |||
53 | /** |
||
54 | * @var Exception|null |
||
55 | */ |
||
56 | private $defaultException; |
||
57 | |||
58 | 11 | public function __construct(ResponseFactory $responseFactory = null) |
|
59 | { |
||
60 | 11 | $this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find(); |
|
61 | 11 | } |
|
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | 7 | public function doSendRequest(RequestInterface $request) |
|
67 | { |
||
68 | 7 | $this->requests[] = $request; |
|
69 | |||
70 | 7 | if (count($this->exceptions) > 0) { |
|
71 | 1 | throw array_shift($this->exceptions); |
|
72 | } |
||
73 | |||
74 | 6 | if (count($this->responses) > 0) { |
|
75 | 2 | return array_shift($this->responses); |
|
76 | } |
||
77 | |||
78 | 4 | if ($this->defaultException) { |
|
79 | 1 | throw $this->defaultException; |
|
80 | } |
||
81 | |||
82 | 3 | if ($this->defaultResponse) { |
|
83 | 1 | return $this->defaultResponse; |
|
84 | } |
||
85 | |||
86 | // Return success response by default |
||
87 | 2 | return $this->responseFactory->createResponse(); |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Adds an exception that will be thrown. |
||
92 | */ |
||
93 | 2 | public function addException(\Exception $exception) |
|
94 | { |
||
95 | 2 | $this->exceptions[] = $exception; |
|
96 | 2 | } |
|
97 | |||
98 | /** |
||
99 | * Sets the default exception to throw when the list of added exceptions and responses is exhausted. |
||
100 | * |
||
101 | * If both a default exception and a default response are set, the exception will be thrown. |
||
102 | */ |
||
103 | 2 | public function setDefaultException(\Exception $defaultException = null) |
|
104 | { |
||
105 | 2 | $this->defaultException = $defaultException; |
|
|
|||
106 | 2 | } |
|
107 | |||
108 | /** |
||
109 | * Adds a response that will be returned in first in first out order. |
||
110 | */ |
||
111 | 3 | public function addResponse(ResponseInterface $response) |
|
112 | { |
||
113 | 3 | $this->responses[] = $response; |
|
114 | 3 | } |
|
115 | |||
116 | /** |
||
117 | * Sets the default response to be returned when the list of added exceptions and responses is exhausted. |
||
118 | */ |
||
119 | 2 | public function setDefaultResponse(ResponseInterface $defaultResponse = null) |
|
123 | |||
124 | /** |
||
125 | * Returns requests that were sent. |
||
126 | * |
||
127 | * @return RequestInterface[] |
||
128 | */ |
||
129 | 1 | public function getRequests() |
|
130 | { |
||
131 | 1 | return $this->requests; |
|
132 | } |
||
133 | |||
134 | 2 | public function getLastRequest() |
|
138 | |||
139 | 1 | public function reset() |
|
147 | } |
||
148 |
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.