| Conditions | 9 |
| Paths | 22 |
| Total Lines | 57 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 30 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 31 | { |
||
| 32 | $io = new SymfonyStyle($input, $output); |
||
| 33 | $helper = $this->getHelper('question'); |
||
| 34 | $email = $input->getArgument('email'); |
||
| 35 | $roleName = $input->getArgument('role'); |
||
| 36 | |||
| 37 | /** @var Role[] $allRoles */ |
||
| 38 | $allRoles = $this->findAllRoles(); |
||
| 39 | if (0 === \count($allRoles)) { |
||
| 40 | $io->error('There is no role to select. Please, create some role first.'); |
||
| 41 | |||
| 42 | return 1; |
||
| 43 | } |
||
| 44 | |||
| 45 | if (null === $email) { |
||
| 46 | $question = new Question('Please, enter the email address of the user: '); |
||
| 47 | $question->setValidator(function ($answer) { |
||
| 48 | if (null === $answer) { |
||
| 49 | throw new \Exception('The email address of the user to promote is required'); |
||
| 50 | } |
||
| 51 | |||
| 52 | return $answer; |
||
| 53 | }); |
||
| 54 | $question->setMaxAttempts(self::QUESTION_MAX_ATTEMPTS); |
||
| 55 | if ($this->isTestEnv()) { |
||
| 56 | $question->setMaxAttempts(1); |
||
| 57 | } |
||
| 58 | $email = $helper->ask($input, $output, $question); |
||
| 59 | } |
||
| 60 | $user = $this->findUserByEmail($email); |
||
| 61 | if (null === $user) { |
||
| 62 | $io->error(sprintf('The user with the email "%s" doesnt exist', $email)); |
||
| 63 | |||
| 64 | return 1; |
||
| 65 | } |
||
| 66 | |||
| 67 | $role = null; |
||
| 68 | if (null !== $roleName) { |
||
| 69 | $role = $this->findRoleByName($roleName); |
||
|
|
|||
| 70 | if (null === $role) { |
||
| 71 | $io->warning(sprintf('The role "%s" doesnt exist. Please, select a valid role', $roleName)); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | if (null === $role) { |
||
| 75 | $question = new ChoiceQuestion('Select a role', $allRoles); |
||
| 76 | $question->setErrorMessage('The role %s is invalid.'); |
||
| 77 | $roleName = $helper->ask($input, $output, $question); |
||
| 78 | $role = $this->findRoleByName($roleName); |
||
| 79 | } |
||
| 80 | $user->addRoleEntity($role); |
||
| 81 | $this->getEntityManager()->persist($user); |
||
| 82 | $this->getEntityManager()->flush(); |
||
| 83 | |||
| 84 | $io->success(sprintf('The role %s was added to the user %s', $roleName, $email)); |
||
| 85 | |||
| 86 | return 0; |
||
| 87 | } |
||
| 89 |