Completed
Pull Request — master (#12)
by
unknown
03:59
created

CurlWebLoader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 11.83 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.94%

Importance

Changes 0
Metric Value
dl 11
loc 93
ccs 31
cts 33
cp 0.9394
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 3
A load() 0 17 4
A getResponseBodyAndStatusCode() 0 7 1
A getDefaultCurlOptions() 0 8 1
A setCurlOptions() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace League\JsonReference\Loader;
4
5
use League\JsonReference;
6
use League\JsonReference\DecoderManager;
7
use League\JsonReference\DecoderInterface;
8
use League\JsonReference\LoaderInterface;
9
10
final class CurlWebLoader implements LoaderInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $prefix;
16
17
    /**
18
     * @var array
19
     */
20
    private $curlOptions;
21
22
    /**
23
     * @var DecoderManager
24
     */
25
    private $decoders;
26
27
    /**
28
     * @param string                              $prefix
29
     * @param array                               $curlOptions
30
     * @param JsonDecoderInterface|DecoderManager $decoders
31
     */
32 72 View Code Duplication
    public function __construct($prefix, array $curlOptions = null, $decoders = null)
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...
33
    {
34 72
        $this->prefix = $prefix;
35 72
        $this->setCurlOptions($curlOptions);
36
        
37 72
        if ($decoders instanceof DecoderInterface) {
38
            $this->decoders = new DecoderManager([$decoders]);
39
        } else {
40 72
            $this->decoders = $decoders ?: new DecoderManager();
0 ignored issues
show
Documentation Bug introduced by
It seems like $decoders ?: new \League...erence\DecoderManager() can also be of type object<League\JsonRefere...r\JsonDecoderInterface>. However, the property $decoders is declared as type object<League\JsonReference\DecoderManager>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
41
        }
42 72
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 16
    public function load($path, $defaultExtension = 'json')
48
    {
49 16
        $uri = $this->prefix . $path;
50
        
51 16
        $extension = isset(pathinfo($path)['extension']) ? pathinfo($path)['extension'] : $defaultExtension;
52
        
53 16
        $ch = curl_init($uri);
54 16
        curl_setopt_array($ch, $this->curlOptions);
55 16
        list($response, $statusCode) = $this->getResponseBodyAndStatusCode($ch);
56 16
        curl_close($ch);
57
58 16
        if ($statusCode >= 400 || !$response) {
59 4
            throw JsonReference\SchemaLoadingException::create($uri);
60
        }
61
62 12
        return $this->decoders->getDecoder($extension)->decode($response);
63
    }
64
65
    /**
66
     * @param resource $ch
67
     *
68
     * @return array
69
     */
70 16
    private function getResponseBodyAndStatusCode($ch)
71
    {
72 16
        $response   = curl_exec($ch);
73 16
        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
74
75 16
        return [$response, $statusCode];
76
    }
77
78
    /**
79
     * @return array
80
     */
81 72
    private function getDefaultCurlOptions()
82
    {
83
        return [
84 72
            CURLOPT_RETURNTRANSFER => true,
85 72
            CURLOPT_FOLLOWLOCATION => true,
86 72
            CURLOPT_MAXREDIRS      => 20,
87 36
        ];
88
    }
89
90
    /**
91
     * @param array|null $curlOptions
92
     */
93 72
    private function setCurlOptions($curlOptions)
94
    {
95 72
        if (is_array($curlOptions)) {
96 2
            $this->curlOptions = $curlOptions + $this->getDefaultCurlOptions();
97 2
            return;
98
        }
99
100 70
        $this->curlOptions = $this->getDefaultCurlOptions();
101 70
    }
102
}
103