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