GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

RemoteProvider::curl()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 8.439
cc 6
eloc 24
nc 12
nop 6
1
<?php
2
3
namespace Kunstmaan\Skylab\Provider;
4
5
use Cilex\Application;
6
use Kunstmaan\Skylab\Exceptions\AccessDeniedException;
7
8
class RemoteProvider extends AbstractProvider
9
{
10
11
    /**
12
     * Registers services on the given app.
13
     *
14
     * @param Application $app An Application instance
15
     */
16
    public function register(Application $app)
17
    {
18
        $app['remote'] = $this;
19
        $this->app = $app;
20
    }
21
22
    /**
23
     * @param $url
24
     * @param  string $contentType
0 ignored issues
show
Documentation introduced by
Should the type for parameter $contentType not be string|null?

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...
25
     * @param  string $filename
0 ignored issues
show
Documentation introduced by
Should the type for parameter $filename not be string|null?

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...
26
     * @return string
27
     */
28
    public function curl($url, $contentType = null, $filename = null, $cacheTimeInSeconds = 0, $username = null, $password = null)
0 ignored issues
show
Unused Code introduced by
The parameter $cacheTimeInSeconds is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
29
    {
30
        $ch = curl_init();
31
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
32
        curl_setopt($ch, CURLOPT_HEADER, false);
33
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
34
        curl_setopt($ch, CURLOPT_URL, $url);
35
        curl_setopt($ch, CURLOPT_REFERER, $url);
36
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
37
        if($username && $password){
38
            curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
39
        }
40
        if ($contentType) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $contentType of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
41
            curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: " . $contentType));
42
        }
43
        $tmpfile = $this->setDownloadHeaders($filename, $ch);
44
        curl_setopt($ch, CURLOPT_USERAGENT, "Skylab " . Application::VERSION . " (https://github.com/Kunstmaan/skylab)");
45
        $result = curl_exec($ch);
46
        $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
47
	if($http_status == 403) {
48
          throw new AccessDeniedException();
49
        }
50
        curl_close($ch);
51
        if ($filename) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filename of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
52
            $this->closeFile($tmpfile);
53
        } else {
54
            return $result;
55
        }
56
57
        return false;
58
    }
59
60
    /**
61
     * @param string|null $filename
62
     * @param resource $ch
63
     */
64
    private function setDownloadHeaders($filename, $ch)
65
    {
66
        if ($filename) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filename of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
67
            $tempFP = fopen($filename, 'w+');
68
            curl_setopt($ch, CURLOPT_FILE, $tempFP);
69
            curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
70
71
            return $tempFP;
72
        }
73
74
        return false;
75
    }
76
77
    private function closeFile($tempFP)
78
    {
79
        if (!$tempFP) {
80
            fclose($tempFP);
81
        }
82
    }
83
84
}
85