| Conditions | 7 |
| Paths | 15 |
| Total Lines | 62 |
| Code Lines | 36 |
| 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 |
||
| 108 | protected function _send_request($method, $uri, $data) |
||
| 109 | { |
||
| 110 | $ch = $this->_create_handle(); |
||
| 111 | curl_setopt($ch, CURLOPT_URL, $this->_url.$uri); |
||
| 112 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
| 113 | |||
| 114 | if ($method == 'POST') |
||
| 115 | { |
||
| 116 | if (is_array($data)) |
||
| 117 | { |
||
| 118 | $data_str = json_encode($data); |
||
| 119 | } else |
||
| 120 | { |
||
| 121 | $data_str = ''; |
||
| 122 | } |
||
| 123 | |||
| 124 | curl_setopt($ch, CURLOPT_POST, true); |
||
| 125 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data_str); |
||
| 126 | } else |
||
| 127 | { |
||
| 128 | curl_setopt($ch, CURLOPT_POST, false); |
||
| 129 | } |
||
| 130 | |||
| 131 | curl_setopt( |
||
| 132 | $ch, |
||
| 133 | CURLOPT_HTTPHEADER, |
||
| 134 | array( |
||
| 135 | 'Expect: ', |
||
| 136 | 'Content-Type: application/json' |
||
| 137 | ) |
||
| 138 | ); |
||
| 139 | |||
| 140 | curl_setopt($ch, CURLOPT_USERPWD, "$this->_user:$this->_password"); |
||
| 141 | $response = curl_exec($ch); |
||
| 142 | if ($response === false) |
||
| 143 | { |
||
| 144 | throw new TestRailAPIException(curl_error($ch)); |
||
| 145 | } |
||
| 146 | |||
| 147 | if ($response) |
||
| 148 | { |
||
| 149 | $result = json_decode($response, true); // As array |
||
|
|
|||
| 150 | } else |
||
| 151 | { |
||
| 152 | $result = array(); |
||
| 153 | } |
||
| 154 | |||
| 155 | $info = curl_getinfo($ch); |
||
| 156 | if ($info['http_code'] != 200) |
||
| 157 | { |
||
| 158 | throw new TestRailAPIException( |
||
| 159 | sprintf( |
||
| 160 | 'TestRail API returned HTTP %s (%s)', |
||
| 161 | $info['http_code'], |
||
| 162 | isset($result['error']) ? |
||
| 163 | '"'.$result['error'].'"' : 'No additional error message received' |
||
| 164 | ) |
||
| 165 | ); |
||
| 166 | } |
||
| 167 | |||
| 168 | $this->_close_handle($ch); |
||
| 169 | return $result; |
||
| 170 | } |
||
| 176 | } |