1 | <?php |
||
12 | class ApiClient |
||
13 | { |
||
14 | /** @var \GuzzleHttp\Client */ |
||
15 | protected $client; |
||
16 | |||
17 | public function __construct() |
||
29 | |||
30 | /** |
||
31 | * @param string $url |
||
32 | * @return array|null |
||
33 | * @throws \Fenerum\API\Exceptions\FenerumApiException |
||
34 | */ |
||
35 | public function get(string $url): ?array |
||
39 | |||
40 | /** |
||
41 | * @param string $url |
||
42 | * @param array $data |
||
43 | * @return array|null |
||
44 | * @throws \Fenerum\API\Exceptions\FenerumApiException |
||
45 | */ |
||
46 | public function post(string $url, array $data): ?array |
||
50 | |||
51 | /** |
||
52 | * @param string $url |
||
53 | * @param array $data |
||
54 | * @return array |
||
55 | * @throws \Fenerum\API\Exceptions\FenerumApiException |
||
56 | */ |
||
57 | public function put(string $url, array $data): ?array |
||
61 | |||
62 | /** |
||
63 | * @param string $url |
||
64 | * @param array $data |
||
65 | * @return array|null |
||
66 | * @throws \Fenerum\API\Exceptions\FenerumApiException |
||
67 | */ |
||
68 | public function patch(string $url, array $data): ?array |
||
72 | |||
73 | /** |
||
74 | * @param string $url |
||
75 | * @return array|null |
||
76 | * @throws \Fenerum\API\Exceptions\FenerumApiException |
||
77 | */ |
||
78 | public function delete(string $url): ?array |
||
82 | |||
83 | /** |
||
84 | * Send Request. |
||
85 | * |
||
86 | * @param string $method |
||
87 | * @param string $uri |
||
88 | * @param array|null $payload |
||
89 | * @param array $headers |
||
90 | * @return array|null |
||
91 | * @throws \Fenerum\API\Exceptions\FenerumApiException |
||
92 | */ |
||
93 | public function request(string $method, string $uri, $payload = null, array $headers = []): ?array |
||
113 | |||
114 | /** |
||
115 | * @param GuzzleException $exception |
||
116 | * @param string $uri |
||
117 | * @throws \Fenerum\API\Exceptions\FenerumApiException |
||
118 | */ |
||
119 | private function handleException(GuzzleException $exception, string $uri): void |
||
134 | |||
135 | /** |
||
136 | * Makes sure that all config objects are set before we can use this. |
||
137 | * @return bool |
||
138 | */ |
||
139 | private function isEnabled(): bool |
||
143 | |||
144 | /** |
||
145 | * @param string $string |
||
146 | * @param array|null $context |
||
147 | */ |
||
148 | private function logger(string $string, ?array $context = []): void |
||
154 | } |
||
155 |
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: