Conditions | 13 |
Paths | 104 |
Total Lines | 75 |
Code Lines | 51 |
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 |
||
87 | public function authenticateWithSuperdeskAction(Request $request) |
||
88 | { |
||
89 | $form = $this->createForm(SuperdeskCredentialAuthenticationType::class, []); |
||
90 | $form->handleRequest($request); |
||
91 | if ($form->isValid()) { |
||
92 | $formData = $form->getData(); |
||
93 | $authorizedSuperdeskHosts = $this->container->getParameter('superdesk_servers'); |
||
94 | $superdeskUser = null; |
||
95 | $client = new GuzzleHttp\Client(); |
||
96 | |||
97 | foreach ($authorizedSuperdeskHosts as $baseUrl) { |
||
98 | try { |
||
99 | $apiRequest = new GuzzleHttp\Psr7\Request('GET', sprintf('%s/api/sessions/%s', $baseUrl, $formData['session_id']), [ |
||
100 | 'Authorization' => $formData['token'], |
||
101 | ]); |
||
102 | $apiResponse = $client->send($apiRequest); |
||
103 | } catch (GuzzleHttp\Exception\ClientException $e) { |
||
104 | if ($e->getResponse()->getStatusCode() !== 200) { |
||
105 | continue; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | if ($apiResponse->getStatusCode() !== 200) { |
||
|
|||
110 | continue; |
||
111 | } |
||
112 | |||
113 | $content = json_decode($apiResponse->getBody()->getContents(), true); |
||
114 | if (is_array($content) && array_key_exists('user', $content)) { |
||
115 | $superdeskUser = $content['user']; |
||
116 | |||
117 | break; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | if (null == $superdeskUser) { |
||
122 | return new SingleResourceResponse([ |
||
123 | 'status' => 401, |
||
124 | 'message' => 'Unauthorized (user not found in Superdesk)', |
||
125 | ], new ResponseContext(401)); |
||
126 | } |
||
127 | |||
128 | $userProvider = $this->get('swp.security.user_provider'); |
||
129 | $publisherUser = $userProvider->findOneByEmail($superdeskUser['email']); |
||
130 | if (null === $publisherUser) { |
||
131 | try { |
||
132 | $publisherUser = $userProvider->loadUserByUsername($superdeskUser['username']); |
||
133 | } catch (UsernameNotFoundException $e) { |
||
134 | $publisherUser = null; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | if (null === $publisherUser) { |
||
139 | $userManager = $this->get('fos_user.user_manager'); |
||
140 | /** @var UserInterface $publisherUser */ |
||
141 | $publisherUser = $userManager->createUser(); |
||
142 | $publisherUser->setUsername($superdeskUser['username']); |
||
143 | $publisherUser->setEmail($superdeskUser['email']); |
||
144 | $publisherUser->setRoles(['ROLE_INTERNAL_API', 'ROLE_USER']); |
||
145 | $publisherUser->setFirstName($superdeskUser['first_name']); |
||
146 | $publisherUser->setLastName($superdeskUser['last_name']); |
||
147 | $publisherUser->setPlainPassword(password_hash(random_bytes(36), PASSWORD_BCRYPT)); |
||
148 | $publisherUser->setEnabled(true); |
||
149 | $userManager->updateUser($publisherUser); |
||
150 | } |
||
151 | |||
152 | if (null !== $publisherUser) { |
||
153 | return $this->getApiToken($publisherUser, str_replace('Basic ', '', $formData['token'])); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | return new SingleResourceResponse([ |
||
158 | 'status' => 401, |
||
159 | 'message' => 'Unauthorized', |
||
160 | ], new ResponseContext(401)); |
||
161 | } |
||
162 | |||
190 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: