Conditions | 20 |
Paths | 507 |
Total Lines | 76 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
53 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
54 | { |
||
55 | $authorization = $this->loadAuthorization($request); |
||
56 | |||
57 | try { |
||
58 | $user = $this->userManager->getCurrentUser(); |
||
59 | |||
60 | if (null !== $user) { |
||
61 | $authorization->setUser($user); |
||
62 | $userAccount = $this->userManager->getCurrentAccount(); |
||
63 | if ($userAccount) { |
||
64 | $authorization->setUserAccount($userAccount); |
||
65 | $isAccountSelectionNeeded = false; |
||
66 | } else { |
||
67 | $isAccountSelectionNeeded = true; |
||
68 | } |
||
69 | $isAuthenticationNeeded = $this->userCheckerManager->isAuthenticationNeeded($authorization); |
||
70 | $isConsentNeeded = !$this->consentRepository || !$this->consentRepository->hasConsentBeenGiven($authorization); |
||
71 | |||
72 | switch (true) { |
||
73 | case $authorization->hasPrompt('none'): |
||
74 | if ($isConsentNeeded) { |
||
75 | throw new OAuth2AuthorizationException(OAuth2Error::ERROR_INTERACTION_REQUIRED, 'The resource owner consent is required.', $authorization); |
||
76 | } |
||
77 | $authorization->allow(); |
||
78 | $routeName = 'oauth2_server_process_endpoint'; |
||
79 | break; |
||
80 | case $authorization->hasPrompt('login') || $isAuthenticationNeeded: |
||
81 | $routeName = 'oauth2_server_login_endpoint'; |
||
82 | break; |
||
83 | case $authorization->hasPrompt('select_account') || $isAccountSelectionNeeded: |
||
84 | $routeName = 'oauth2_server_select_account_endpoint'; |
||
85 | break; |
||
86 | case $authorization->hasPrompt('consent') || $isConsentNeeded: |
||
87 | $routeName = 'oauth2_server_consent_endpoint'; |
||
|
|||
88 | // no break |
||
89 | case !$authorization->hasPrompt('consent') && !$isConsentNeeded: |
||
90 | $authorization->allow(); |
||
91 | $routeName = 'oauth2_server_process_endpoint'; |
||
92 | break; |
||
93 | default: |
||
94 | $routeName = 'oauth2_server_consent_endpoint'; |
||
95 | break; |
||
96 | } |
||
97 | |||
98 | $authorizationId = Base64Url::encode(random_bytes(64)); |
||
99 | $this->saveAuthorization($authorizationId, $authorization); |
||
100 | $redirectTo = $this->getRouteFor($routeName, $authorizationId); |
||
101 | |||
102 | return $this->createRedirectResponse($redirectTo); |
||
103 | } else { |
||
104 | if ($authorization->hasPrompt('none')) { |
||
105 | $isConsentNeeded = !$this->consentRepository || !$this->consentRepository->hasConsentBeenGiven($authorization); |
||
106 | if ($isConsentNeeded) { |
||
107 | throw new OAuth2AuthorizationException(OAuth2Error::ERROR_LOGIN_REQUIRED, 'The resource owner is not logged in.', $authorization); |
||
108 | } |
||
109 | $authorization->allow(); |
||
110 | $routeName = 'oauth2_server_process_endpoint'; |
||
111 | } else { |
||
112 | $routeName = 'oauth2_server_login_endpoint'; |
||
113 | } |
||
114 | |||
115 | $authorizationId = Base64Url::encode(random_bytes(64)); |
||
116 | $this->saveAuthorization($authorizationId, $authorization); |
||
117 | $redirectTo = $this->getRouteFor($routeName, $authorizationId); |
||
118 | |||
119 | return $this->createRedirectResponse($redirectTo); |
||
120 | } |
||
121 | } catch (OAuth2AuthorizationException $e) { |
||
122 | throw $e; |
||
123 | } catch (OAuth2Error $e) { |
||
124 | throw new OAuth2AuthorizationException($e->getMessage(), $e->getErrorDescription(), $authorization, $e); |
||
125 | } catch (\Exception $e) { |
||
126 | throw new OAuth2AuthorizationException(OAuth2Error::ERROR_INVALID_REQUEST, $e->getMessage(), $authorization, $e); |
||
127 | } |
||
128 | } |
||
129 | |||
148 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.