1 | <?php |
||
14 | class ParameterCommand extends Command |
||
15 | { |
||
16 | /** |
||
17 | * The structure |
||
18 | * |
||
19 | * @var Structure |
||
20 | **/ |
||
21 | private $structure; |
||
22 | |||
23 | /** |
||
24 | * the receive params |
||
25 | * |
||
26 | * @var array |
||
27 | **/ |
||
28 | private $received = array(); |
||
29 | |||
30 | /** |
||
31 | * Configure the command |
||
32 | * |
||
33 | * @return void |
||
34 | **/ |
||
35 | protected function configure() |
||
39 | |||
40 | /** |
||
41 | * Get the structure |
||
42 | * |
||
43 | * @return Structure |
||
44 | */ |
||
45 | public function getStructure() |
||
49 | |||
50 | /** |
||
51 | * The structure |
||
52 | * |
||
53 | * @param Structure $structure |
||
54 | * @return ParameterCommand |
||
55 | */ |
||
56 | public function setStructure(Structure $structure) |
||
62 | |||
63 | /** |
||
64 | * Get the received parameters |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | public function getReceived() |
||
72 | |||
73 | /** |
||
74 | * Set the received parameters |
||
75 | * |
||
76 | * @param array $received |
||
77 | * @return ParameterCommand |
||
78 | */ |
||
79 | public function setReceived(array $received) |
||
85 | |||
86 | /** |
||
87 | * Execute the command |
||
88 | * |
||
89 | * @param InputInterface $input |
||
90 | * @param OutputInterface $output |
||
91 | * @return void |
||
92 | **/ |
||
93 | protected function execute(InputInterface $input, OutputInterface $output) |
||
103 | } |
||
104 |
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: