Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 3 | abstract class AbstractRequest |
||
|
|
|||
| 4 | { |
||
| 5 | const CLIENTID = '81e8a76e-1e02-4d17-9ba0-8a7020261b26'; |
||
| 6 | const APIURL = 'https://api.go-tellm.com/api'; |
||
| 7 | const SECRET = 'hFvMqLauMtnodakokftuKETbIsVLxpqfjAXiRoih'; |
||
| 8 | const USERAGENT = 'Jodel/4.41.0 Dalvik/2.1.0 (Linux; U; Android 5.1.1; )'; |
||
| 9 | const CLIENT_TYPE = 'android_4.41.0'; |
||
| 10 | |||
| 11 | private $accessToken = null; |
||
| 12 | private $payLoad; |
||
| 13 | public $expects = ''; |
||
| 14 | public $version = 'v2'; |
||
| 15 | public $hasPayload = FALSE; |
||
| 16 | |||
| 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(); |
||
| 150 | /** |
||
| 151 | * Gets Sign headers |
||
| 152 | * @return array headers |
||
| 153 | */ |
||
| 154 | private function getSignHeaders() |
||
| 200 | /** |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | private function getAccessToken() |
||
| 207 | /** |
||
| 208 | * @param string $accessToken |
||
| 209 | */ |
||
| 210 | public function setAccessToken($accessToken) |
||
| 214 | } |
||
| 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.