Completed
Push — master ( 78dfa0...3cbabb )
by Stefano
03:27
created

CURL::request()   C

Complexity

Conditions 12
Paths 32

Size

Total Lines 53
Code Lines 43

Duplication

Lines 20
Ratio 37.74 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 43
c 1
b 0
f 0
nc 32
nop 1
dl 20
loc 53
rs 6.3525

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * HTTP\Driver
5
 *
6
 * Core\HTTP\Driver\CURL cURL HTTP Driver.
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2015-2016 - http://caffeina.com
11
 */
12
13
namespace HTTP;
14
15
class CURL implements Driver\Adapter {
16
17
  public function request(HTTP\Request $request){
18
    $http_method = strtoupper($method);
0 ignored issues
show
Bug introduced by
The variable $method does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
19
    $ch  = curl_init($url);
0 ignored issues
show
Bug introduced by
The variable $url does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
20
    $opt = [
21
      CURLOPT_CUSTOMREQUEST   => $http_method,
22
      CURLOPT_SSL_VERIFYHOST  => false,
23
      CURLOPT_CONNECTTIMEOUT  => 10,
24
      CURLOPT_RETURNTRANSFER  => true,
25
      CURLOPT_USERAGENT       => static::$UA,
26
      CURLOPT_HEADER          => false,
27
      CURLOPT_MAXREDIRS       => 10,
28
      CURLOPT_FOLLOWLOCATION  => true,
29
      CURLOPT_ENCODING        => '',
30
    ];
31
32
    if($username && $password) {
33
      $opt[CURLOPT_USERPWD] = "$username:$password";
0 ignored issues
show
Bug introduced by
The variable $username does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $password does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
34
    }
35
36
    $headers = array_merge($headers,static::$headers);
0 ignored issues
show
Bug introduced by
The variable $headers seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
37
38 View Code Duplication
    if($http_method == 'GET'){
39
        if($data && is_array($data)){
40
          $tmp                       = [];
41
          $queried_url               = $url;
42
          foreach($data as $key=>$val) $tmp[] = $key.'='.$val;
0 ignored issues
show
Bug introduced by
The variable $data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
43
          $queried_url               .= (strpos($queried_url,'?') === false) ? '?' : '&';
44
          $queried_url               .= implode('&',$tmp);
45
          $opt[CURLOPT_URL]          = $queried_url;
46
          $opt[CURLOPT_HTTPGET]      = true;
47
          unset($opt[CURLOPT_CUSTOMREQUEST]);
48
        }
49
    } else {
50
        $opt[CURLOPT_CUSTOMREQUEST]  = $http_method;
51
        if($data_as_json or is_object($data)){
0 ignored issues
show
Bug introduced by
The variable $data_as_json does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
52
          $headers['Content-Type']   = 'application/json';
53
          $opt[CURLOPT_POSTFIELDS]   = json_encode($data);
54
        } else {
55
          $opt[CURLOPT_POSTFIELDS]   = http_build_query($data);
56
        }
57
    }
58
59
    curl_setopt_array($ch,$opt);
60
    $_harr = [];
61
    foreach($headers as $key=>$val)  $_harr[] = $key.': '.$val;
62
    curl_setopt($ch, CURLOPT_HTTPHEADER, $_harr);
63
    $result = curl_exec($ch);
64
    $contentType = strtolower(curl_getinfo($ch, CURLINFO_CONTENT_TYPE));
65
    static::$last_info = curl_getinfo($ch);
66
    if(false !== strpos($contentType,'json')) $result = json_decode($result);
67
    curl_close($ch);
68
    return $result;
69
  }
70
71
  public static function valid(){
72
    return function_exists('curl_init');
73
  }
74
75
}
76