Conditions | 19 |
Paths | 82 |
Total Lines | 65 |
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 |
||
62 | public function ask(): string |
||
63 | { |
||
64 | $text = $this->question; |
||
65 | if ($this->helpText) { |
||
66 | $text .= ' (? for help)'; |
||
67 | } |
||
68 | if ($this->default) { |
||
69 | if (!$this->yesNoQuestion) { |
||
70 | $text .= ' [' . $this->default . ']'; |
||
71 | } elseif ($this->default === 'y') { |
||
72 | $text .= ' [Y/n]'; |
||
73 | } elseif ($this->default === 'n') { |
||
74 | $text .= ' [y/N]'; |
||
75 | } else { |
||
76 | throw new \InvalidArgumentException('Default value must be "y" or "n".'); |
||
77 | } |
||
78 | } elseif ($this->yesNoQuestion) { |
||
79 | $text .= ' [y/n]'; |
||
80 | } |
||
81 | $text .= ': '; |
||
82 | |||
83 | $question = new \Symfony\Component\Console\Question\Question($text, $this->default); |
||
84 | |||
85 | $validator = $this->validator; |
||
86 | |||
87 | if ($this->yesNoQuestion) { |
||
88 | $validator = function (?string $response) use ($validator) { |
||
89 | $response = $response ?? ''; |
||
90 | $response = \strtolower(trim($response)); |
||
91 | if (!\in_array($response, ['y', 'n', 'yes', 'no'])) { |
||
92 | throw new \InvalidArgumentException('Answer must be "y" or "n"'); |
||
93 | } |
||
94 | $response = \in_array($response, ['y', 'yes']) ? '1' : ''; |
||
95 | return $validator ? $validator($response) : $response; |
||
96 | }; |
||
97 | } |
||
98 | |||
99 | if ($this->helpText !== null) { |
||
100 | $validator = function (?string $response) use ($validator) { |
||
101 | $response = $response ?? ''; |
||
102 | if (trim($response) === '?') { |
||
103 | $this->output->writeln($this->helpText ?: ''); |
||
104 | return '?'; |
||
105 | } |
||
106 | return $validator ? $validator($response) : $response; |
||
107 | }; |
||
108 | } |
||
109 | |||
110 | if ($this->compulsory) { |
||
111 | $validator = function (?string $response) use ($validator) { |
||
112 | $response = $response ?? ''; |
||
113 | if (trim($response) === '') { |
||
114 | throw new \InvalidArgumentException('This field is compulsory.'); |
||
115 | } |
||
116 | return $validator ? $validator($response) : $response; |
||
117 | }; |
||
118 | } |
||
119 | |||
120 | $question->setValidator($validator); |
||
121 | |||
122 | do { |
||
123 | $answer = $this->helper->ask($this->input, $this->output, $question); |
||
124 | } while ($this->helpText !== null && $answer === '?'); |
||
125 | |||
126 | return $answer; |
||
|
|||
127 | } |
||
129 |