Conditions | 20 |
Paths | 162 |
Total Lines | 70 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
50 | public function ask(): string |
||
51 | { |
||
52 | $text = $this->question; |
||
53 | if ($this->helpText) { |
||
54 | $text .= ' (? for help)'; |
||
55 | } |
||
56 | if ($this->default) { |
||
57 | if (!$this->yesNoQuestion) { |
||
58 | $text .= ' [' . $this->default . ']'; |
||
59 | } elseif ($this->default === 'y') { |
||
60 | $text .= ' [Y/n]'; |
||
61 | } elseif ($this->default === 'n') { |
||
62 | $text .= ' [y/N]'; |
||
63 | } else { |
||
64 | throw new \InvalidArgumentException('Default value must be "y" or "n".'); |
||
65 | } |
||
66 | } elseif ($this->yesNoQuestion) { |
||
67 | $text .= ' [y/n]'; |
||
68 | } |
||
69 | $text .= ': '; |
||
70 | |||
71 | $question = new SymfonyQuestion($text, $this->default); |
||
72 | |||
73 | $validator = $this->validator; |
||
74 | |||
75 | if ($this->yesNoQuestion) { |
||
76 | $validator = function (?string $response) use ($validator) { |
||
77 | $response = $response ?? ''; |
||
78 | $response = \strtolower(trim($response)); |
||
79 | if (!\in_array($response, ['y', 'n', 'yes', 'no'])) { |
||
80 | throw new \InvalidArgumentException('Answer must be "y" or "n"'); |
||
81 | } |
||
82 | $response = \in_array($response, ['y', 'yes']) ? '1' : ''; |
||
83 | return $validator ? $validator($response) : $response; |
||
84 | }; |
||
85 | } |
||
86 | |||
87 | if ($this->helpText !== null) { |
||
88 | $validator = function (?string $response) use ($validator) { |
||
89 | $response = $response ?? ''; |
||
90 | if (trim($response) === '?') { |
||
91 | $this->output->writeln($this->helpText ?: ''); |
||
92 | return '?'; |
||
93 | } |
||
94 | return $validator ? $validator($response) : $response; |
||
95 | }; |
||
96 | } |
||
97 | |||
98 | if ($this->compulsory) { |
||
99 | $validator = function (?string $response) use ($validator) { |
||
100 | $response = $response ?? ''; |
||
101 | if (trim($response) === '') { |
||
102 | throw new \InvalidArgumentException('This field is compulsory.'); |
||
103 | } |
||
104 | return $validator ? $validator($response) : $response; |
||
105 | }; |
||
106 | } |
||
107 | |||
108 | $question->setValidator($validator); |
||
109 | |||
110 | do { |
||
111 | $answer = $this->helper->ask($this->input, $this->output, $question); |
||
112 | } while ($this->helpText !== null && $answer === '?'); |
||
113 | |||
114 | if ($this->printAnswer) { |
||
115 | $this->output->writeln("<info>Your answer: $answer</info>"); |
||
116 | $this->spacer(); |
||
117 | } |
||
118 | |||
119 | return $answer; |
||
|
|||
120 | } |
||
122 |