| Conditions | 17 |
| Paths | 80 |
| Total Lines | 60 |
| 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 |
||
| 89 | public function ask() |
||
| 90 | { |
||
| 91 | $text = $this->question; |
||
| 92 | if ($this->helpText) { |
||
| 93 | $text .= ' (? for help)'; |
||
| 94 | } |
||
| 95 | if ($this->default) { |
||
| 96 | if (!$this->yesNoQuestion) { |
||
| 97 | $text .= ' ['.$this->default.']'; |
||
| 98 | } elseif ($this->default === 'y') { |
||
| 99 | $text .= ' [Y/n]'; |
||
| 100 | } elseif ($this->default === 'n') { |
||
| 101 | $text .= ' [y/N]'; |
||
| 102 | } else { |
||
| 103 | $text .= ' [y/n]'; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | $text .= ': '; |
||
| 107 | |||
| 108 | $question = new \Symfony\Component\Console\Question\Question($text, $this->default); |
||
| 109 | |||
| 110 | $validator = $this->validator; |
||
| 111 | |||
| 112 | if ($this->yesNoQuestion) { |
||
| 113 | $validator = function(?string $response) use ($validator) { |
||
| 114 | $response = trim(\strtolower($response)); |
||
| 115 | if (!\in_array($response, ['y', 'n', 'yes', 'no'])) { |
||
| 116 | throw new \InvalidArgumentException('Answer must be "y" or "n"'); |
||
| 117 | } |
||
| 118 | $response = \in_array($response, ['y', 'yes']) ? '1' : ''; |
||
| 119 | return $validator ? $validator($response) : $response; |
||
| 120 | }; |
||
| 121 | } |
||
| 122 | |||
| 123 | if ($this->helpText) { |
||
| 124 | $validator = function(?string $response) use ($validator) { |
||
| 125 | if (trim($response) === '?') { |
||
| 126 | $this->output->writeln($this->helpText); |
||
| 127 | return '?'; |
||
| 128 | } |
||
| 129 | return $validator ? $validator($response) : $response; |
||
| 130 | }; |
||
| 131 | } |
||
| 132 | |||
| 133 | if ($this->compulsory) { |
||
| 134 | $validator = function(?string $response) use ($validator) { |
||
| 135 | if (trim($response) === '') { |
||
| 136 | throw new \InvalidArgumentException('This field is compulsory.'); |
||
| 137 | } |
||
| 138 | return $validator ? $validator($response) : $response; |
||
| 139 | }; |
||
| 140 | } |
||
| 141 | |||
| 142 | $question->setValidator($validator); |
||
| 143 | |||
| 144 | do { |
||
| 145 | $answer = $this->helper->ask($this->input, $this->output, $question); |
||
| 146 | } while ($this->helpText !== null && $answer === '?'); |
||
| 147 | |||
| 148 | return $answer; |
||
| 149 | } |
||
| 151 |