Conditions | 5 |
Paths | 5 |
Total Lines | 55 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
36 | public function verifyYubiKeySecondFactorAction(Request $request) |
||
37 | { |
||
38 | /** @var ResponseContext $responseContext */ |
||
39 | $context = $this->get( |
||
40 | $this->get('gateway.proxy.state_handler')->getResponseContextServiceId() |
||
41 | ); |
||
42 | $originalRequestId = $context->getInResponseTo(); |
||
43 | |||
44 | $logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
||
45 | |||
46 | $selectedSecondFactor = $this->get('gateway.service.require_selected_factor') |
||
47 | ->requireSelectedSecondFactor($logger); |
||
48 | |||
49 | $logger->notice('Verifying possession of Yubikey second factor'); |
||
50 | |||
51 | $command = new VerifyYubikeyOtpCommand(); |
||
52 | $command->secondFactorId = $selectedSecondFactor; |
||
53 | |||
54 | $form = $this->createForm('gateway_verify_yubikey_otp', $command)->handleRequest($request); |
||
55 | |||
56 | if ($form->get('cancel')->isClicked()) { |
||
|
|||
57 | return $this->forward('SurfnetStepupGatewayGatewayBundle:Failure:sendAuthenticationCancelledByUser'); |
||
58 | } |
||
59 | |||
60 | if (!$form->isValid()) { |
||
61 | // OTP field is rendered empty in the template. |
||
62 | return ['form' => $form->createView()]; |
||
63 | } |
||
64 | |||
65 | $result = $this->get('gateway.service.stepup_authentication')->verifyYubikeyOtp($command); |
||
66 | |||
67 | if ($result->didOtpVerificationFail()) { |
||
68 | $form->addError(new FormError('gateway.form.verify_yubikey.otp_verification_failed')); |
||
69 | |||
70 | // OTP field is rendered empty in the template. |
||
71 | return ['form' => $form->createView()]; |
||
72 | } elseif (!$result->didPublicIdMatch()) { |
||
73 | $form->addError(new FormError('gateway.form.verify_yubikey.public_id_mismatch')); |
||
74 | |||
75 | // OTP field is rendered empty in the template. |
||
76 | return ['form' => $form->createView()]; |
||
77 | } |
||
78 | |||
79 | $context->markSecondFactorVerified(); |
||
80 | $this->get('gateway.authentication_logger')->logSecondFactorAuthentication($originalRequestId); |
||
81 | |||
82 | $logger->info( |
||
83 | sprintf( |
||
84 | 'Marked Yubikey Second Factor "%s" as verified, forwarding to Saml Proxy to respond', |
||
85 | $selectedSecondFactor |
||
86 | ) |
||
87 | ); |
||
88 | |||
89 | return $this->forward($context->getResponseAction()); |
||
90 | } |
||
91 | } |
||
92 |
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: