These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Rj\FrontendBundle\Command\Options; |
||
4 | |||
5 | use Symfony\Component\Console\Command\Command; |
||
6 | use Symfony\Component\Console\Input\InputInterface; |
||
7 | use Symfony\Component\Console\Output\OutputInterface; |
||
8 | |||
9 | abstract class BaseOptionHelper |
||
10 | { |
||
11 | protected $defaultValue = 0; |
||
12 | protected $allowedValues = array(); |
||
13 | protected $errorMessage = null; |
||
14 | |||
15 | private $command; |
||
16 | private $input; |
||
17 | private $output; |
||
18 | |||
19 | abstract protected function getQuestion($question); |
||
20 | |||
21 | 4 | public function __construct(Command $command, InputInterface $input, OutputInterface $output) |
|
22 | { |
||
23 | 4 | $this->command = $command; |
|
24 | 4 | $this->input = $input; |
|
25 | 4 | $this->output = $output; |
|
26 | 4 | } |
|
27 | |||
28 | 4 | public function setAllowedValues($allowedValues) |
|
29 | { |
||
30 | 4 | $this->allowedValues = $allowedValues; |
|
31 | |||
32 | 4 | return $this; |
|
33 | } |
||
34 | |||
35 | 4 | public function setErrorMessage($errorMessage) |
|
36 | { |
||
37 | 4 | $this->errorMessage = $errorMessage; |
|
38 | |||
39 | 4 | return $this; |
|
40 | } |
||
41 | |||
42 | 4 | public function setDefaultValue($defaultValue) |
|
43 | { |
||
44 | 4 | $this->defaultValue = $defaultValue; |
|
45 | |||
46 | 4 | return $this; |
|
47 | } |
||
48 | |||
49 | 4 | public function setOption($name, $question) |
|
50 | { |
||
51 | 4 | $selection = $this->input->getOption($name); |
|
52 | 4 | $allowed = $this->allowedValues; |
|
53 | 4 | $error = $this->errorMessage; |
|
54 | |||
55 | 4 | if (null !== $selection && !empty($allowed) && !in_array($selection, $allowed)) { |
|
56 | 4 | $this->output->writeln(sprintf("<error>$error</error>", $selection)); |
|
1 ignored issue
–
show
|
|||
57 | 4 | $selection = null; |
|
58 | } |
||
59 | |||
60 | 4 | if (null === $selection) { |
|
61 | 4 | $selection = $this->ask($this->getQuestion($question)); |
|
62 | } |
||
63 | |||
64 | 4 | if ($selection === 'false') { |
|
65 | 4 | $selection = false; |
|
66 | } |
||
67 | |||
68 | 4 | if ($selection === 'true') { |
|
69 | $selection = true; |
||
70 | } |
||
71 | |||
72 | 4 | $this->input->setOption($name, $selection); |
|
73 | |||
74 | 4 | return $selection; |
|
75 | } |
||
76 | |||
77 | 4 | private function ask($question) |
|
78 | { |
||
79 | 4 | return $this->command->getHelper('question')->ask($this->input, $this->output, $question); |
|
80 | } |
||
81 | } |
||
82 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.