Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
30 | class YubikeyController extends SecondFactorController |
||
31 | { |
||
32 | /** |
||
33 | * @Template |
||
34 | * @param Request $request |
||
35 | * @param string $procedureId |
||
36 | * @return array|Response |
||
37 | */ |
||
38 | public function verifyAction(Request $request, $procedureId) |
||
39 | { |
||
40 | $this->assertSecondFactorEnabled('yubikey'); |
||
41 | |||
42 | $this->denyAccessUnlessGranted(['ROLE_RA']); |
||
43 | |||
44 | $logger = $this->get('ra.procedure_logger')->forProcedure($procedureId); |
||
45 | $logger->notice('Requested Yubikey Verfication'); |
||
46 | |||
47 | View Code Duplication | if (!$this->getVettingService()->hasProcedure($procedureId)) { |
|
|
|||
48 | $logger->notice(sprintf('Vetting procedure "%s" not found', $procedureId)); |
||
49 | throw new NotFoundHttpException(sprintf('Vetting procedure "%s" not found', $procedureId)); |
||
50 | } |
||
51 | |||
52 | $command = new VerifyYubikeyPublicIdCommand(); |
||
53 | $form = $this->createForm(VerifyYubikeyPublicIdType::class, $command)->handleRequest($request); |
||
54 | |||
55 | View Code Duplication | if ($form->isSubmitted() && $form->isValid()) { |
|
56 | $result = $this->getVettingService()->verifyYubikeyPublicId($procedureId, $command); |
||
57 | |||
58 | if ($result->didPublicIdMatch()) { |
||
59 | $logger->notice('Yubikey Verified, redirecting to verify identity'); |
||
60 | |||
61 | return $this->redirectToRoute('ra_vetting_verify_identity', ['procedureId' => $procedureId]); |
||
62 | } |
||
63 | |||
64 | if ($result->wasOtpInvalid()) { |
||
65 | $this->addFlash('error', 'ra.verify_yubikey_command.otp.otp_invalid'); |
||
66 | } elseif ($result->didOtpVerificationFail()) { |
||
67 | $this->addFlash('error', 'ra.verify_yubikey_command.otp.verification_error'); |
||
68 | } else { |
||
69 | $this->addFlash('error', 'ra.prove_yubikey_possession.different_yubikey_used'); |
||
70 | } |
||
71 | |||
72 | $logger->notice('Yubikey could not be verified, added error to form'); |
||
73 | } |
||
74 | |||
75 | $logger->notice('Rendering Yubikey Verification Form'); |
||
76 | // OTP field is rendered empty in the template. |
||
77 | return ['form' => $form->createView()]; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return VettingService |
||
82 | */ |
||
83 | private function getVettingService() |
||
87 | } |
||
88 |
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.