| Conditions | 18 |
| Paths | 58 |
| Total Lines | 81 |
| Code Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 2 | 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 |
||
| 64 | public function getFile($url, $dstpath, $headers = []) { |
||
| 65 | if (empty($url)) { |
||
| 66 | throw new Exception('Empty path'); |
||
| 67 | } |
||
| 68 | $url = $this->getAbsoluteUrl($url); |
||
| 69 | $file_handle = fopen($dstpath, "w"); |
||
| 70 | |||
| 71 | if (!$file_handle) { |
||
| 72 | throw new Exception('[CURL] Error writing to temporary file! (' . $dstpath . ')'); |
||
| 73 | } |
||
| 74 | |||
| 75 | // straight up curl instead of sabredav here, sabredav put's the entire get result in memory |
||
| 76 | $curl = curl_init($url); |
||
| 77 | |||
| 78 | if ($this->verifyPeer !== null) { |
||
| 79 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->verifyPeer); |
||
| 80 | } |
||
| 81 | if ($this->trustedCertificates) { |
||
| 82 | curl_setopt($curl, CURLOPT_CAINFO, $this->trustedCertificates); |
||
| 83 | } |
||
| 84 | |||
| 85 | curl_setopt($curl, CURLOPT_USERPWD, $this->userName . ":" . $this->password); |
||
| 86 | curl_setopt($curl, CURLOPT_FILE, $file_handle); |
||
| 87 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
||
| 88 | curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); |
||
| 89 | curl_setopt($curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); |
||
| 90 | |||
| 91 | curl_exec($curl); |
||
| 92 | |||
| 93 | $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
| 94 | |||
| 95 | curl_close($curl); |
||
| 96 | |||
| 97 | $response = [ |
||
| 98 | 'statusCode' => $statusCode, |
||
| 99 | ]; |
||
| 100 | |||
| 101 | if ($response['statusCode'] >= 400) { |
||
| 102 | switch ($response['statusCode']) { |
||
| 103 | case 400 : |
||
| 104 | throw new BadRequest('Bad request'); |
||
| 105 | |||
| 106 | case 401 : |
||
| 107 | throw new NotAuthenticated('Not authenticated'); |
||
| 108 | |||
| 109 | case 402 : |
||
| 110 | throw new PaymentRequired('Payment required'); |
||
| 111 | |||
| 112 | case 403 : |
||
| 113 | throw new Forbidden('Forbidden'); |
||
| 114 | |||
| 115 | case 404: |
||
| 116 | throw new NotFound('Resource not found.'); |
||
| 117 | |||
| 118 | case 405 : |
||
| 119 | throw new MethodNotAllowed('Method not allowed'); |
||
| 120 | |||
| 121 | case 409 : |
||
| 122 | throw new Conflict('Conflict'); |
||
| 123 | |||
| 124 | case 412 : |
||
| 125 | throw new PreconditionFailed('Precondition failed'); |
||
| 126 | |||
| 127 | case 416 : |
||
| 128 | throw new RequestedRangeNotSatisfiable('Requested Range Not Satisfiable'); |
||
| 129 | |||
| 130 | case 500 : |
||
| 131 | throw new Exception('Internal server error'); |
||
| 132 | |||
| 133 | case 501 : |
||
| 134 | throw new NotImplemented('Not Implemented'); |
||
| 135 | |||
| 136 | case 507 : |
||
| 137 | throw new InsufficientStorage('Insufficient storage'); |
||
| 138 | |||
| 139 | default: |
||
| 140 | throw new Exception('HTTP error response. (errorcode ' . $response['statusCode'] . ')'); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | return $response; |
||
| 145 | } |
||
| 151 |