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 |
|
136 | |||
137 | /** |
||
138 | * @param string $method |
||
139 | * @param string $path |
||
140 | * @return RequestInterface |
||
141 | */ |
||
142 | 4 | protected function createRequest(string $method, string $path): RequestInterface |
|
151 | |||
152 | /** |
||
153 | * @param string $json |
||
154 | * @return PromiseInterface |
||
155 | */ |
||
156 | public function jsonDecode(string $json): PromiseInterface |
||
162 | |||
163 | /** |
||
164 | * @return Hydrator |
||
165 | */ |
||
166 | 1 | public function getHydrator(): Hydrator |
|
170 | |||
171 | /** |
||
172 | * @return LoopInterface |
||
173 | */ |
||
174 | 3 | public function getLoop(): LoopInterface |
|
178 | |||
179 | /** |
||
180 | * @return string |
||
181 | */ |
||
182 | 7 | public function getBaseURL(): string |
|
186 | } |
||
187 |
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: