| Conditions | 8 |
| Paths | 14 |
| Total Lines | 60 |
| Code Lines | 39 |
| Lines | 9 |
| Ratio | 15 % |
| 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 |
||
| 56 | public function revokeAction(Request $request, $state, $secondFactorId) |
||
| 57 | { |
||
| 58 | $identity = $this->getIdentity(); |
||
| 59 | |||
| 60 | /** @var SecondFactorService $service */ |
||
| 61 | $service = $this->get('surfnet_stepup_self_service_self_service.service.second_factor'); |
||
| 62 | View Code Duplication | if (!$service->identityHasSecondFactorOfStateWithId($identity->id, $state, $secondFactorId)) { |
|
|
|
|||
| 63 | $this->get('logger')->error(sprintf( |
||
| 64 | 'Identity "%s" tried to revoke "%s" second factor "%s", but does not own that second factor', |
||
| 65 | $identity->id, |
||
| 66 | $state, |
||
| 67 | $secondFactorId |
||
| 68 | )); |
||
| 69 | throw new NotFoundHttpException(); |
||
| 70 | } |
||
| 71 | |||
| 72 | switch ($state) { |
||
| 73 | case 'unverified': |
||
| 74 | $secondFactor = $service->findOneUnverified($secondFactorId); |
||
| 75 | break; |
||
| 76 | case 'verified': |
||
| 77 | $secondFactor = $service->findOneVerified($secondFactorId); |
||
| 78 | break; |
||
| 79 | case 'vetted': |
||
| 80 | $secondFactor = $service->findOneVetted($secondFactorId); |
||
| 81 | break; |
||
| 82 | default: |
||
| 83 | throw new LogicException('There are no other types of second factor.'); |
||
| 84 | } |
||
| 85 | |||
| 86 | if ($secondFactor === null) { |
||
| 87 | throw new NotFoundHttpException( |
||
| 88 | sprintf("No %s second factor with id '%s' exists.", $state, $secondFactorId) |
||
| 89 | ); |
||
| 90 | } |
||
| 91 | |||
| 92 | $command = new RevokeCommand(); |
||
| 93 | $command->identity = $identity; |
||
| 94 | $command->secondFactor = $secondFactor; |
||
| 95 | |||
| 96 | $form = $this->createForm('ss_revoke_second_factor', $command)->handleRequest($request); |
||
| 97 | |||
| 98 | if ($form->isValid()) { |
||
| 99 | /** @var FlashBagInterface $flashBag */ |
||
| 100 | $flashBag = $this->get('session')->getFlashBag(); |
||
| 101 | |||
| 102 | if ($service->revoke($command)) { |
||
| 103 | $flashBag->add('success', 'ss.second_factor.revoke.alert.revocation_successful'); |
||
| 104 | } else { |
||
| 105 | $flashBag->add('error', 'ss.second_factor.revoke.alert.revocation_failed'); |
||
| 106 | } |
||
| 107 | |||
| 108 | return $this->redirectToRoute('ss_second_factor_list'); |
||
| 109 | } |
||
| 110 | |||
| 111 | return [ |
||
| 112 | 'form' => $form->createView(), |
||
| 113 | 'secondFactor' => $secondFactor, |
||
| 114 | ]; |
||
| 115 | } |
||
| 116 | } |
||
| 117 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.