These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * See class comment |
||
4 | * |
||
5 | * PHP Version 5 |
||
6 | * |
||
7 | * @category Netresearch |
||
8 | * @package Netresearch\Kite |
||
9 | * @subpackage Task |
||
10 | * @author Christian Opitz <[email protected]> |
||
11 | * @license http://www.netresearch.de Netresearch Copyright |
||
12 | * @link http://www.netresearch.de |
||
13 | */ |
||
14 | |||
15 | namespace Netresearch\Kite\Task; |
||
16 | use Netresearch\Kite\Task; |
||
17 | use Symfony\Component\Console\Question\Question; |
||
18 | |||
19 | /** |
||
20 | * Ask a question and return the answer |
||
21 | * |
||
22 | * @category Netresearch |
||
23 | * @package Netresearch\Kite |
||
24 | * @subpackage Task |
||
25 | * @author Christian Opitz <[email protected]> |
||
26 | * @license http://www.netresearch.de Netresearch Copyright |
||
27 | * @link http://www.netresearch.de |
||
28 | */ |
||
29 | class AnswerTask extends Task |
||
30 | { |
||
31 | /** |
||
32 | * Configure the variables |
||
33 | * |
||
34 | * @return array |
||
35 | */ |
||
36 | View Code Duplication | protected function configureVariables() |
|
37 | { |
||
38 | return array( |
||
39 | 'question' => array( |
||
40 | 'type' => 'string', |
||
41 | 'required' => true, |
||
42 | 'label' => 'The question to ask' |
||
43 | ), |
||
44 | 'default' => array( |
||
45 | 'type' => 'string|numeric', |
||
46 | 'label' => 'Default value (shown to the user as well)' |
||
47 | ), |
||
48 | '--' |
||
49 | ) + parent::configureVariables(); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Format a question |
||
54 | * |
||
55 | * @param string $question The question |
||
56 | * @param mixed $default Default value |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | protected function formatQuestion($question, $default = null) |
||
61 | { |
||
62 | return '<question>' . $question . '</question> ' |
||
63 | . ($default !== null && $default !== '' ? "[{$default}] " : ''); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Create a question |
||
68 | * |
||
69 | * @param string $question The question |
||
70 | * @param mixed $default Default value |
||
71 | * |
||
72 | * @return Question |
||
73 | */ |
||
74 | protected function createQuestion($question, $default) |
||
75 | { |
||
76 | return new Question($this->formatQuestion($question, $default), $default); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Execute the task |
||
81 | * |
||
82 | * @return mixed |
||
83 | */ |
||
84 | public function execute() |
||
85 | { |
||
86 | $answer = $this->console->getHelper('question')->ask( |
||
0 ignored issues
–
show
|
|||
87 | $this->console->getInput(), |
||
88 | $this->console->getOutput(), |
||
89 | $this->createQuestion($this->get('question'), $this->get('default', null)) |
||
90 | ); |
||
91 | |||
92 | return $answer; |
||
93 | } |
||
94 | } |
||
95 | ?> |
||
96 |
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: