Completed
Pull Request — master (#20)
by
unknown
13:54
created

FileGetContentsWebLoader   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 13.51 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 10
loc 74
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 3
A load() 0 17 2
A parseHttpResponseHeader() 0 17 4

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\DecoderManager;
6
use League\JsonReference\DecoderInterface;
7
use League\JsonReference\LoaderInterface;
8
use League\JsonReference\SchemaLoadingException;
9
use function League\JsonReference\determineMediaType;
10
11
final class FileGetContentsWebLoader implements LoaderInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    private $prefix;
17
18
    /**
19
     * @var DecoderManager
20
     */
21
    private $decoderManager;
22
23
    /**
24
     * @param string $prefix
25
     * @param DecoderInterface|DecoderManager $decoders
0 ignored issues
show
Bug introduced by
There is no parameter named $decoders. 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...
26 6
     */
27 View Code Duplication
    public function __construct($prefix, $decoderManager = 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...
28 6
    {
29 6
        $this->prefix = $prefix;
30 6
31
        if ($decoderManager instanceof DecoderInterface) {
32
            $this->decoderManager = new DecoderManager([null => $decoderManager]);
33
        } else {
34
            $this->decoderManager = $decoderManager ?: new DecoderManager();
35 6
        }
36
    }
37 6
38 6
    /**
39 2
     * {@inheritdoc}
40 6
     */
41 3
    public function load($path)
42 2
    {
43
        $uri = $this->prefix . $path;
44 2
        set_error_handler(function () use ($uri) {
45 1
            throw SchemaLoadingException::create($uri);
46
        });
47
        $response = file_get_contents($uri);
48 1
        restore_error_handler();
49
50
        if (!$response) {
51
            throw SchemaLoadingException::create($uri);
52
        }
53
54
        $headers = $this->parseHttpResponseHeader($http_response_header);
55
        $type    = determineMediaType(['headers' => $headers, 'uri' => $uri]);
56
        return $this->decoderManager->getDecoder($type)->decode($response);
57
    }
58
59
    /**
60
     * Parse http headers returned by $http_response_header
61
     * @link http://php.net/manual/en/reserved.variables.httpresponseheader.php
62
     *
63
     * @param array $headers
64
     *
65
     * @return array
66
     */
67
    public static function parseHttpResponseHeader($headers)
68
    {
69
        $head = array();
70
        foreach ($headers as $k => $v) {
71
            $t = explode(':', $v, 2);
72
            if (isset($t[1])) {
73
                $head[ trim($t[0]) ] = trim($t[1]);
74
            } else {
75
                $head[] = $v;
76
                if (preg_match("#HTTP/[0-9\.]+\s+([0-9]+)#", $v, $out)) {
77
                    $head['response_code'] = intval($out[1]);
78
                }
79
            }
80
        }
81
82
        return $head;
83
    }
84
}
85