Conditions | 19 |
Paths | 144 |
Total Lines | 94 |
Code Lines | 67 |
Lines | 42 |
Ratio | 44.68 % |
Changes | 2 | ||
Bugs | 0 | 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 |
||
50 | public function request(\Communique\RESTClientRequest $request) { |
||
51 | $headers = array(); |
||
52 | foreach ($request->headers as $header_key => $header_value) { |
||
53 | $headers[] = $header_key . ': ' . $header_value; |
||
54 | } |
||
55 | |||
56 | $this->curl->setopt_array(array( |
||
57 | CURLOPT_URL => $request->url, |
||
58 | CURLOPT_RETURNTRANSFER => true, |
||
59 | CURLOPT_HEADER => true, |
||
60 | CURLOPT_HTTPHEADER => $headers |
||
61 | )); |
||
62 | |||
63 | $this->curl->setopt(CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); |
||
64 | |||
65 | switch ($request->method) { |
||
66 | View Code Duplication | case 'POST': |
|
67 | $this->curl->setopt_array( |
||
68 | array( |
||
69 | CURLOPT_POST => true, |
||
70 | CURLOPT_POSTFIELDS => $request->payload |
||
71 | ) |
||
72 | ); |
||
73 | break; |
||
74 | |||
75 | View Code Duplication | case 'PUT': |
|
76 | $this->curl->setopt_array( |
||
77 | array( |
||
78 | CURLOPT_PUT => true, |
||
79 | CURLOPT_POSTFIELDS => $request->payload |
||
80 | ) |
||
81 | ); |
||
82 | break; |
||
83 | |||
84 | View Code Duplication | case 'DELETE': |
|
85 | if ($payload = http_build_query($request->payload)) { |
||
86 | $payload = '?' . $payload; |
||
87 | } else { |
||
88 | $payload = ''; |
||
89 | } |
||
90 | $this->curl->setopt_array( |
||
91 | array( |
||
92 | CURLOPT_CUSTOMREQUEST => 'DELETE', |
||
93 | CURLOPT_URL => $request->url . $payload |
||
94 | ) |
||
95 | ); |
||
96 | break; |
||
97 | |||
98 | default: |
||
99 | View Code Duplication | case 'GET': |
|
100 | if ($payload = http_build_query($request->payload)) { |
||
101 | $payload = '?' . $payload; |
||
102 | } else { |
||
103 | $payload = ''; |
||
104 | } |
||
105 | $this->curl->setopt_array( |
||
106 | array( |
||
107 | CURLOPT_HTTPGET => true, |
||
108 | CURLOPT_URL => $request->url . $payload |
||
109 | ) |
||
110 | ); |
||
111 | break; |
||
112 | } |
||
113 | |||
114 | $raw_response = $this->curl->exec(); |
||
115 | |||
116 | if (!$raw_response) { |
||
117 | $curl_error = $this->curl->errno(); |
||
118 | switch ($curl_error) { |
||
119 | case CURLE_SSL_PEER_CERTIFICATE: |
||
120 | case CURLE_SSL_ENGINE_NOTFOUND: |
||
121 | case CURLE_SSL_ENGINE_SETFAILED: |
||
122 | case CURLE_SSL_CERTPROBLEM: |
||
123 | case CURLE_SSL_CIPHER: |
||
124 | case CURLE_SSL_CACERT: |
||
125 | case CURLE_SSL_CONNECT_ERROR: |
||
126 | throw new \Communique\CommuniqueRESTSSLException('cURL SSL Error: ' . $this->curl->error() . ' cURL Error Code: ' . $curl_error); |
||
127 | |||
128 | case CURLE_UNSUPPORTED_PROTOCOL: |
||
129 | case CURLE_COULDNT_CONNECT: |
||
130 | case CURLE_COULDNT_RESOLVE_HOST: |
||
131 | throw new \Communique\CommuniqueRESTConnectionException('cURL Error: ' . $this->curl->error() . ' cURL Error Code: ' . $curl_error); |
||
132 | |||
133 | default: |
||
134 | throw new \Communique\CommuniqueRESTException('cURL Error: ' . $this->curl->error() . ' cURL Error Code: ' . $curl_error); |
||
135 | } |
||
136 | } else { |
||
137 | return new \Communique\RESTClientResponse( |
||
138 | $raw_response['http_status_code'], |
||
139 | $raw_response['body'], |
||
140 | $raw_response['headers'] |
||
141 | ); |
||
142 | } |
||
143 | } |
||
144 | } |