1 | <?php |
||
18 | class Logger |
||
19 | { |
||
20 | /** |
||
21 | * @var \Psr\Log\LoggerInterface|callable |
||
22 | */ |
||
23 | protected $logger; |
||
24 | |||
25 | /** |
||
26 | * @var \GuzzleHttp\MessageFormatter|callable |
||
27 | */ |
||
28 | protected $formatter; |
||
29 | |||
30 | /** |
||
31 | * @var string|callable Constant or callable that accepts a Response. |
||
32 | */ |
||
33 | protected $logLevel; |
||
34 | |||
35 | /** |
||
36 | * @var boolean Whether or not to log requests as they are made. |
||
37 | */ |
||
38 | protected $logRequests; |
||
39 | |||
40 | /** |
||
41 | * Creates a callable middleware for logging requests and responses. |
||
42 | * |
||
43 | * @param LoggerInterface|callable $logger |
||
44 | * @param string|callable Constant or callable that accepts a Response. |
||
45 | */ |
||
46 | 12 | public function __construct($logger, $formatter = null) |
|
52 | |||
53 | /** |
||
54 | * Returns the default formatter; |
||
55 | * |
||
56 | * @return MessageFormatter |
||
57 | */ |
||
58 | 10 | protected function getDefaultFormatter() |
|
62 | |||
63 | /** |
||
64 | * Sets whether requests should be logged before the response is received. |
||
65 | * |
||
66 | * @param boolean $logRequests |
||
67 | */ |
||
68 | 2 | public function setRequestLoggingEnabled($logRequests = true) |
|
72 | |||
73 | /** |
||
74 | * Sets the logger, which can be a PSR-3 logger or a callable that accepts |
||
75 | * a log level, message, and array context. |
||
76 | * |
||
77 | * @param LoggerInterface|callable $logger |
||
78 | * |
||
79 | * @throws InvalidArgumentException |
||
80 | */ |
||
81 | 12 | public function setLogger($logger) |
|
82 | { |
||
83 | 12 | if ($logger instanceof LoggerInterface || is_callable($logger)) { |
|
84 | 11 | $this->logger = $logger; |
|
|
|||
85 | } else { |
||
86 | 1 | throw new InvalidArgumentException( |
|
87 | 1 | "Logger has to be a Psr\Log\LoggerInterface or callable" |
|
88 | ); |
||
89 | } |
||
90 | 11 | } |
|
91 | |||
92 | /** |
||
93 | * Sets the formatter, which can be a MessageFormatter or callable that |
||
94 | * accepts a request, response, and a reason if an error has occurred. |
||
95 | * |
||
96 | * @param MessageFormatter|callable $formatter |
||
97 | * |
||
98 | * @throws InvalidArgumentException |
||
99 | */ |
||
100 | 11 | public function setFormatter($formatter) |
|
101 | { |
||
102 | 11 | if ($formatter instanceof MessageFormatter || is_callable($formatter)) { |
|
103 | 11 | $this->formatter = $formatter; |
|
104 | } else { |
||
105 | 1 | throw new InvalidArgumentException( |
|
106 | 1 | "Formatter has to be a \GuzzleHttp\MessageFormatter or callable" |
|
107 | ); |
||
108 | } |
||
109 | 11 | } |
|
110 | |||
111 | /** |
||
112 | * Sets the log level to use, which can be either a string or a callable |
||
113 | * that accepts a response (which could be null). A log level could also |
||
114 | * be null, which indicates that the default log level should be used. |
||
115 | * |
||
116 | * @param string|callable|null |
||
117 | */ |
||
118 | 2 | public function setLogLevel($logLevel) |
|
122 | |||
123 | /** |
||
124 | * Logs a request and/or a response. |
||
125 | * |
||
126 | * @param RequestInterface $request |
||
127 | * @param ResponseInterface|null $response |
||
128 | * @param mixed $reason |
||
129 | */ |
||
130 | protected function log( |
||
154 | |||
155 | /** |
||
156 | * Formats a request and response as a log message. |
||
157 | * |
||
158 | * @param RequestInterface $request |
||
159 | * @param ResponseInterface|null $response |
||
160 | * @param mixed $reason |
||
161 | * |
||
162 | * @return string The formatted message. |
||
163 | */ |
||
164 | protected function getLogMessage( |
||
179 | |||
180 | /** |
||
181 | * Returns a log level for a given response. |
||
182 | * |
||
183 | * @param ResponseInterface $response The response being logged. |
||
184 | * |
||
185 | * @return string LogLevel |
||
186 | */ |
||
187 | protected function getLogLevel(ResponseInterface $response = null) |
||
199 | |||
200 | /** |
||
201 | * Returns the default log level for a response. |
||
202 | * |
||
203 | * @param ResponseInterface $response |
||
204 | * |
||
205 | * @return string LogLevel |
||
206 | */ |
||
207 | protected function getDefaultLogLevel(ResponseInterface $response = null) { |
||
214 | |||
215 | /** |
||
216 | * Returns a function which is handled when a request was successful. |
||
217 | * |
||
218 | * @param RequestInterface $request |
||
219 | * |
||
220 | * @return Closure |
||
221 | */ |
||
222 | protected function onSuccess(RequestInterface $request) |
||
229 | |||
230 | /** |
||
231 | * Returns a function which is handled when a request was rejected. |
||
232 | * |
||
233 | * @param RequestInterface $request |
||
234 | * |
||
235 | * @return Closure |
||
236 | */ |
||
237 | protected function onFailure(RequestInterface $request) |
||
249 | |||
250 | /** |
||
251 | * Called when the middleware is handled by the client. |
||
252 | * |
||
253 | * @param callable $handler |
||
254 | * |
||
255 | * @return Closure |
||
256 | */ |
||
257 | public function __invoke(callable $handler) |
||
272 | } |
||
273 |
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.