| Conditions | 12 |
| Paths | 46 |
| Total Lines | 67 |
| Code Lines | 47 |
| 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 |
||
| 183 | public function getAction(array $userData) |
||
| 184 | { |
||
| 185 | $superGlobal = new protect\SuperGlobal\SuperGlobal(); |
||
| 186 | $strErrorDesc = ''; |
||
| 187 | $requestMethod = $superGlobal->get('REQUEST_METHOD', 'SERVER'); |
||
| 188 | |||
| 189 | // get parameters |
||
| 190 | $arrQueryStringParams = $this->getQueryStringParams(); |
||
| 191 | |||
| 192 | if (strtoupper($requestMethod) === 'GET') { |
||
| 193 | if (empty($userData['folders_list']) === false) { |
||
| 194 | $userData['folders_list'] = explode(',', $userData['folders_list']); |
||
| 195 | } else { |
||
| 196 | $userData['folders_list'] = []; |
||
| 197 | } |
||
| 198 | |||
| 199 | if (isset($arrQueryStringParams['id']) === true && empty($arrQueryStringParams['id']) === false ) { |
||
| 200 | try { |
||
| 201 | $itemModel = new ItemModel(); |
||
| 202 | |||
| 203 | $item = $itemModel->getItem($arrQueryStringParams['id'], $userData['private_key'], $userData['id'], $userData['folders_list']); |
||
| 204 | |||
| 205 | if (!empty($item)) { |
||
| 206 | $responseData = json_encode($item); |
||
| 207 | } else { |
||
| 208 | $strErrorDesc = 'No content for this label'; |
||
| 209 | $strErrorHeader = 'HTTP/1.1 204 No Content'; |
||
| 210 | } |
||
| 211 | } catch (Error $e) { |
||
| 212 | $strErrorDesc = $e->getMessage().'. Something went wrong! Please contact support.'; |
||
| 213 | $strErrorHeader = 'HTTP/1.1 500 Internal Server Error'; |
||
| 214 | } |
||
| 215 | } else if (isset($arrQueryStringParams['label']) === true && empty($arrQueryStringParams['label']) === false ) { |
||
| 216 | try { |
||
| 217 | $itemModel = new ItemModel(); |
||
| 218 | |||
| 219 | $item = $itemModel->getItemByLabel($arrQueryStringParams['label'], $userData['private_key'], $userData['id'], $userData['folders_list']); |
||
| 220 | |||
| 221 | if (!empty($item)) { |
||
| 222 | $responseData = json_encode($item); |
||
| 223 | } else { |
||
| 224 | $strErrorDesc = 'No content for this label'; |
||
| 225 | $strErrorHeader = 'HTTP/1.1 204 No Content'; |
||
| 226 | } |
||
| 227 | } catch (Error $e) { |
||
| 228 | $strErrorDesc = $e->getMessage().'. Something went wrong! Please contact support.'; |
||
| 229 | $strErrorHeader = 'HTTP/1.1 500 Internal Server Error'; |
||
| 230 | } |
||
| 231 | } else { |
||
| 232 | $strErrorDesc = 'Id are mandatory'; |
||
| 233 | $strErrorHeader = 'HTTP/1.1 204 No Content'; |
||
| 234 | } |
||
| 235 | } else { |
||
| 236 | $strErrorDesc = 'Method not supported'; |
||
| 237 | $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; |
||
| 238 | } |
||
| 239 | |||
| 240 | // send output |
||
| 241 | if (empty($strErrorDesc) === true) { |
||
| 242 | $this->sendOutput( |
||
| 243 | $responseData, |
||
| 244 | ['Content-Type: application/json', 'HTTP/1.1 200 OK'] |
||
| 245 | ); |
||
| 246 | } else { |
||
| 247 | $this->sendOutput( |
||
| 248 | json_encode(['error' => $strErrorDesc]), |
||
| 249 | ['Content-Type: application/json', $strErrorHeader] |
||
| 250 | ); |
||
| 255 |