| Conditions | 8 |
| Paths | 24 |
| Total Lines | 56 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 97 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 98 | { |
||
| 99 | $io = new SymfonyStyle($input, $output); |
||
| 100 | $id = $input->getArgument('id'); |
||
| 101 | if (is_numeric($id)) { |
||
| 102 | $criteria = ['uid' => (int) $id]; |
||
| 103 | } elseif (filter_var($id, FILTER_VALIDATE_EMAIL)) { |
||
| 104 | $criteria = ['email' => $id]; |
||
| 105 | } else { |
||
| 106 | $criteria = ['uname' => $id]; |
||
| 107 | } |
||
| 108 | try { |
||
| 109 | $mapping = $this->authenticationMappingRepository->findOneBy($criteria); |
||
| 110 | } catch (\Exception $e) { |
||
| 111 | $io->error($this->translator->trans('Found more than one user by that criteria. Try a different criteria (uid works best).')); |
||
| 112 | |||
| 113 | return 1; |
||
| 114 | } |
||
| 115 | if (empty($mapping)) { |
||
| 116 | $io->error($this->translator->trans('Found zero users by that criteria. Try a different criteria (uid works best).')); |
||
| 117 | |||
| 118 | return 1; |
||
| 119 | } |
||
| 120 | $choices = [ |
||
| 121 | $this->translator->trans('password'), |
||
| 122 | $this->translator->trans('email'), |
||
| 123 | $this->translator->trans('username'), |
||
| 124 | ]; |
||
| 125 | $choice = $io->choice($this->translator->trans('Value to change?'), $choices, $this->translator->trans('password')); |
||
| 126 | $method = $this->translator->trans('password') === $choice ? 'askHidden' : 'ask'; |
||
| 127 | $value = $io->{$method}($this->translator->trans('New value for %choice%?', ['%choice%' => $choice])); |
||
| 128 | $validators = array_combine($choices, [new ValidPassword(), new ValidEmail(), new ValidUname()]); |
||
| 129 | $errors = $this->validator->validate($value, $validators[$choice]); |
||
| 130 | if (0 !== count($errors)) { |
||
| 131 | $io->error($this->translator->trans('Invalid %choice%', ['%choice%' => $choice]) . '. ' . $errors[0]->getMessage()); |
||
| 132 | |||
| 133 | return 2; |
||
| 134 | } |
||
| 135 | |||
| 136 | switch ($choice) { |
||
| 137 | case 'password': |
||
| 138 | $mapping->setPass($this->encoderFactory->getEncoder($mapping)->encodePassword($value, null)); |
||
| 139 | break; |
||
| 140 | default: |
||
| 141 | unset($choices[0]); |
||
| 142 | $methods = array_combine($choices, ['setEmail', 'setUname']); |
||
| 143 | $mapping->{$methods[$choice]}($value); |
||
| 144 | $userEntity = $this->userRepository->findOneBy($criteria); |
||
| 145 | $userEntity->{$methods[$choice]}($value); |
||
| 146 | $this->userRepository->persistAndFlush($userEntity); |
||
|
|
|||
| 147 | break; |
||
| 148 | } |
||
| 149 | $this->authenticationMappingRepository->persistAndFlush($mapping); |
||
| 150 | $io->success($this->translator->trans('The %choice% has been changed.', ['%choice%' => $choice])); |
||
| 151 | |||
| 152 | return 0; |
||
| 153 | } |
||
| 155 |