1 | <?php |
||
10 | abstract class Command extends SymfonyCommand |
||
11 | { |
||
12 | /** |
||
13 | * Symfony console output. |
||
14 | * |
||
15 | * @var SymfonyOutput |
||
16 | */ |
||
17 | protected $output; |
||
18 | |||
19 | /** |
||
20 | * Symfony input implementation. |
||
21 | * |
||
22 | * @var InputInterface |
||
23 | */ |
||
24 | protected $input; |
||
25 | |||
26 | /** |
||
27 | * Execute the command. |
||
28 | * |
||
29 | * @param InputInterface $input |
||
30 | * @param OutputInterface $output |
||
31 | */ |
||
32 | protected function execute(InputInterface $input, OutputInterface $output) |
||
42 | |||
43 | /** |
||
44 | * Return the output implementation. |
||
45 | * |
||
46 | * @return SymfonyOutput |
||
47 | */ |
||
48 | protected function getOutput() |
||
52 | |||
53 | /** |
||
54 | * Get the value of a command argument. |
||
55 | * |
||
56 | * @param string $key |
||
57 | * |
||
58 | * @return string|array |
||
59 | */ |
||
60 | protected function argument($key = null) |
||
68 | |||
69 | /** |
||
70 | * Determine if the given argument is present. |
||
71 | * |
||
72 | * @param string|int $name |
||
73 | * |
||
74 | * @return bool |
||
75 | */ |
||
76 | protected function hasArgument($name) |
||
80 | |||
81 | /** |
||
82 | * Get the value of a command option. |
||
83 | * |
||
84 | * @param string $key |
||
85 | * |
||
86 | * @return string|array |
||
87 | */ |
||
88 | protected function option($key = null) |
||
96 | |||
97 | /** |
||
98 | * Determine if the given option is present. |
||
99 | * |
||
100 | * @param string $name |
||
101 | * |
||
102 | * @return bool |
||
103 | */ |
||
104 | protected function hasOption($name) |
||
108 | } |
||
109 |
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 sub-classes 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 parent class: