| Conditions | 7 |
| Paths | 66 |
| Total Lines | 67 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 34 | public function inFoldersAction(array $userData): void |
||
| 35 | { |
||
| 36 | $superGlobal = new protect\SuperGlobal\SuperGlobal(); |
||
| 37 | $strErrorDesc = ''; |
||
| 38 | $requestMethod = $superGlobal->get('REQUEST_METHOD', 'SERVER'); |
||
| 39 | |||
| 40 | // get parameters |
||
| 41 | $arrQueryStringParams = $this->getQueryStringParams(); |
||
| 42 | |||
| 43 | if (strtoupper($requestMethod) === 'GET') { |
||
| 44 | // define WHERE clause |
||
| 45 | $sqlExtra = ''; |
||
| 46 | if (empty($userData['folders_list']) === false) { |
||
| 47 | $userData['folders_list'] = explode(',', $userData['folders_list']); |
||
| 48 | } else { |
||
| 49 | $userData['folders_list'] = []; |
||
| 50 | } |
||
| 51 | |||
| 52 | // SQL where clause with folders list |
||
| 53 | if (isset($arrQueryStringParams['folders']) === true) { |
||
| 54 | // convert the folders to an array |
||
| 55 | $arrQueryStringParams['folders'] = explode(',', str_replace( array('[',']') , '' , $arrQueryStringParams['folders'])); |
||
| 56 | |||
| 57 | // ensure to only use the intersection |
||
| 58 | $foldersList = implode(',', array_intersect($arrQueryStringParams['folders'], $userData['folders_list'])); |
||
| 59 | |||
| 60 | // build sql where clause |
||
| 61 | $sqlExtra = ' WHERE id_tree IN ('.$foldersList.')'; |
||
| 62 | } else { |
||
| 63 | // Send error |
||
| 64 | $this->sendOutput( |
||
| 65 | json_encode(['error' => 'Folders are mandatory']), |
||
| 66 | ['Content-Type: application/json', 'HTTP/1.1 401 Expected parameters not provided'] |
||
| 67 | ); |
||
| 68 | } |
||
| 69 | |||
| 70 | // SQL LIMIT |
||
| 71 | $intLimit = 0; |
||
| 72 | if (isset($arrQueryStringParams['limit']) === true) { |
||
| 73 | $intLimit = $arrQueryStringParams['limit']; |
||
| 74 | } |
||
| 75 | |||
| 76 | // send query |
||
| 77 | try { |
||
| 78 | $itemModel = new ItemModel(); |
||
| 79 | |||
| 80 | $arrItems = $itemModel->getItems($sqlExtra, $intLimit, $userData['private_key'], $userData['id']); |
||
| 81 | $responseData = json_encode($arrItems); |
||
| 82 | } catch (Error $e) { |
||
| 83 | $strErrorDesc = $e->getMessage().'. Something went wrong! Please contact support.'; |
||
| 84 | $strErrorHeader = 'HTTP/1.1 500 Internal Server Error'; |
||
| 85 | } |
||
| 86 | } else { |
||
| 87 | $strErrorDesc = 'Method not supported'; |
||
| 88 | $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; |
||
| 89 | } |
||
| 90 | |||
| 91 | // send output |
||
| 92 | if (empty($strErrorDesc) === true) { |
||
| 93 | $this->sendOutput( |
||
| 94 | $responseData, |
||
|
|
|||
| 95 | ['Content-Type: application/json', 'HTTP/1.1 200 OK'] |
||
| 96 | ); |
||
| 97 | } else { |
||
| 98 | $this->sendOutput( |
||
| 99 | json_encode(['error' => $strErrorDesc]), |
||
| 100 | ['Content-Type: application/json', $strErrorHeader] |
||
| 101 | ); |
||
| 163 | } |