1 | <?php |
||
8 | class RequestBuilder |
||
9 | { |
||
10 | /**@var string */ |
||
11 | private $method; |
||
12 | /**@var string */ |
||
13 | private $host; |
||
14 | /**@var array */ |
||
15 | private $query; |
||
16 | /**@var array */ |
||
17 | private $headers; |
||
18 | /**@var bool */ |
||
19 | private $secure; |
||
20 | /**@var IRequestBroker */ |
||
21 | private $requester; |
||
22 | |||
23 | 9 | public function __construct(string $method, string $host) |
|
32 | |||
33 | /** |
||
34 | * @param IRequestBroker $requester |
||
35 | * @return RequestBuilder |
||
36 | */ |
||
37 | 4 | public function setRequester(IRequestBroker $requester): RequestBuilder |
|
42 | |||
43 | /** |
||
44 | * @param string $host |
||
45 | * @return RequestBuilder |
||
46 | * @SuppressWarnings(PHPMD) |
||
47 | */ |
||
48 | 9 | public static function get(string $host): RequestBuilder |
|
52 | |||
53 | /** |
||
54 | * @param string $host |
||
55 | * @return RequestBuilder |
||
56 | * @SuppressWarnings(PHPMD) |
||
57 | */ |
||
58 | 1 | public static function post(string $host): RequestBuilder |
|
62 | |||
63 | 2 | public static function multi(array $requests, $broker = null): RequestBuilder |
|
77 | |||
78 | /** |
||
79 | * @return string |
||
80 | */ |
||
81 | 1 | public function getMethod(): string |
|
85 | |||
86 | /** |
||
87 | * @return string |
||
88 | */ |
||
89 | 1 | public function getHost(): string |
|
93 | |||
94 | /** |
||
95 | * @return array |
||
96 | */ |
||
97 | 1 | public function getHeaders(): array |
|
101 | |||
102 | 1 | public function secure(): RequestBuilder |
|
107 | |||
108 | 1 | public function unSecure(): RequestBuilder |
|
113 | |||
114 | 2 | public function setHeader(string $name, string $value): RequestBuilder |
|
119 | |||
120 | 1 | public function setQuery(string $name, ?string $value = null): RequestBuilder |
|
125 | |||
126 | 4 | private function preSend() |
|
138 | |||
139 | 4 | public function send() |
|
150 | |||
151 | 7 | public function buildURL(): string |
|
165 | |||
166 | 2 | public function getAsJSON() |
|
174 | } |
||
175 |
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: