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.

HttpRequest::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
crap 1
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
88
         // CURLOPT_FOLLOWLOCATION doesnt play well with open_basedir/safe_mode
89 153
        if (ini_get('safe_mode') || ini_get('open_basedir')) {
90
            $options[CURLOPT_FOLLOWLOCATION] = false;
91
            $options[CURLOPT_TIMEOUT] = 15;
92
            $this->config['force_redirects'] = true;
93
        }
94
95 153
        $handler = curl_init();
96 153
        curl_setopt_array($handler, $options);
97 153
        $response = curl_exec($handler);
98
99 153
        $status = curl_getinfo($handler, CURLINFO_HTTP_CODE);
100 153
        $headerSize = curl_getinfo($handler, CURLINFO_HEADER_SIZE);
101
102 153
        $header = substr($response, 0, $headerSize);
103 153
        $body = substr($response, $headerSize);
104 153
        curl_close($handler);
105
106 153
        if ($this->config['force_redirects'] && in_array($status, array('301', '302'))) {
107
108 2
            if (preg_match('~(?:location|uri): ?([^\n]+)~i', $header, $matches)) {
109
110 2
                $url = trim($matches['1']);
111
112
                // Relative redirections
113 2
                if (substr($url, 0, 1) == '/') {
114 2
                    $parsed = parse_url($options[CURLOPT_URL]);
115 2
                    $url = $parsed['scheme'] . '://' . rtrim($parsed['host'], '/') . $url;
116 2
                }
117
118 2
                return $this->curl($url, $options);
119
            }
120
        }
121
122 153
        if (empty($body) || !in_array($status, array('200'))) {
123 3
            throw new \Exception($status . ': Invalid response for ' . $url);
124
        }
125
126 151
        return $body;
127
    }
128
129
    /**
130
     * Uses file_get_contents to fetch data from an url
131
     *
132
     * @param string $url
133
     * @param array $params Additional parameters for the respective part
134
     * @return string
135
     *
136
     * @throws Exception when allow_url_fopen is disabled or when no data was returned
137
     */
138 4
    protected function fileGetContents($url, array $params = array())
139
    {
140 4
        if (!ini_get('allow_url_fopen')) {
141
            throw new \Exception('Could not execute lookup, allow_url_fopen is disabled');
142
        }
143
144 4
        if (!filter_var($url, FILTER_VALIDATE_URL)) {
145 2
            throw new \Exception('Invalid url ' . $url);
146
        }
147
148
        $defaultOptions = array(
149 2
            'method' => 'GET',
150 2
            'user_agent' => $this->userAgent,
151 2
            'follow_location' => 1,
152 2
            'max_redirects' => 20,
153
            'timeout' => 40
154 2
        );
155
156 2
        $context = array('http' => array_merge($defaultOptions, $this->config['fopen'], $params));
157 2
        if ($data = file_get_contents($url, false, stream_context_create($context))) {
158 2
            return $data;
159
        }
160
161
        throw new \Exception('Invalid Server Response from ' . $url);
162
    }
163
}
164
165
?>
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...
166