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