| Conditions | 12 |
| Paths | 240 |
| Total Lines | 54 |
| Code Lines | 34 |
| 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 |
||
| 37 | public function callMethod($requestPayload) |
||
| 38 | { |
||
| 39 | try { |
||
| 40 | $_request = $this->decode($requestPayload); |
||
| 41 | |||
| 42 | if (isset($_request['jsonrpc'])) { |
||
| 43 | $this->_specificationVersion = $_request['jsonrpc']; |
||
| 44 | if ($this->_specificationVersion > 2.0) { |
||
| 45 | throw new TRpcException('Unsupported specification version', '-32600'); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | if (isset($_request['id'])) { |
||
| 50 | $this->_id = $_request['id']; |
||
| 51 | } |
||
| 52 | |||
| 53 | if (!isset($_request['method'])) { |
||
| 54 | throw new TRpcException('Missing request method', '-32600'); |
||
| 55 | } |
||
| 56 | |||
| 57 | if (!isset($_request['params'])) { |
||
| 58 | $parameters = []; |
||
| 59 | } else { |
||
| 60 | $parameters = $_request['params']; |
||
| 61 | } |
||
| 62 | |||
| 63 | if (!is_array($parameters)) { |
||
| 64 | $parameters = [$parameters]; |
||
| 65 | } |
||
| 66 | |||
| 67 | // a request without an id is a notification that doesn't need a response |
||
| 68 | if ($this->_id !== null) { |
||
| 69 | if ($this->_specificationVersion == 2.0) { |
||
| 70 | return $this->encode([ |
||
| 71 | 'jsonrpc' => '2.0', |
||
| 72 | 'id' => $this->_id, |
||
| 73 | 'result' => $this->callApiMethod($_request['method'], $parameters), |
||
| 74 | ]); |
||
| 75 | } else { |
||
| 76 | return $this->encode([ |
||
| 77 | 'id' => $this->_id, |
||
| 78 | 'result' => $this->callApiMethod($_request['method'], $_request['params']), |
||
| 79 | 'error' => null, |
||
| 80 | ]); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } catch (TRpcException $e) { |
||
| 84 | return $this->createErrorResponse($e); |
||
| 85 | } catch (THttpException $e) { |
||
| 86 | throw $e; |
||
| 87 | } catch (\Exception $e) { |
||
| 88 | return $this->createErrorResponse(new TRpcException('An internal error occured', '-32603')); |
||
| 89 | } |
||
| 90 | return ''; |
||
| 91 | } |
||
| 168 |