1 | <?php declare(strict_types=1); |
||
19 | final class Client implements ClientInterface |
||
20 | { |
||
21 | const DEFAULT_OPTIONS = [ |
||
22 | Options::SCHEMA => 'https', |
||
23 | Options::PATH => '/', |
||
24 | Options::HEADERS => [], |
||
25 | ]; |
||
26 | |||
27 | /** |
||
28 | * @var LoopInterface |
||
29 | */ |
||
30 | protected $loop; |
||
31 | |||
32 | /** |
||
33 | * @var Locator |
||
34 | */ |
||
35 | protected $locator; |
||
36 | |||
37 | /** |
||
38 | * @var Browser |
||
39 | */ |
||
40 | protected $browser; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $options = []; |
||
46 | |||
47 | /** |
||
48 | * @var string[] |
||
49 | */ |
||
50 | protected $middleware = []; |
||
51 | |||
52 | /** |
||
53 | * @param LoopInterface $loop |
||
54 | * @param Locator $locator |
||
55 | * @param Browser $buzz |
||
56 | * @param array $options |
||
57 | */ |
||
58 | 17 | public function __construct( |
|
73 | |||
74 | 16 | protected function constructMiddlewares(array $options): MiddlewareRunner |
|
90 | |||
91 | protected function combinedMiddlewares(array $extraMiddlewares): array |
||
105 | |||
106 | /** |
||
107 | * @param RequestInterface $request |
||
108 | * @param array $options |
||
109 | * @return PromiseInterface |
||
110 | */ |
||
111 | 16 | public function request(RequestInterface $request, array $options = []): PromiseInterface |
|
112 | { |
||
113 | 16 | $options = $this->applyRequestOptions($options); |
|
114 | 16 | $request = $this->applyApiSettingsToRequest($request, $options); |
|
115 | 16 | $executioner = $this->constructMiddlewares($options); |
|
116 | |||
117 | return $executioner->pre($request)->then(function ($request) use ($options) { |
||
|
|||
118 | 16 | return resolve($this->browser->send( |
|
119 | 16 | $request |
|
120 | )); |
||
121 | }, function (ResponseInterface $response) { |
||
122 | return resolve($response); |
||
123 | })->then(function (ResponseInterface $response) use ($executioner) { |
||
124 | 8 | return $executioner->post($response); |
|
125 | 16 | })->otherwise(function (Throwable $throwable) use ($executioner) { |
|
126 | 8 | return reject($executioner->error($throwable)); |
|
127 | 16 | }); |
|
128 | } |
||
129 | |||
130 | 16 | protected function applyApiSettingsToRequest(RequestInterface $request, array $options): RequestInterface |
|
152 | |||
153 | 16 | public function applyRequestOptions(array $options): array |
|
164 | } |
||
165 |
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: