| Conditions | 12 |
| Paths | 32 |
| Total Lines | 99 |
| Lines | 32 |
| Ratio | 32.32 % |
| 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 |
||
| 198 | protected function interact(InputInterface $input, OutputInterface $output) |
||
| 199 | { |
||
| 200 | View Code Duplication | if (!$input->getArgument('username')) { |
|
| 201 | $question = New Question('Please choose a username:'); |
||
| 202 | $question->setValidator(function ($username) { |
||
| 203 | if (null === $username) { |
||
| 204 | throw new \InvalidArgumentException('Username can not be empty'); |
||
| 205 | } |
||
| 206 | |||
| 207 | return $username; |
||
| 208 | }); |
||
| 209 | $username = $this->getHelper('question')->ask( |
||
| 210 | $input, |
||
| 211 | $output, |
||
| 212 | $question |
||
| 213 | ); |
||
| 214 | $input->setArgument('username', $username); |
||
| 215 | } |
||
| 216 | |||
| 217 | View Code Duplication | if (!$input->getArgument('email')) { |
|
| 218 | $question = New Question('Please choose an email:'); |
||
| 219 | $question->setValidator(function ($email) { |
||
| 220 | if (null === $email) { |
||
| 221 | throw new \InvalidArgumentException('Email can not be empty'); |
||
| 222 | } |
||
| 223 | |||
| 224 | return $email; |
||
| 225 | }); |
||
| 226 | $email = $this->getHelper('question')->ask( |
||
| 227 | $input, |
||
| 228 | $output, |
||
| 229 | $question |
||
| 230 | ); |
||
| 231 | $input->setArgument('email', $email); |
||
| 232 | } |
||
| 233 | |||
| 234 | if (!$input->getArgument('password')) { |
||
| 235 | |||
| 236 | $question = New Question('Please choose a password:'); |
||
| 237 | $question->setHidden(true); |
||
| 238 | $question->setHiddenFallback(false); |
||
| 239 | $question->setValidator(function ($password) { |
||
| 240 | if (null === $password) { |
||
| 241 | throw new \InvalidArgumentException('Password can not be empty'); |
||
| 242 | } |
||
| 243 | |||
| 244 | return $password; |
||
| 245 | }); |
||
| 246 | $password = $this->getHelper('question')->ask( |
||
| 247 | $input, |
||
| 248 | $output, |
||
| 249 | $question |
||
| 250 | ); |
||
| 251 | |||
| 252 | $input->setArgument('password', $password); |
||
| 253 | } |
||
| 254 | |||
| 255 | if (!$input->getArgument('locale')) { |
||
| 256 | $locale = $this->getHelper('question')->ask( |
||
| 257 | $input, |
||
| 258 | $output, |
||
| 259 | new Question('Please enter the locale (or leave empty for default admin locale):') |
||
| 260 | ); |
||
| 261 | $input->setArgument('locale', $locale); |
||
| 262 | } |
||
| 263 | |||
| 264 | if (!$input->getOption('group')) { |
||
| 265 | $question = new ChoiceQuestion( |
||
| 266 | 'Please enter the group(s) the user should be a member of (multiple possible, separated by comma):', |
||
| 267 | $this->groups, |
||
| 268 | '' |
||
| 269 | ); |
||
| 270 | $question->setMultiselect(true); |
||
| 271 | $question->setValidator(function ($groupsInput) { |
||
| 272 | |||
| 273 | if (!$this->groups) { |
||
| 274 | throw new \RuntimeException('No user group(s) could be found'); |
||
| 275 | } |
||
| 276 | |||
| 277 | // Validate that the chosen group options exist in the available groups |
||
| 278 | $groupNames = array_unique(explode(',', $groupsInput)); |
||
| 279 | if (count(array_intersect_key(array_flip($groupNames),$this->groups)) !== count($groupNames)) { |
||
| 280 | throw new InvalidArgumentException('You have chosen non existing group(s)'); |
||
| 281 | } |
||
| 282 | |||
| 283 | if ($groupsInput === '') { |
||
| 284 | throw new \RuntimeException( |
||
| 285 | 'Group(s) must be of type integer and can not be empty' |
||
| 286 | ); |
||
| 287 | } |
||
| 288 | return $groupsInput; |
||
| 289 | }); |
||
| 290 | |||
| 291 | // Group has to be imploded because $input->setOption expects a string |
||
| 292 | $groups = $this->getHelper('question')->ask($input, $output, $question); |
||
| 293 | |||
| 294 | $input->setOption('group', $groups); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 311 |
If you suppress an error, we recommend checking for the error condition explicitly: