Conditions | 19 |
Paths | 1 |
Total Lines | 72 |
Code Lines | 44 |
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 |
||
84 | private function getDefaultValidator(): callable |
||
85 | { |
||
86 | $choices = $this->choices; |
||
87 | $errorMessage = 'Value "%s" is invalid'; |
||
88 | $multiselect = $this->multiselect; |
||
89 | $isAssoc = (bool)count(array_filter(array_keys($this->choices), '\is_string')); |
||
90 | |||
91 | return function ($selected) use ($choices, $errorMessage, $multiselect, $isAssoc) { |
||
92 | |||
93 | // Collapse all spaces. |
||
94 | $selectedChoices = str_replace(' ', '', $selected); |
||
95 | |||
96 | if ($this->helpText !== null && $selectedChoices === '?') { |
||
97 | $this->output->writeln($this->helpText ?: ''); |
||
98 | return '?'; |
||
99 | } |
||
100 | |||
101 | if ($multiselect) { |
||
102 | // Check for a separated comma values |
||
103 | if (!preg_match('/^[^,]+(?:,[^,]+)*$/', $selectedChoices, $matches)) { |
||
104 | throw new InvalidArgumentException(sprintf($errorMessage, $selected)); |
||
105 | } |
||
106 | $selectedChoices = explode(',', $selectedChoices); |
||
107 | } else { |
||
108 | $selectedChoices = array($selected); |
||
109 | } |
||
110 | |||
111 | $multiselectChoices = array(); |
||
112 | foreach ($selectedChoices as $value) { |
||
113 | $results = array(); |
||
114 | foreach ($choices as $key => $choice) { |
||
115 | if ($choice === $value) { |
||
116 | $results[] = $key; |
||
117 | } |
||
118 | } |
||
119 | |||
120 | if (count($results) > 1) { |
||
121 | throw new InvalidArgumentException(sprintf('The provided answer is ambiguous. Value should be one of %s.', implode(' or ', $results))); |
||
122 | } |
||
123 | |||
124 | $result = array_search($value, $choices, true); |
||
125 | |||
126 | if (!$isAssoc) { |
||
127 | if (false !== $result) { |
||
128 | $result = $choices[$result]; |
||
129 | } elseif (isset($choices[$value])) { |
||
130 | $result = $choices[$value]; |
||
131 | } |
||
132 | } elseif (false === $result && isset($choices[$value])) { |
||
133 | $result = $value; |
||
134 | } |
||
135 | |||
136 | if (false === $result) { |
||
137 | throw new InvalidArgumentException(sprintf($errorMessage, $value)); |
||
138 | } |
||
139 | $multiselectChoices[] = (string)$result; |
||
140 | } |
||
141 | if ($multiselect) { |
||
142 | if ($this->printAnswer) { |
||
143 | $this->output->writeln('<info>You selected: ' . \implode(", ", $multiselectChoices) . '</info>'); |
||
144 | $this->spacer(); |
||
145 | } |
||
146 | return $multiselectChoices; |
||
147 | } |
||
148 | |||
149 | $answer = current($multiselectChoices); |
||
150 | if ($this->printAnswer) { |
||
151 | $this->output->writeln("<info>You selected: $answer</info>"); |
||
152 | $this->spacer(); |
||
153 | } |
||
154 | |||
155 | return $answer; |
||
156 | }; |
||
159 |