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