Completed
Pull Request — master (#2)
by
unknown
08:22
created

CurlRequest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 85
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUrl() 0 4 1
A close() 0 4 1
A execute() 0 9 2
A getInfo() 0 12 2
A setOption() 0 5 1
A __get() 0 10 2
1
<?php
2
3
namespace vfalies\tmdb;
4
5
class CurlRequest implements Interfaces\HttpRequestInterface
6
{
7
8
    private $handle = null;
9
10
    /**
11
     * Set the url to cUrl call
12
     * @param string $url
13
     * @throws Exception
14
     */
15 4
    public function setUrl(string $url): void
16
    {
17 4
        $this->handle = curl_init($url);
18 4
    }
19
20
    /**
21
     * Close cUrl handle
22
     */
23 1
    public function close(): void
24
    {
25 1
        curl_close($this->handle);
26 1
    }
27
28
    /**
29
     * Execute cUrl call
30
     * @return mixed
31
     * @throws \Exception
32
     */
33 2
    public function execute()
34
    {
35 2
        $result = curl_exec($this->handle);
36 2
        if ($result === false)
37
        {
38 1
            throw new \Exception('cUrl failed : '.var_export($this->getInfo(), true), 1004);
39
        }
40 1
        return $result;
41
    }
42
43
    /**
44
     * Get cUrl infos
45
     * @param string $name
46
     * @return mixed
47
     * @throws Exception
48
     */
49 2
    public function getInfo(string $name = '')
50
    {
51 2
        if (empty($name))
52
        {
53 2
            $info = curl_getinfo($this->handle);
54
        }
55
        else
56
        {
57 1
            $info = curl_getinfo($this->handle, $name);
58
        }
59 2
        return $info;
60
    }
61
62
    /**
63
     * Set cUrl option
64
     * @param string $name
65
     * @param string $value
66
     * @throws \Exception
67
     */
68 3
    public function setOption(string $name, string $value): Interfaces\HttpRequestInterface
69
    {
70 3
        curl_setopt($this->handle, $name, $value);
71 3
        return $this;
72
    }
73
74
    /**
75
     * Magical getter
76
     * @param string $name
77
     * @return mixed
78
     */
79 1
    public function __get(string $name)
80
    {
81
        switch ($name)
82
        {
83 1
            case 'handle':
84 1
                $response = $this->$name;
85 1
                break;
86
        }
87 1
        return $response;
0 ignored issues
show
Bug introduced by
The variable $response 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...
88
    }
89
}
90