| Conditions | 13 |
| Paths | 64 |
| Total Lines | 109 |
| Code Lines | 65 |
| Lines | 32 |
| Ratio | 29.36 % |
| 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 |
||
| 139 | protected function interact(InputInterface $input, OutputInterface $output) |
||
| 140 | { |
||
| 141 | View Code Duplication | if (!$input->getArgument('username')) { |
|
| 142 | $question = New Question('Please choose a username:'); |
||
| 143 | $question->setValidator(function ($username) { |
||
| 144 | if (null === $username) { |
||
| 145 | throw new \InvalidArgumentException('Username can not be empty'); |
||
| 146 | } |
||
| 147 | |||
| 148 | return $username; |
||
| 149 | }); |
||
| 150 | $username = $this->getHelper('question')->ask( |
||
| 151 | $input, |
||
| 152 | $output, |
||
| 153 | $question |
||
| 154 | ); |
||
| 155 | $input->setArgument('username', $username); |
||
| 156 | } |
||
| 157 | |||
| 158 | View Code Duplication | if (!$input->getArgument('email')) { |
|
| 159 | $question = New Question('Please choose an email:'); |
||
| 160 | $question->setValidator(function ($email) { |
||
| 161 | if (null === $email) { |
||
| 162 | throw new \InvalidArgumentException('Email can not be empty'); |
||
| 163 | } |
||
| 164 | |||
| 165 | return $email; |
||
| 166 | }); |
||
| 167 | $email = $this->getHelper('question')->ask( |
||
| 168 | $input, |
||
| 169 | $output, |
||
| 170 | $question |
||
| 171 | ); |
||
| 172 | $input->setArgument('email', $email); |
||
| 173 | } |
||
| 174 | |||
| 175 | if (!$input->getArgument('password')) { |
||
| 176 | |||
| 177 | $question = New Question('Please choose a password:'); |
||
| 178 | $question->setHidden(true); |
||
| 179 | $question->setHiddenFallback(false); |
||
| 180 | $question->setValidator(function ($password) { |
||
| 181 | if (null === $password) { |
||
| 182 | throw new \InvalidArgumentException('Password can not be empty'); |
||
| 183 | } |
||
| 184 | |||
| 185 | return $password; |
||
| 186 | }); |
||
| 187 | $password = $this->getHelper('question')->ask( |
||
| 188 | $input, |
||
| 189 | $output, |
||
| 190 | $question |
||
| 191 | ); |
||
| 192 | |||
| 193 | $input->setArgument('password', $password); |
||
| 194 | } |
||
| 195 | |||
| 196 | if (!$input->getArgument('locale')) { |
||
| 197 | $locale = $this->getHelper('question')->ask( |
||
| 198 | $input, |
||
| 199 | $output, |
||
| 200 | new Question('Please enter the locale (or leave empty for default admin locale):') |
||
| 201 | ); |
||
| 202 | $input->setArgument('locale', $locale); |
||
| 203 | } |
||
| 204 | |||
| 205 | $this->groups = $this->getContainer()->get('fos_user.group_manager')->findGroups(); |
||
| 206 | |||
| 207 | // reindexing the array, using the db id as the key |
||
| 208 | $newGroups = []; |
||
| 209 | foreach($this->groups as $group) { |
||
| 210 | $newGroups[$group->getId()] = $group; |
||
| 211 | } |
||
| 212 | |||
| 213 | $this->groups = $newGroups; |
||
| 214 | |||
| 215 | if (!$input->getOption('group')) { |
||
| 216 | $question = new ChoiceQuestion( |
||
| 217 | 'Please enter the group(s) the user should be a member of (multiple possible, separated by comma):', |
||
| 218 | $this->groups, |
||
| 219 | '' |
||
| 220 | ); |
||
| 221 | $question->setMultiselect(true); |
||
| 222 | $question->setValidator(function ($groupsInput) { |
||
| 223 | |||
| 224 | if (!$this->groups) { |
||
| 225 | throw new \RuntimeException('No user group(s) could be found'); |
||
| 226 | } |
||
| 227 | |||
| 228 | // Validate that the chosen group options exist in the available groups |
||
| 229 | $groupNames = array_unique(explode(',', $groupsInput)); |
||
| 230 | if (count(array_intersect_key(array_flip($groupNames),$this->groups)) !== count($groupNames)) { |
||
| 231 | throw new InvalidArgumentException('You have chosen non existing group(s)'); |
||
| 232 | } |
||
| 233 | |||
| 234 | if ($groupsInput === '') { |
||
| 235 | throw new \RuntimeException( |
||
| 236 | 'Group(s) must be of type integer and can not be empty' |
||
| 237 | ); |
||
| 238 | } |
||
| 239 | return $groupsInput; |
||
| 240 | }); |
||
| 241 | |||
| 242 | // Group has to be imploded because $input->setOption expects a string |
||
| 243 | $groups = $this->getHelper('question')->ask($input, $output, $question); |
||
| 244 | |||
| 245 | $input->setOption('group', $groups); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | } |
||
| 249 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.