| Conditions | 11 |
| Paths | 82 |
| Total Lines | 57 |
| Code Lines | 37 |
| 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 |
||
| 185 | public function getAction(array $userData): void |
||
| 186 | { |
||
| 187 | $superGlobal = new SuperGlobal(); |
||
| 188 | $strErrorDesc = ''; |
||
| 189 | $sqlExtra = ''; |
||
| 190 | $responseData = ''; |
||
| 191 | $strErrorHeader = ''; |
||
| 192 | $requestMethod = $superGlobal->get('REQUEST_METHOD', 'SERVER'); |
||
| 193 | $sql_constraint = ' AND (i.id_tree IN ('.$userData['folders_list'].') OR i.id IN ('.$userData['restricted_items_list'].'))'; |
||
| 194 | |||
| 195 | // get parameters |
||
| 196 | $arrQueryStringParams = $this->getQueryStringParams(); |
||
| 197 | |||
| 198 | if (strtoupper($requestMethod) === 'GET') { |
||
| 199 | // SQL where clause with item id |
||
| 200 | if (isset($arrQueryStringParams['id']) === true) { |
||
| 201 | // build sql where clause by ID |
||
| 202 | $sqlExtra = ' WHERE i.id = '.$arrQueryStringParams['id'] . $sql_constraint; |
||
| 203 | } else if (isset($arrQueryStringParams['label']) === true) { |
||
| 204 | // build sql where clause by LABEL |
||
| 205 | $sqlExtra = ' WHERE i.label '.(isset($arrQueryStringParams['like']) === true && (int) $arrQueryStringParams['like'] === 1 ? ' LIKE '.$arrQueryStringParams['label'] : ' = '.$arrQueryStringParams['label']) . $sql_constraint; |
||
| 206 | } else if (isset($arrQueryStringParams['description']) === true) { |
||
| 207 | // build sql where clause by LABEL |
||
| 208 | $sqlExtra = ' WHERE i.description '.(isset($arrQueryStringParams['like']) === true && (int) $arrQueryStringParams['like'] === 1 ? ' LIKE '.$arrQueryStringParams['description'] : ' = '.$arrQueryStringParams['description']).$sql_constraint; |
||
| 209 | } else { |
||
| 210 | // Send error |
||
| 211 | $this->sendOutput( |
||
| 212 | json_encode(['error' => 'Item id, label or description is mandatory']), |
||
| 213 | ['Content-Type: application/json', 'HTTP/1.1 401 Expected parameters not provided'] |
||
| 214 | ); |
||
| 215 | } |
||
| 216 | |||
| 217 | // send query |
||
| 218 | try { |
||
| 219 | $itemModel = new ItemModel(); |
||
| 220 | |||
| 221 | $arrItems = $itemModel->getItems($sqlExtra, 0, $userData['private_key'], $userData['id']); |
||
| 222 | $responseData = json_encode($arrItems); |
||
| 223 | } catch (Error $e) { |
||
| 224 | $strErrorDesc = $e->getMessage().'. Something went wrong! Please contact support.'; |
||
| 225 | $strErrorHeader = 'HTTP/1.1 500 Internal Server Error'; |
||
| 226 | } |
||
| 227 | } else { |
||
| 228 | $strErrorDesc = 'Method not supported'; |
||
| 229 | $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; |
||
| 230 | } |
||
| 231 | |||
| 232 | // send output |
||
| 233 | if (empty($strErrorDesc) === true) { |
||
| 234 | $this->sendOutput( |
||
| 235 | $responseData, |
||
| 236 | ['Content-Type: application/json', 'HTTP/1.1 200 OK'] |
||
| 237 | ); |
||
| 238 | } else { |
||
| 239 | $this->sendOutput( |
||
| 240 | json_encode(['error' => $strErrorDesc]), |
||
| 241 | ['Content-Type: application/json', $strErrorHeader] |
||
| 242 | ); |
||
| 246 | } |