Pool::buildUrl()   B
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
dl 0
loc 28
rs 8.4444
c 0
b 0
f 0
nc 7
nop 1
1
<?php
2
namespace PHPDaemon\Clients\HTTP;
3
4
use PHPDaemon\Network\Client;
5
6
/**
7
 * @package    NetworkClients
8
 * @subpackage HTTPClient
9
 * @author     Vasily Zorin <[email protected]>
10
 */
11
class Pool extends Client
12
{
13
    /**
14
     * Perform a HEAD request
15
     * @param string $url
16
     * @param array $params
17
     * @param callable $resultcb
0 ignored issues
show
Bug introduced by
There is no parameter named $resultcb. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
18
     * @call  ( url $url, array $params )
19
     * @call  ( url $url, callable $resultcb )
20
     * @callback $resultcb ( Connection $conn, boolean $success )
21
     */
22 View Code Duplication
    public function head($url, $params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
    {
24
        if (is_callable($params)) {
25
            $params = ['resultcb' => $params];
26
        }
27
        if (!isset($params['uri']) || !isset($params['host'])) {
28
            list($params['scheme'], $params['host'], $params['uri'], $params['port']) = static::parseUrl($url);
29
        }
30
        if (isset($params['connect'])) {
31
            $dest = $params['connect'];
32
        } elseif (isset($params['proxy']) && $params['proxy']) {
33
            if ($params['proxy']['type'] === 'http') {
34
                $dest = 'tcp://' . $params['proxy']['addr'];
35
            }
36
        } else {
37
            $dest = 'tcp://' . $params['host'] . (isset($params['port']) ? ':' . $params['port'] : null) . ($params['scheme'] === 'https' ? '#ssl' : '');
38
        }
39
        $this->getConnection($dest, function ($conn) use ($url, $params) {
0 ignored issues
show
Bug introduced by
The variable $dest does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
40
            if (!$conn->isConnected()) {
41
                $params['resultcb'](false);
42
                return;
43
            }
44
            $conn->head($url, $params);
45
        });
46
    }
47
48
    /**
49
     * Parse URL
50
     * @param string $mixed Look Pool::buildUrl()
51
     * @call  ( string $str )
52
     * @call  ( array $mixed )
53
     * @return array|bool
54
     */
55
    public static function parseUrl($mixed)
56
    {
57
        $url = static::buildUrl($mixed);
58
        if (false === $url) {
59
            return false;
60
        }
61
        $u = parse_url($url);
62
        $uri = '';
63
        if (!isset($u['path']) && isset($u['query'])) {
64
            $u['path'] = '/';
65
        }
66
        if (isset($u['path'])) {
67
            $uri .= $u['path'];
68
            if (isset($u['query'])) {
69
                $uri .= '?' . $u['query'];
70
            }
71
        }
72
        return [$u['scheme'], $u['host'], $uri, isset($u['port']) ? $u['port'] : null];
73
    }
74
75
    /**
76
     * Builds URL from array
77
     * @param string $mixed
78
     * @call  ( string $str )
79
     * @call  ( array $mixed )
80
     * @return string|false
81
     */
82
    public static function buildUrl($mixed)
83
    {
84
        if (is_string($mixed)) {
85
            return $mixed;
86
        }
87
        if (!is_array($mixed)) {
88
            return false;
89
        }
90
        $url = '';
91
        $buf = [];
92
        $queryDelimiter = '?';
93
        $mixed[] = '';
94
        foreach ($mixed as $k => $v) {
95
            if (is_int($k) || ctype_digit($k)) {
96
                if (sizeof($buf) > 0) {
97
                    if (mb_orig_strpos($url, '?') !== false) {
98
                        $queryDelimiter = '&';
99
                    }
100
                    $url .= $queryDelimiter . http_build_query($buf);
101
                    $queryDelimiter = '';
102
                }
103
                $url .= $v;
104
            } else {
105
                $buf[$k] = $v;
106
            }
107
        }
108
        return $url;
109
    }
110
111
    /**
112
     * Perform a GET request
113
     * @param string $url
114
     * @param array $params
115
     * @param callable $resultcb
0 ignored issues
show
Bug introduced by
There is no parameter named $resultcb. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
116
     * @call  ( url $url, array $params )
117
     * @call  ( url $url, callable $resultcb )
118
     * @callback $resultcb ( Connection $conn, boolean $success )
119
     */
120 View Code Duplication
    public function get($url, $params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
    {
122
        if (is_callable($params)) {
123
            $params = ['resultcb' => $params];
124
        }
125
        if (!isset($params['uri']) || !isset($params['host'])) {
126
            list($params['scheme'], $params['host'], $params['uri'], $params['port']) = static::parseUrl($url);
127
        }
128
        if (isset($params['connect'])) {
129
            $dest = $params['connect'];
130
        } elseif (isset($params['proxy']) && $params['proxy']) {
131
            if ($params['proxy']['type'] === 'http') {
132
                $dest = 'tcp://' . $params['proxy']['addr'];
133
            }
134
        } else {
135
            $dest = 'tcp://' . $params['host'] . (isset($params['port']) ? ':' . $params['port'] : null) . ($params['scheme'] === 'https' ? '#ssl' : '');
136
        }
137
        $this->getConnection($dest, function ($conn) use ($url, $params) {
0 ignored issues
show
Bug introduced by
The variable $dest does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
138
            if (!$conn->isConnected()) {
139
                $params['resultcb'](false);
140
                return;
141
            }
142
            $conn->get($url, $params);
143
        });
144
    }
145
146
    /**
147
     * Perform a POST request
148
     * @param string $url
149
     * @param array $data
150
     * @param array $params
151
     * @param callable $resultcb
0 ignored issues
show
Bug introduced by
There is no parameter named $resultcb. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
152
     * @param \Closure $beforeConnect
0 ignored issues
show
Documentation introduced by
Should the type for parameter $beforeConnect not be null|\Closure?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
153
     * @call  ( url $url, array $data, array $params )
154
     * @call  ( url $url, array $data, callable $resultcb )
155
     * @callback $resultcb ( Connection $conn, boolean $success )
156
     */
157
    public function post($url, $data, $params, \Closure $beforeConnect = null)
158
    {
159
        if (!$data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
160
            $data = [];
161
        }
162
163
        if (is_callable($params)) {
164
            $params = ['resultcb' => $params];
165
        }
166
        if (!isset($params['uri']) || !isset($params['host'])) {
167
            list($params['scheme'], $params['host'], $params['uri'], $params['port']) = static::parseUrl($url);
168
        }
169
        if (isset($params['connect'])) {
170
            $dest = $params['connect'];
171
        } elseif (isset($params['proxy']) && $params['proxy']) {
172
            if ($params['proxy']['type'] === 'http') {
173
                $dest = 'tcp://' . $params['proxy']['addr'];
174
            }
175
        } else {
176
            $dest = 'tcp://' . $params['host'] . (isset($params['port']) ? ':' . $params['port'] : null) . ($params['scheme'] === 'https' ? '#ssl' : '');
177
        }
178
        $this->getConnection($dest, function ($conn) use ($url, $data, $params) {
0 ignored issues
show
Bug introduced by
The variable $dest does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
179
            if (!$conn->isConnected()) {
180
                $params['resultcb'](false);
181
                return;
182
            }
183
            $conn->post($url, $data, $params);
184
        }, 0, $beforeConnect);
185
    }
186
187
    /**
188
     * Setting default config options
189
     * Overriden from NetworkClient::getConfigDefaults
190
     * @return array|bool
191
     */
192
    protected function getConfigDefaults()
193
    {
194
        return [
195
            /* [integer] Default port */
196
            'port' => 80,
197
198
            /* [integer] Default SSL port */
199
            'sslport' => 443,
200
201
            /* [boolean] Send User-Agent header? */
202
            'expose' => 1,
203
        ];
204
    }
205
}
206