| Conditions | 22 |
| Paths | 500 |
| Total Lines | 132 |
| Code Lines | 76 |
| Lines | 16 |
| Ratio | 12.12 % |
| 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 |
||
| 17 | public function execute() |
||
| 18 | { |
||
| 19 | $result = new \stdClass(); |
||
| 20 | |||
| 21 | $this->payLoad = $this->getPayload(); |
||
| 22 | $device_uid = ''; |
||
| 23 | if(isset($this->payLoad['device_uid'])) { |
||
| 24 | $device_uid = $this->payLoad['device_uid']; |
||
| 25 | } |
||
| 26 | |||
| 27 | |||
| 28 | $this->payLoad = json_encode($this->payLoad); |
||
| 29 | $header = $this->getSignHeaders(); |
||
| 30 | $url = $this->getFullUrl(); |
||
| 31 | |||
| 32 | if ($this->getAccessToken()) { |
||
| 33 | $header['Authorization'] = "Bearer " . $this->getAccessToken(); |
||
| 34 | } |
||
| 35 | //Comment out to debug the Request: |
||
| 36 | |||
| 37 | /* |
||
| 38 | printf("URL: "); |
||
| 39 | var_dump($url); |
||
| 40 | echo "<br />"; |
||
| 41 | printf("Header: "); |
||
| 42 | var_dump($header); |
||
| 43 | echo "<br />"; |
||
| 44 | printf("Payload: "); |
||
| 45 | var_dump($this->payLoad); |
||
| 46 | echo "<br />"; |
||
| 47 | */ |
||
| 48 | /* |
||
| 49 | $options = array( |
||
| 50 | 'timeout' => 100, |
||
| 51 | 'connect_timeout' => 100, |
||
| 52 | 'proxy' => '186.103.169.165:8080', |
||
| 53 | );*/ |
||
| 54 | |||
| 55 | switch ($this->getMethod()) { |
||
| 56 | case 'POST': |
||
| 57 | $result = Requests::post($url, $header, $this->payLoad); |
||
| 58 | break; |
||
| 59 | case 'GET': |
||
| 60 | if($this->hasPayload) |
||
| 61 | { |
||
| 62 | $result = Requests::get($url, $header, $this->payLoad); |
||
| 63 | } |
||
| 64 | else |
||
| 65 | { |
||
| 66 | $result = Requests::get($url, $header); |
||
| 67 | } |
||
| 68 | break; |
||
| 69 | case 'PUT': |
||
| 70 | $result = Requests::put($url, $header, $this->payLoad); |
||
| 71 | break; |
||
| 72 | } |
||
| 73 | |||
| 74 | http_response_code($result->status_code); |
||
| 75 | |||
| 76 | switch ($result->status_code) { |
||
| 77 | case 200: |
||
| 78 | $result = json_decode($result->body, true); |
||
| 79 | break; |
||
| 80 | case 204: |
||
| 81 | $result = 'Success'; |
||
| 82 | http_response_code(200); |
||
| 83 | break; |
||
| 84 | View Code Duplication | case 400: |
|
| 85 | $result = json_decode($result->body, true); |
||
| 86 | error_log('Error 400 - ' . print_r($result, true)); |
||
| 87 | break; |
||
| 88 | case 401: |
||
| 89 | //$result = json_decode($result->body, true); |
||
| 90 | |||
| 91 | if(is_array($result) && $result['error'] == 'length') |
||
| 92 | { |
||
| 93 | |||
| 94 | } |
||
| 95 | else |
||
| 96 | { |
||
| 97 | error_log('Error 401 - ' . print_r($result, true)); |
||
| 98 | } |
||
| 99 | break; |
||
| 100 | View Code Duplication | case 404: |
|
| 101 | error_log('Error 404 - ' . print_r($result, true)); |
||
| 102 | $result = json_decode($result->body, true); |
||
| 103 | break; |
||
| 104 | View Code Duplication | case 477: |
|
| 105 | $result = json_decode($result->body, true); |
||
| 106 | error_log('Error 477 - ' . print_r($result, true)); |
||
| 107 | break; |
||
| 108 | View Code Duplication | case 429: |
|
| 109 | error_log('Error 429 - Too Many Requests' . print_r(json_decode($result->body, true), true)); |
||
| 110 | exit("Error 429: Too Many Requests"); |
||
| 111 | break; |
||
| 112 | case 403: |
||
| 113 | error_log('Error 403 - Access denied:' . print_r(json_decode($result->body, true), true)); |
||
| 114 | $result = json_decode($result->body, true); |
||
| 115 | break; |
||
| 116 | case 502: |
||
| 117 | error_log('Error 502 - ' . print_r($result, true)); |
||
| 118 | $result = json_decode($result->body, true); |
||
| 119 | header('location:'.$_SERVER['PHP_SELF']); |
||
| 120 | break; |
||
| 121 | case 503: |
||
| 122 | error_log('Error 503 - ' . print_r($result, true)); |
||
| 123 | $result = json_decode($result->body, true); |
||
| 124 | |||
| 125 | if(array_key_exists('error', $result) && $result['error'] == 'Service Unavailable') |
||
| 126 | { |
||
| 127 | header('location:'.$_SERVER['PHP_SELF']); |
||
| 128 | } |
||
| 129 | break; |
||
| 130 | default: |
||
| 131 | error_log('Error '.$result->status_code.' - unknown error'); |
||
| 132 | $result = json_decode($result->body, true); |
||
| 133 | } |
||
| 134 | |||
| 135 | //important for account refresh |
||
| 136 | if($device_uid != '') |
||
| 137 | { |
||
| 138 | $result[0] = $result; |
||
| 139 | $result[1] = $device_uid; |
||
| 140 | } |
||
| 141 | |||
| 142 | |||
| 143 | /* |
||
| 144 | var_dump($result); |
||
| 145 | */ |
||
| 146 | |||
| 147 | return $result; |
||
| 148 | } |
||
| 149 | abstract function getPayload(); |
||
| 215 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.