Conditions | 23 |
Paths | 80 |
Total Lines | 101 |
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 |
||
248 | function verifyUserCodeParam() |
||
249 | { |
||
250 | $retval = false; // Assume user code & other session info is not valid |
||
251 | |||
252 | $log = new Loggit(); |
||
253 | |||
254 | // If idphint/selected_idp were previously set in the clientparams |
||
255 | // PHP session variable, get them this time around. |
||
256 | $clientparams = array(); |
||
257 | $clientparams = @array_intersect_key( |
||
258 | json_decode(Util::getSessionVar('clientparams'), true), |
||
259 | ['idphint' => 1, 'selected_idp' => 1] |
||
260 | ); |
||
261 | |||
262 | // If a user_code was passed in, use that to get the associated |
||
263 | // clientparams. Otherwise, get clientparams from the PHP session. |
||
264 | $user_code = Util::getGetorPostVar('user_code'); |
||
265 | if (strlen($user_code) > 0) { |
||
266 | Util::unsetSessionVar('clientparams'); // Don't use any previous values |
||
267 | $log->info('Calling checkUserCode dbService method...'); |
||
268 | $dbs = new DBService(); |
||
269 | if ( |
||
270 | ($dbs->checkUserCode($user_code)) && |
||
271 | (!($dbs->status & 1)) |
||
272 | ) { // STATUS_OK codes are even |
||
273 | if (strlen($dbs->client_id) > 0) { |
||
274 | // Use the client_id associated with the user_code to get |
||
275 | // the rest of the OAuth2 client registration information. |
||
276 | $clientparams['user_code'] = $dbs->user_code; |
||
277 | $clientparams['client_id'] = $dbs->client_id; |
||
278 | $clientparams['scope'] = $dbs->scope; |
||
279 | // getOIDCClientParams assumes client_id is stored in the |
||
280 | // passed-in $clientparams variable. |
||
281 | Util::getOIDCClientParams($clientparams); |
||
282 | // If no scope was requested, then assume ALL scopes |
||
283 | // 'scope' is a space-separated string, while |
||
284 | // client_scopes is a JSON list; need to transform into |
||
285 | // space-separated string. |
||
286 | if (strlen($clientparams['scope']) == 0) { |
||
287 | $clientparams['scope'] = implode( |
||
288 | ' ', |
||
289 | json_decode($clientparams['client_scopes'], true) |
||
290 | ); |
||
291 | } |
||
292 | } else { |
||
293 | Util::setSessionVar('user_code_error_msg', 'Unable to find a client matching the user code.'); |
||
294 | } |
||
295 | } else { // STATUS_ERROR code returned |
||
296 | $errstr = 'Error checking user code.'; // Generic error message |
||
297 | if (!is_null($dbs->status)) { |
||
298 | $errstr = array_search($dbs->status, DBService::$STATUS); |
||
299 | // Customize error messages for Device Authz Grant flow |
||
300 | if ($dbs->status == 0x10001) { |
||
301 | $errstr = 'User code not found.'; |
||
302 | } elseif ($db->status == 0x10003) { |
||
303 | $errstr = 'User code expired.'; |
||
304 | } |
||
305 | } |
||
306 | Util::setSessionVar('user_code_error_msg', $errstr); |
||
307 | } |
||
308 | } else { // No user_code passed in, so check the PHP session clientparams |
||
309 | $clientparams = json_decode(Util::getSessionVar('clientparams'), true); |
||
310 | } |
||
311 | |||
312 | // If no error so far, check all of the client parameters |
||
313 | if ( |
||
314 | (strlen(Util::getSessionVar('user_code_error_msg')) == 0) && |
||
315 | (isset($clientparams['user_code'])) && |
||
316 | (isset($clientparams['client_id'])) && |
||
317 | (isset($clientparams['scope'])) && |
||
318 | (isset($clientparams['client_name'])) && |
||
319 | (isset($clientparams['client_home_url'])) && |
||
320 | (isset($clientparams['client_callback_uri'])) && |
||
321 | (isset($clientparams['client_scopes'])) && |
||
322 | (isset($clientparams['clientstatus'])) && |
||
323 | (!($clientparams['clientstatus'] & 1)) |
||
324 | ) { // STATUS_OK codes are even |
||
325 | $retval = true; |
||
326 | Util::setSessionVar('clientparams', json_encode($clientparams)); |
||
327 | } else { |
||
328 | Util::unsetSessionVar('clientparams'); |
||
329 | } |
||
330 | |||
331 | // Save idphint/selected_idp from query parameters to PHP session |
||
332 | $idphint = Util::getGetVar('idphint'); |
||
333 | $selected_idp = Util::getGetVar('selected_idp'); |
||
334 | if ( |
||
335 | (strlen($idphint) > 0) || |
||
336 | (strlen($selected_idp) > 0) |
||
337 | ) { |
||
338 | if (strlen($idphint) > 0) { |
||
339 | $clientparams['idphint'] = $idphint; |
||
340 | } |
||
341 | if (strlen($selected_idp) > 0) { |
||
342 | $clientparams['selected_idp'] = $selected_idp; |
||
343 | } |
||
344 | Util::setSessionVar('clientparams', json_encode($clientparams)); |
||
345 | } |
||
346 | |||
347 | return $retval; |
||
348 | } |
||
349 |
This check looks for functions that have already been defined in other files.
Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the
@ignore
annotation.See also the PhpDoc documentation for @ignore.