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