1 | <?php declare(strict_types=1); |
||
16 | class FetchAndHydrateService implements ServiceInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var RequestService |
||
20 | */ |
||
21 | private $requestService; |
||
22 | |||
23 | /** |
||
24 | * @var Hydrator |
||
25 | */ |
||
26 | private $hydrator; |
||
27 | |||
28 | /** |
||
29 | * @param RequestService $requestService |
||
30 | * @param Hydrator $hydrator |
||
31 | */ |
||
32 | public function __construct(RequestService $requestService, Hydrator $hydrator) |
||
37 | |||
38 | /** |
||
39 | * @param string|null $path |
||
40 | * @param string|null $index |
||
41 | * @param string|null $hydrateClass |
||
42 | * @return CancellablePromiseInterface |
||
43 | */ |
||
44 | public function handle( |
||
58 | } |
||
59 |
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: