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