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 |
||
| 39 | class TwoFactorMiddleware extends Middleware { |
||
| 40 | |||
| 41 | /** @var Manager */ |
||
| 42 | private $twoFactorManager; |
||
| 43 | |||
| 44 | /** @var Session */ |
||
| 45 | private $userSession; |
||
| 46 | |||
| 47 | /** @var ISession */ |
||
| 48 | private $session; |
||
| 49 | |||
| 50 | /** @var IURLGenerator */ |
||
| 51 | private $urlGenerator; |
||
| 52 | |||
| 53 | /** @var IControllerMethodReflector */ |
||
| 54 | private $reflector; |
||
| 55 | |||
| 56 | /** @var IRequest */ |
||
| 57 | private $request; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param Manager $twoFactorManager |
||
| 61 | * @param Session $userSession |
||
| 62 | * @param ISession $session |
||
| 63 | * @param IURLGenerator $urlGenerator |
||
| 64 | */ |
||
| 65 | View Code Duplication | public function __construct(Manager $twoFactorManager, Session $userSession, ISession $session, |
|
|
|
|||
| 66 | IURLGenerator $urlGenerator, IControllerMethodReflector $reflector, IRequest $request) { |
||
| 67 | $this->twoFactorManager = $twoFactorManager; |
||
| 68 | $this->userSession = $userSession; |
||
| 69 | $this->session = $session; |
||
| 70 | $this->urlGenerator = $urlGenerator; |
||
| 71 | $this->reflector = $reflector; |
||
| 72 | $this->request = $request; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param Controller $controller |
||
| 77 | * @param string $methodName |
||
| 78 | */ |
||
| 79 | public function beforeController($controller, $methodName) { |
||
| 80 | if ($this->reflector->hasAnnotation('PublicPage')) { |
||
| 81 | // Don't block public pages |
||
| 82 | return; |
||
| 83 | } |
||
| 84 | |||
| 85 | if ($controller instanceof \OC\Core\Controller\LoginController && $methodName === 'logout') { |
||
| 86 | // Don't block the logout page, to allow canceling the 2FA |
||
| 87 | return; |
||
| 88 | } |
||
| 89 | |||
| 90 | if ($this->userSession->isLoggedIn()) { |
||
| 91 | $user = $this->userSession->getUser(); |
||
| 92 | |||
| 93 | if ($this->twoFactorManager->isTwoFactorAuthenticated($user)) { |
||
| 94 | $this->checkTwoFactor($controller, $methodName); |
||
| 95 | } else if ($controller instanceof TwoFactorChallengeController) { |
||
| 96 | // Allow access to the two-factor controllers only if two-factor authentication |
||
| 97 | // is in progress. |
||
| 98 | throw new UserAlreadyLoggedInException(); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | // TODO: dont check/enforce 2FA if a auth token is used |
||
| 102 | } |
||
| 103 | |||
| 104 | private function checkTwoFactor($controller, $methodName) { |
||
| 121 | |||
| 122 | public function afterException($controller, $methodName, Exception $exception) { |
||
| 132 | |||
| 133 | } |
||
| 134 |
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.