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 |
||
17 | use Psr\Log\LoggerAwareTrait; |
||
18 | |||
19 | final class OAuth2Presenter extends Presenter implements LoggerAwareInterface |
||
20 | { |
||
21 | use LoggerAwareTrait; |
||
22 | use Psr7Trait; |
||
23 | |||
24 | const SESSION_NAMESPACE = 'nette-oauth2-server'; |
||
25 | |||
26 | /** |
||
27 | * @var IAuthorizationRequestSerializer |
||
28 | * @inject |
||
29 | */ |
||
30 | public $authorizationRequestSerializer; |
||
31 | |||
32 | /** |
||
33 | * @var AuthorizationServer |
||
34 | * @inject |
||
35 | */ |
||
36 | public $authorizationServer; |
||
37 | |||
38 | /** |
||
39 | * @var RedirectConfig |
||
40 | * @inject |
||
41 | */ |
||
42 | public $redirectConfig; |
||
43 | |||
44 | public function actionAccessToken() |
||
80 | |||
81 | public function actionAuthorize() |
||
82 | { |
||
83 | $response = $this->createResponse(); |
||
84 | |||
85 | View Code Duplication | if (!$this->getHttpRequest()->isMethod(IRequest::GET)) { |
|
86 | $body = $this->createStream(); |
||
87 | $body->write('Method not allowed'); |
||
88 | /** @var ApplicationPsr7ResponseInterface $response */ |
||
89 | $response = $response->withStatus(IResponse::S405_METHOD_NOT_ALLOWED)->withBody($body); |
||
90 | $this->sendResponse($response); |
||
91 | } |
||
92 | |||
93 | try { |
||
94 | $this->getSession(self::SESSION_NAMESPACE)->authorizationRequest = $this->authorizationRequestSerializer->serialize( |
||
95 | $this->authorizationServer->validateAuthorizationRequest($this->createServerRequest()) |
||
96 | ); |
||
97 | if (!$this->getUser()->isLoggedIn()) { |
||
98 | $this->redirectConfig->redirectToLoginDestination($this); |
||
99 | } |
||
100 | $this->redirectConfig->redirectToApproveDestination($this); |
||
101 | |||
102 | } catch (AbortException $e) { |
||
103 | throw $e; |
||
104 | |||
105 | } catch (OAuthServerException $e) { |
||
106 | /** @var ApplicationPsr7ResponseInterface $response */ |
||
107 | $response = $e->generateHttpResponse($response); |
||
108 | $this->sendResponse($response); |
||
109 | |||
110 | } catch (\Throwable $e) { |
||
123 |
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.