| Conditions | 24 |
| Paths | 6912 |
| Total Lines | 123 |
| Code Lines | 64 |
| Lines | 0 |
| Ratio | 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 |
||
| 232 | public function MakeRequest( $pCanonizedPath, $pMethod = 'GET', $params = array(), $ch = null ) {
|
||
| 233 | if ( !FS_SDK__HAS_CURL ) {
|
||
| 234 | $this->ThrowNoCurlException(); |
||
| 235 | } |
||
| 236 | |||
| 237 | // Connectivity errors simulation. |
||
| 238 | if ( FS_SDK__SIMULATE_NO_API_CONNECTIVITY_CLOUDFLARE ) {
|
||
| 239 | $this->ThrowCloudFlareDDoSException(); |
||
| 240 | } else if ( FS_SDK__SIMULATE_NO_API_CONNECTIVITY_SQUID_ACL ) {
|
||
| 241 | $this->ThrowSquidAclException(); |
||
| 242 | } |
||
| 243 | |||
| 244 | if ( ! $ch ) {
|
||
| 245 | $ch = curl_init(); |
||
| 246 | } |
||
| 247 | |||
| 248 | $opts = self::$CURL_OPTS; |
||
| 249 | |||
| 250 | if ( ! isset( $opts[ CURLOPT_HTTPHEADER ] ) || ! is_array( $opts[ CURLOPT_HTTPHEADER ] ) ) {
|
||
| 251 | $opts[ CURLOPT_HTTPHEADER ] = array(); |
||
| 252 | } |
||
| 253 | |||
| 254 | if ( 'POST' === $pMethod || 'PUT' === $pMethod ) {
|
||
| 255 | if ( is_array( $params ) && 0 < count( $params ) ) {
|
||
| 256 | $opts[ CURLOPT_HTTPHEADER ][] = 'Content-Type: application/json'; |
||
| 257 | $opts[ CURLOPT_POST ] = count( $params ); |
||
| 258 | $opts[ CURLOPT_POSTFIELDS ] = json_encode( $params ); |
||
| 259 | } |
||
| 260 | |||
| 261 | $opts[ CURLOPT_RETURNTRANSFER ] = true; |
||
| 262 | } |
||
| 263 | |||
| 264 | $opts[ CURLOPT_URL ] = $this->GetUrl( $pCanonizedPath ); |
||
| 265 | $opts[ CURLOPT_CUSTOMREQUEST ] = $pMethod; |
||
| 266 | |||
| 267 | $resource = explode( '?', $pCanonizedPath ); |
||
| 268 | |||
| 269 | // Only sign request if not ping.json connectivity test. |
||
| 270 | if ( '/v1/ping.json' !== strtolower( substr( $resource[0], - strlen( '/v1/ping.json' ) ) ) ) {
|
||
| 271 | $this->SignRequest( $resource[0], $opts ); |
||
| 272 | } |
||
| 273 | |||
| 274 | // disable the 'Expect: 100-continue' behaviour. This causes CURL to wait |
||
| 275 | // for 2 seconds if the server does not support this header. |
||
| 276 | $opts[ CURLOPT_HTTPHEADER ][] = 'Expect:'; |
||
| 277 | |||
| 278 | if ( 'https' === substr( strtolower( $pCanonizedPath ), 0, 5 ) ) {
|
||
| 279 | $opts[ CURLOPT_SSL_VERIFYHOST ] = false; |
||
| 280 | $opts[ CURLOPT_SSL_VERIFYPEER ] = false; |
||
| 281 | } |
||
| 282 | |||
| 283 | curl_setopt_array( $ch, $opts ); |
||
| 284 | $result = curl_exec( $ch ); |
||
| 285 | |||
| 286 | /*if (curl_errno($ch) == 60) // CURLE_SSL_CACERT |
||
| 287 | {
|
||
| 288 | self::errorLog('Invalid or no certificate authority found, using bundled information');
|
||
| 289 | curl_setopt($ch, CURLOPT_CAINFO, |
||
| 290 | dirname(__FILE__) . '/fb_ca_chain_bundle.crt'); |
||
| 291 | $result = curl_exec($ch); |
||
| 292 | }*/ |
||
| 293 | |||
| 294 | // With dual stacked DNS responses, it's possible for a server to |
||
| 295 | // have IPv6 enabled but not have IPv6 connectivity. If this is |
||
| 296 | // the case, curl will try IPv4 first and if that fails, then it will |
||
| 297 | // fall back to IPv6 and the error EHOSTUNREACH is returned by the |
||
| 298 | // operating system. |
||
| 299 | if ( false === $result && empty( $opts[ CURLOPT_IPRESOLVE ] ) ) {
|
||
| 300 | $matches = array(); |
||
| 301 | $regex = '/Failed to connect to ([^:].*): Network is unreachable/'; |
||
| 302 | if ( preg_match( $regex, curl_error( $ch ), $matches ) ) {
|
||
| 303 | if ( strlen( @inet_pton( $matches[1] ) ) === 16 ) {
|
||
| 304 | // self::errorLog('Invalid IPv6 configuration on server, Please disable or get native IPv6 on your server.');
|
||
| 305 | self::$CURL_OPTS[ CURLOPT_IPRESOLVE ] = CURL_IPRESOLVE_V4; |
||
| 306 | curl_setopt( $ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); |
||
| 307 | $result = curl_exec( $ch ); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | if ( $result === false ) {
|
||
| 313 | $e = new Freemius_Exception( array( |
||
| 314 | 'error' => array( |
||
| 315 | 'code' => curl_errno( $ch ), |
||
| 316 | 'message' => curl_error( $ch ), |
||
| 317 | 'type' => 'CurlException', |
||
| 318 | ), |
||
| 319 | ) ); |
||
| 320 | |||
| 321 | curl_close( $ch ); |
||
| 322 | throw $e; |
||
| 323 | } |
||
| 324 | |||
| 325 | curl_close( $ch ); |
||
| 326 | |||
| 327 | if (empty($result)) |
||
| 328 | return null; |
||
| 329 | |||
| 330 | $decoded = json_decode( $result ); |
||
| 331 | |||
| 332 | if ( is_null( $decoded ) ) {
|
||
| 333 | if ( preg_match( '/Please turn JavaScript on/i', $result ) && |
||
| 334 | preg_match( '/text\/javascript/', $result ) |
||
| 335 | ) {
|
||
| 336 | $this->ThrowCloudFlareDDoSException( $result ); |
||
| 337 | } else if ( preg_match( '/Access control configuration prevents your request from being allowed at this time. Please contact your service provider if you feel this is incorrect./', $result ) && |
||
| 338 | preg_match( '/squid/', $result ) |
||
| 339 | ) {
|
||
| 340 | $this->ThrowSquidAclException( $result ); |
||
| 341 | } else {
|
||
| 342 | $decoded = (object) array( |
||
| 343 | 'error' => (object) array( |
||
| 344 | 'type' => 'Unknown', |
||
| 345 | 'message' => $result, |
||
| 346 | 'code' => 'unknown', |
||
| 347 | 'http' => 402 |
||
| 348 | ) |
||
| 349 | ); |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | return $decoded; |
||
| 354 | } |
||
| 355 | |||
| 403 | } |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.