Conditions | 20 |
Paths | 162 |
Total Lines | 66 |
Code Lines | 43 |
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 |
||
101 | public function ask(): string |
||
102 | { |
||
103 | $text = $this->question; |
||
104 | if ($this->helpText) { |
||
105 | $text .= ' (? for help)'; |
||
106 | } |
||
107 | if ($this->default) { |
||
108 | if (!$this->yesNoQuestion) { |
||
109 | $text .= ' [' . $this->default . ']'; |
||
110 | } elseif ($this->default === 'y') { |
||
111 | $text .= ' [Y/n]'; |
||
112 | } elseif ($this->default === 'n') { |
||
113 | $text .= ' [y/N]'; |
||
114 | } else { |
||
115 | throw new \InvalidArgumentException('Default value must be "y" or "n".'); |
||
116 | } |
||
117 | } elseif ($this->yesNoQuestion) { |
||
118 | $text .= ' [y/n]'; |
||
119 | } |
||
120 | $text .= ': '; |
||
121 | |||
122 | |||
123 | $question = $this->choiceQuestion ? new \Symfony\Component\Console\Question\ChoiceQuestion($text, $this->choices, $this->default) : new \Symfony\Component\Console\Question\Question($text, $this->default); |
||
124 | |||
125 | $validator = $this->validator; |
||
126 | |||
127 | if ($this->yesNoQuestion) { |
||
128 | $validator = function (?string $response) use ($validator) { |
||
129 | $response = $response ?? ''; |
||
130 | $response = \strtolower(trim($response)); |
||
131 | if (!\in_array($response, ['y', 'n', 'yes', 'no'])) { |
||
132 | throw new \InvalidArgumentException('Answer must be "y" or "n"'); |
||
133 | } |
||
134 | $response = \in_array($response, ['y', 'yes']) ? '1' : ''; |
||
135 | return $validator ? $validator($response) : $response; |
||
136 | }; |
||
137 | } |
||
138 | |||
139 | if ($this->helpText !== null) { |
||
140 | $validator = function (?string $response) use ($validator) { |
||
141 | $response = $response ?? ''; |
||
142 | if (trim($response) === '?') { |
||
143 | $this->output->writeln($this->helpText ?: ''); |
||
144 | return '?'; |
||
145 | } |
||
146 | return $validator ? $validator($response) : $response; |
||
147 | }; |
||
148 | } |
||
149 | |||
150 | if ($this->compulsory) { |
||
151 | $validator = function (?string $response) use ($validator) { |
||
152 | $response = $response ?? ''; |
||
153 | if (trim($response) === '') { |
||
154 | throw new \InvalidArgumentException('This field is compulsory.'); |
||
155 | } |
||
156 | return $validator ? $validator($response) : $response; |
||
157 | }; |
||
158 | } |
||
159 | |||
160 | $question->setValidator($validator); |
||
161 | |||
162 | do { |
||
163 | $answer = $this->helper->ask($this->input, $this->output, $question); |
||
164 | } while ($this->helpText !== null && $answer === '?'); |
||
165 | |||
166 | return $answer; |
||
|
|||
167 | } |
||
169 |