| Conditions | 6 |
| Paths | 6 |
| Total Lines | 55 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 1 | 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 |
||
| 93 | public function verifySmsSecondFactorChallengeAction(Request $request) |
||
| 94 | { |
||
| 95 | /** @var ResponseContext $context */ |
||
| 96 | $context = $this->get( |
||
| 97 | $this->get('gateway.proxy.state_handler')->getResponseContextServiceId() |
||
| 98 | ); |
||
| 99 | $originalRequestId = $context->getInResponseTo(); |
||
| 100 | |||
| 101 | $logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
||
| 102 | |||
| 103 | $selectedSecondFactor = $this->get('gateway.service.require_selected_factor') |
||
| 104 | ->requireSelectedSecondFactor($logger); |
||
| 105 | |||
| 106 | $command = new VerifyPossessionOfPhoneCommand(); |
||
| 107 | $form = $this->createForm('gateway_verify_sms_challenge', $command)->handleRequest($request); |
||
| 108 | |||
| 109 | if ($form->get('cancel')->isClicked()) { |
||
| 110 | return $this->forward('SurfnetStepupGatewayGatewayBundle:Failure:sendAuthenticationCancelledByUser'); |
||
| 111 | } |
||
| 112 | |||
| 113 | if (!$form->isValid()) { |
||
| 114 | return ['form' => $form->createView()]; |
||
| 115 | } |
||
| 116 | |||
| 117 | $logger->notice('Verifying input SMS challenge matches'); |
||
| 118 | |||
| 119 | $verification = $this->get('gateway.service.stepup_authentication')->verifySmsChallenge($command); |
||
| 120 | |||
| 121 | if ($verification->wasSuccessful()) { |
||
| 122 | $this->get('gateway.service.stepup_authentication')->clearSmsVerificationState(); |
||
| 123 | |||
| 124 | $context->markSecondFactorVerified(); |
||
| 125 | $this->get('gateway.authentication_logger')->logSecondFactorAuthentication($originalRequestId); |
||
| 126 | |||
| 127 | $logger->info( |
||
| 128 | sprintf( |
||
| 129 | 'Marked Sms Second Factor "%s" as verified, forwarding to Saml Proxy to respond', |
||
| 130 | $selectedSecondFactor |
||
| 131 | ) |
||
| 132 | ); |
||
| 133 | |||
| 134 | return $this->forward($context->getResponseAction()); |
||
| 135 | } elseif ($verification->didOtpExpire()) { |
||
| 136 | $logger->notice('SMS challenge expired'); |
||
| 137 | $form->addError(new FormError('gateway.form.send_sms_challenge.challenge_expired')); |
||
| 138 | } elseif ($verification->wasAttemptedTooManyTimes()) { |
||
| 139 | $logger->notice('SMS challenge verification was attempted too many times'); |
||
| 140 | $form->addError(new FormError('gateway.form.send_sms_challenge.too_many_attempts')); |
||
| 141 | } else { |
||
| 142 | $logger->notice('SMS challenge did not match'); |
||
| 143 | $form->addError(new FormError('gateway.form.send_sms_challenge.sms_challenge_incorrect')); |
||
| 144 | } |
||
| 145 | |||
| 146 | return ['form' => $form->createView()]; |
||
| 147 | } |
||
| 148 | } |
||
| 149 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: