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.
Completed
Pull Request — master (#40)
by
unknown
08:26 queued 02:05
created

HttpRequest::fetch()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
nc 2
cc 3
eloc 8
nop 2
crap 3
1
<?php
2
/**
3
 * HttpRequest.php
4
 *
5
 * @package Embera
6
 * @author Michael Pratt <[email protected]>
7
 * @link   http://www.michael-pratt.com/
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Embera;
14
15
/**
16
 * This class is in charge of doing http requests. Its a very minimal
17
 * wrapper for curl or file_get_contents
18
 */
19
class HttpRequest
20
{
21
    /** @var array Array with custom curl/fopen options */
22
    protected $config = array();
23
24
    /** @var string User Agent String */
25
    protected $userAgent = 'Mozilla/5.0 PHP/Embera';
26
27
    /**
28
     * Constructor
29
     *
30
     * @param array $config
31
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
32
     */
33 401
    public function __construct(array $config = array())
34
    {
35 401
        $this->config = array_merge(array(
36 401
            'curl' => array(),
37 401
            'fopen' => array(),
38 401
            'force_redirects' => false,
39 401
            'prefer_curl' => true,
40 401
        ), $config);
41 401
    }
42
43
    /**
44
     * Executes http requests
45
     *
46
     * @param string $url
47
     * @param array $params Additional parameters for the respective part
48
     * @return string
49
     *
50
     * @throws Exception when an error ocurred or if no way to do a request exists
51
     */
52 157
    public function fetch($url, array $params = array())
53
    {
54 157
        $params = array_merge(array(
55 157
            'curl' => array(),
56 157
            'fopen' => array(),
57 157
        ), $params);
58
59 157
        if (function_exists('curl_init') && $this->config['prefer_curl']) {
60 153
            return $this->curl($url, $params['curl']);
61
        }
62
63 4
        return $this->fileGetContents($url, $params['fopen']);
64
    }
65
66
    /**
67
     * Uses Curl to fetch data from an url
68
     *
69
     * @param string $url
70
     * @param array $params Additional parameters for the respective part
71
     * @return string
72
     *
73
     * @throws Exception when the returned status code is not 200 or no data was found
74
     */
75 153
    protected function curl($url, array $params = array())
76
    {
77
        // Not using array_merge here because that function reindexes numeric keys
78 153
        $options = $params + $this->config['curl'] + array(
79 153
            CURLOPT_USERAGENT => $this->userAgent,
80 153
            CURLOPT_ENCODING => '',
81 153
            CURLOPT_FOLLOWLOCATION => true,
82 153
        );
83
84 153
        $options[CURLOPT_URL] = $url;
85 153
        $options[CURLOPT_HEADER] = true;
86 153
        $options[CURLOPT_RETURNTRANSFER] = 1;
87
        $options[CURLOPT_SSL_VERIFYPEER] = false;
88
89 153
         // CURLOPT_FOLLOWLOCATION doesnt play well with open_basedir/safe_mode
90
        if (ini_get('safe_mode') || ini_get('open_basedir')) {
91
            $options[CURLOPT_FOLLOWLOCATION] = false;
92
            $options[CURLOPT_TIMEOUT] = 15;
93
            $this->config['force_redirects'] = true;
94
        }
95 153
96 153
        $handler = curl_init();
97 153
        curl_setopt_array($handler, $options);
98
        $response = curl_exec($handler);
99 153
100 153
        $status = curl_getinfo($handler, CURLINFO_HTTP_CODE);
101
        $headerSize = curl_getinfo($handler, CURLINFO_HEADER_SIZE);
102 153
103 153
        $header = substr($response, 0, $headerSize);
104 153
        $body = substr($response, $headerSize);
105
        curl_close($handler);
106 153
107
        if ($this->config['force_redirects'] && in_array($status, array('301', '302'))) {
108 2
109
            if (preg_match('~(?:location|uri): ?([^\n]+)~i', $header, $matches)) {
110 2
111
                $url = trim($matches['1']);
112
113 2
                // Relative redirections
114 2
                if (substr($url, 0, 1) == '/') {
115 2
                    $parsed = parse_url($options[CURLOPT_URL]);
116 2
                    $url = $parsed['scheme'] . '://' . rtrim($parsed['host'], '/') . $url;
117
                }
118 2
119
                return $this->curl($url, $options);
120
            }
121
        }
122 153
123 3
        if (empty($body) || !in_array($status, array('200'))) {
124
            throw new \Exception($status . ': Invalid response for ' . $url);
125
        }
126 151
127
        return $body;
128
    }
129
130
    /**
131
     * Uses file_get_contents to fetch data from an url
132
     *
133
     * @param string $url
134
     * @param array $params Additional parameters for the respective part
135
     * @return string
136
     *
137
     * @throws Exception when allow_url_fopen is disabled or when no data was returned
138 4
     */
139
    protected function fileGetContents($url, array $params = array())
140 4
    {
141
        if (!ini_get('allow_url_fopen')) {
142
            throw new \Exception('Could not execute lookup, allow_url_fopen is disabled');
143
        }
144 4
145 2
        if (!filter_var($url, FILTER_VALIDATE_URL)) {
146
            throw new \Exception('Invalid url ' . $url);
147
        }
148
149 2
        $defaultOptions = array(
150 2
            'method' => 'GET',
151 2
            'user_agent' => $this->userAgent,
152 2
            'follow_location' => 1,
153
            'max_redirects' => 20,
154 2
            'timeout' => 40
155
        );
156 2
157 2
        $context = array('http' => array_merge($defaultOptions, $this->config['fopen'], $params));
158 2
        if ($data = file_get_contents($url, false, stream_context_create($context))) {
159
            return $data;
160
        }
161
162
        throw new \Exception('Invalid Server Response from ' . $url);
163
    }
164
}
165
166
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
167