1 | <?php |
||
18 | class Client |
||
19 | { |
||
20 | const DEFAULT_OPTIONS = [ |
||
21 | 'schema' => 'https', |
||
22 | 'path' => '/', |
||
23 | 'user_agent' => 'WyriHaximus/php-api-client', |
||
24 | 'headers' => [], |
||
25 | ]; |
||
26 | |||
27 | /** |
||
28 | * @var GuzzleClient |
||
29 | */ |
||
30 | protected $handler; |
||
31 | |||
32 | /** |
||
33 | * @var LoopInterface |
||
34 | */ |
||
35 | protected $loop; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $options = []; |
||
41 | |||
42 | /** |
||
43 | * @var Hydrator |
||
44 | */ |
||
45 | protected $hydrator; |
||
46 | |||
47 | /** |
||
48 | * @var CacheInterface |
||
49 | */ |
||
50 | protected $cache; |
||
51 | |||
52 | /** |
||
53 | * @param LoopInterface $loop |
||
54 | * @param GuzzleClient $handler |
||
55 | * @param array $options |
||
56 | */ |
||
57 | 12 | public function __construct(LoopInterface $loop, GuzzleClient $handler, array $options = []) |
|
67 | |||
68 | /** |
||
69 | * @param string $path |
||
70 | * @param bool $refresh |
||
71 | * @return PromiseInterface |
||
72 | */ |
||
73 | 5 | public function request(string $path, bool $refresh = false): PromiseInterface |
|
79 | |||
80 | /** |
||
81 | * @param string $path |
||
82 | * @param bool $refresh |
||
83 | * @return PromiseInterface |
||
84 | */ |
||
85 | 5 | public function requestRaw(string $path, bool $refresh = false): PromiseInterface |
|
95 | |||
96 | /** |
||
97 | * @param string $path |
||
98 | * @return PromiseInterface |
||
99 | */ |
||
100 | 3 | protected function checkCache(string $path): PromiseInterface |
|
108 | |||
109 | /** |
||
110 | * @param string $path |
||
111 | * @param string $method |
||
112 | * @return PromiseInterface |
||
113 | */ |
||
114 | 4 | protected function sendRequest(string $path, string $method = 'GET', bool $raw = false): PromiseInterface |
|
128 | |||
129 | /** |
||
130 | * @param RequestInterface $request |
||
131 | * @return PromiseInterface |
||
132 | */ |
||
133 | 4 | public function requestPsr7(RequestInterface $request): PromiseInterface |
|
147 | |||
148 | /** |
||
149 | * @param string $method |
||
150 | * @param string $path |
||
151 | * @return RequestInterface |
||
152 | */ |
||
153 | 4 | protected function createRequest(string $method, string $path): RequestInterface |
|
159 | |||
160 | /** |
||
161 | * @return array |
||
162 | */ |
||
163 | 4 | public function getHeaders(): array |
|
171 | |||
172 | /** |
||
173 | * @param string $json |
||
174 | * @return PromiseInterface |
||
175 | */ |
||
176 | public function jsonDecode(string $json): PromiseInterface |
||
182 | |||
183 | /** |
||
184 | * @return Hydrator |
||
185 | */ |
||
186 | 1 | public function getHydrator(): Hydrator |
|
190 | |||
191 | /** |
||
192 | * @return LoopInterface |
||
193 | */ |
||
194 | 3 | public function getLoop(): LoopInterface |
|
198 | |||
199 | /** |
||
200 | * @return string |
||
201 | */ |
||
202 | 7 | public function getBaseURL(): string |
|
206 | } |
||
207 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: