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 | * @param bool $raw |
||
113 | * @return PromiseInterface |
||
114 | */ |
||
115 | 4 | protected function sendRequest(string $path, string $method = 'GET', bool $raw = false): PromiseInterface |
|
129 | |||
130 | /** |
||
131 | * @param RequestInterface $request |
||
132 | * @return PromiseInterface |
||
133 | */ |
||
134 | 4 | public function requestPsr7(RequestInterface $request): PromiseInterface |
|
148 | |||
149 | /** |
||
150 | * @param string $method |
||
151 | * @param string $path |
||
152 | * @return RequestInterface |
||
153 | */ |
||
154 | 4 | protected function createRequest(string $method, string $path): RequestInterface |
|
160 | |||
161 | /** |
||
162 | * @return array |
||
163 | */ |
||
164 | 4 | public function getHeaders(): array |
|
172 | |||
173 | /** |
||
174 | * @param string $json |
||
175 | * @return PromiseInterface |
||
176 | */ |
||
177 | public function jsonDecode(string $json): PromiseInterface |
||
183 | |||
184 | /** |
||
185 | * @return Hydrator |
||
186 | */ |
||
187 | 1 | public function getHydrator(): Hydrator |
|
191 | |||
192 | /** |
||
193 | * @return LoopInterface |
||
194 | */ |
||
195 | 3 | public function getLoop(): LoopInterface |
|
199 | |||
200 | /** |
||
201 | * @return string |
||
202 | */ |
||
203 | 7 | public function getBaseURL(): string |
|
207 | } |
||
208 |
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: