1 | <?php |
||
9 | class CreateFlightCommandHandler implements CommandHandlerInterface |
||
10 | { |
||
11 | /** |
||
12 | * @var DoliDB |
||
13 | */ |
||
14 | private $db; |
||
15 | |||
16 | /** |
||
17 | * @var stdClass |
||
18 | */ |
||
19 | private $conf; |
||
20 | |||
21 | /** |
||
22 | * @var User |
||
23 | */ |
||
24 | private $user; |
||
25 | |||
26 | /** |
||
27 | * @var Translate |
||
28 | */ |
||
29 | private $langs; |
||
30 | |||
31 | /** |
||
32 | * @var |
||
33 | */ |
||
34 | private $validator; |
||
35 | |||
36 | /** |
||
37 | * @param DoliDB $db |
||
38 | * @param stdClass $conf |
||
39 | * @param User $user |
||
40 | * @param Translate $langs |
||
41 | * @param FlightValidator $validator |
||
42 | */ |
||
43 | public function __construct( |
||
56 | |||
57 | /** |
||
58 | * @param CreateFlightCommand|CommandInterface $command |
||
59 | * |
||
60 | * @return Bbcvols |
||
61 | * @throws Exception |
||
62 | */ |
||
63 | public function handle(CommandInterface $command) |
||
98 | |||
99 | /** |
||
100 | * Handles the order of a flight. |
||
101 | * |
||
102 | * @param Bbcvols $flight |
||
103 | * |
||
104 | * @throws Exception |
||
105 | */ |
||
106 | private function handleOrder($flight) |
||
135 | |||
136 | /** |
||
137 | * @param Bbcvols $flight |
||
138 | * |
||
139 | * @return Commande |
||
140 | * @throws Exception |
||
141 | */ |
||
142 | private function getOrderFromFlight($flight) |
||
147 | |||
148 | /** |
||
149 | * @param int $orderId |
||
150 | * |
||
151 | * @return int |
||
152 | */ |
||
153 | private function numberOfPassengersLinkedToOrder($orderId) |
||
172 | } |
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: