1 | <?php |
||
10 | abstract class BaseOptionHelper |
||
11 | { |
||
12 | /** |
||
13 | * @var mixed |
||
14 | */ |
||
15 | protected $defaultValue = 0; |
||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $allowedValues = []; |
||
21 | |||
22 | /** |
||
23 | * @var string|null |
||
24 | */ |
||
25 | protected $errorMessage = null; |
||
26 | |||
27 | /** |
||
28 | * @var Command |
||
29 | */ |
||
30 | private $command; |
||
31 | |||
32 | /** |
||
33 | * @var InputInterface |
||
34 | */ |
||
35 | private $input; |
||
36 | |||
37 | /** |
||
38 | * @var OutputInterface |
||
39 | */ |
||
40 | private $output; |
||
41 | |||
42 | /** |
||
43 | * @param string $question |
||
44 | * |
||
45 | * @return Question |
||
46 | */ |
||
47 | abstract protected function getQuestion($question); |
||
48 | |||
49 | /** |
||
50 | * @param Command $command |
||
51 | * @param InputInterface $input |
||
52 | * @param OutputInterface $output |
||
53 | */ |
||
54 | 2 | public function __construct(Command $command, InputInterface $input, OutputInterface $output) |
|
60 | |||
61 | /** |
||
62 | * @param array $allowedValues |
||
63 | * |
||
64 | * @return $this |
||
65 | */ |
||
66 | 2 | public function setAllowedValues(array $allowedValues) |
|
72 | |||
73 | /** |
||
74 | * @param string $errorMessage |
||
75 | * |
||
76 | * @return $this |
||
77 | */ |
||
78 | 2 | public function setErrorMessage($errorMessage) |
|
84 | |||
85 | /** |
||
86 | * @param mixed $defaultValue |
||
87 | * |
||
88 | * @return $this |
||
89 | */ |
||
90 | 2 | public function setDefaultValue($defaultValue) |
|
96 | |||
97 | /** |
||
98 | * @param string $name |
||
99 | * @param string $question |
||
100 | * |
||
101 | * @return string|bool |
||
102 | */ |
||
103 | 2 | public function setOption($name, $question) |
|
130 | |||
131 | /** |
||
132 | * @param Question $question |
||
133 | * |
||
134 | * @return mixed |
||
135 | */ |
||
136 | 2 | private function ask(Question $question) |
|
140 | } |
||
141 |
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: