Completed
Pull Request — master (#101)
by Hiraku
05:27
created

CurlRemoteFilesystem   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 14
Bugs 4 Features 0
Metric Value
wmc 8
c 14
b 4
f 0
lcom 1
cbo 3
dl 0
loc 51
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getContents() 0 15 3
A getLastHeaders() 0 8 3
A __debugInfo() 0 4 1
1
<?php
2
/*
3
 * hirak/prestissimo
4
 * @author Hiraku NAKANO
5
 * @license MIT https://github.com/hirak/prestissimo
6
 */
7
namespace Hirak\Prestissimo;
8
9
use Composer\Config;
10
use Composer\IO;
11
use Composer\Downloader;
12
use Composer\Util;
13
14
class CurlRemoteFilesystem extends Util\RemoteFilesystem
15
{
16
    protected $io, $config, $options, $disableTls;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
17
18
    private $req;
19
20
    /**
21
     * @inheritDoc
22
     */
23
    public function __construct(IO\IOInterface $io, Config $config = null, array $options = array(), $disableTls = false)
24
    {
25
        $this->io = $io;
26
        $this->config = $config;
27
        $this->options = $options;
28
        $this->disableTls = $disableTls;
29
        parent::__construct($io, $config, $options, $disableTls);
30
    }
31
32
    /**
33
     * @inheritDoc
34
     */
35
    public function getContents($originUrl, $fileUrl, $progress = true, $options = array())
36
    {
37
        $res = null;
38
        try {
39
            $this->req = new FetchRequest($fileUrl, $this->io, $this->config);
0 ignored issues
show
Bug introduced by
It seems like $this->config can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
40
            $res = $this->req->fetch();
41
        } catch (\Exception $e) {
42
            $this->io->writeError((string)$e);
43
        }
44
        if ($res) {
45
            return $res;
46
        } else {
47
            return parent::getContents($originUrl, $fileUrl, $progress, $options);
48
        }
49
    }
50
51
    public function getLastHeaders()
52
    {
53
        if ($this->req && ($headers = $this->req->getLastHeaders())) {
54
            return $headers;
55
        } else {
56
            return parent::getLastHeaders();
57
        }
58
    }
59
60
    public function __debugInfo()
61
    {
62
        return array();
63
    }
64
}
65