Passed
Push — master ( 9ca9c4...998b9e )
by Benjamin
07:17 queued 05:29
created

HttpTransport::setProxy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the php-gelf package.
5
 *
6
 * (c) Benjamin Zikarsky <http://benjamin-zikarsky.de>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gelf\Transport;
13
14
use Gelf\MessageInterface;
15
use Gelf\Encoder\CompressedJsonEncoder;
16
use Gelf\Encoder\JsonEncoder as DefaultEncoder;
17
use RuntimeException;
18
19
/**
20
 * HttpTransport allows the transfer of GELF-messages to an compatible
21
 * GELF-HTTP-backend as described in
22
 * http://www.graylog2.org/resources/documentation/sending/gelfhttp
23
 *
24
 * It can also act as a direct publisher
25
 *
26
 * @author Benjamin Zikarsky <[email protected]>
27
 */
28
class HttpTransport extends AbstractTransport
29
{
30
    const DEFAULT_HOST = "127.0.0.1";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal 127.0.0.1 does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
31
    const DEFAULT_PORT = 12202;
32
    const DEFAULT_PATH = "/gelf";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /gelf does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
33
    
34
    const AUTO_SSL_PORT = 443;
35
    
36
    /**
37
     * @var string
38
     */
39
    protected $host;
40
41
    /**
42
     * @var int
43
     */
44
    protected $port;
45
46
    /**
47
     * @var string
48
     */
49
    protected $path;
50
51
    /**
52
     * @var StreamSocketClient
53
     */
54
    protected $socketClient;
55
56
    /**
57
     * @var SslOptions|null
58
     */
59
    protected $sslOptions = null;
60
61
    /**
62
     * @var string|null
63
     */
64
    protected $authentication = null;
65
66
    /**
67
     * @var string|null
68
     */
69
    protected $proxyUri = null;
70
71
    /**
72
     * @var bool
73
     */
74 15
    protected $requestFullUri = false;
75
76
    /**
77
     * Class constructor
78
     *
79
     * @param string|null     $host       when NULL or empty default-host is used
80 15
     * @param int|null        $port       when NULL or empty default-port is used
81 15
     * @param string|null     $path       when NULL or empty default-path is used
82 15
     * @param SslOptions|null $sslOptions when null not SSL is used
83
     */
84 15
    public function __construct(
85 1
        $host = self::DEFAULT_HOST,
86 1
        $port = self::DEFAULT_PORT,
87
        $path = self::DEFAULT_PATH,
88 15
        SslOptions $sslOptions = null
89
    ) {
90 15
        $this->host = $host;
91 15
        $this->port = $port;
92 15
        $this->path = $path;
93 15
94 15
        if ($port == self::AUTO_SSL_PORT && $sslOptions == null) {
95 15
            $sslOptions = new SslOptions();
96 15
        }
97 15
98
        $this->sslOptions = $sslOptions;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
99
        $this->messageEncoder = new DefaultEncoder();
100
        $this->socketClient = new StreamSocketClient(
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
101
            $this->getScheme(),
102
            $this->host,
103
            $this->port,
104
            $this->getContext()
105
        );
106
    }
107
108
    /**
109
     * Creates a HttpTransport from a URI
110
     *
111
     * Supports http and https schemes, port-, path- and auth-definitions
112
     * If the port is omitted 80 and 443 are used respectively.
113 3
     * If a username but no password is given, and empty password is used.
114
     * If a https URI is given, the provided SslOptions (with a fallback to
115 3
     * the default SslOptions) are used.
116
     *
117
     * @param  string          $url
118 3
     * @param  SslOptions|null $sslOptions
119 1
     *
120
     * @return HttpTransport
121
     */
122
    public static function fromUrl($url, SslOptions $sslOptions = null)
123 2
    {
124 2
        $parsed = parse_url($url);
125 1
        
126
        // check it's a valid URL
127
        if (false === $parsed || !isset($parsed['host']) || !isset($parsed['scheme'])) {
128
            throw new \InvalidArgumentException("$url is not a valid URL");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $url instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
129 1
        }
130
        
131
        // check it's http or https
132 1
        $scheme = strtolower($parsed['scheme']);
133 1
        if (!in_array($scheme, array('http', 'https'))) {
134 1
            throw new \InvalidArgumentException("$url is not a valid http/https URL");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $url instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
135 1
        }
136
137
        // setup defaults
138 1
        $defaults = array('port' => 80, 'path' => '', 'user' => null, 'pass' => '');
139 1
140
        // change some defaults for https
141
        if ($scheme == 'https') {
142 1
            $sslOptions = $sslOptions ?: new SslOptions();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
143 1
            $defaults['port'] = 443;
144 1
        }
145
         
146 1
        // merge defaults and real data and build transport
147
        $parsed = array_merge($defaults, $parsed);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
148
        $transport = new static($parsed['host'], $parsed['port'], $parsed['path'], $sslOptions);
149
150
        // add optional authentication
151
        if ($parsed['user']) {
152
            $transport->setAuthentication($parsed['user'], $parsed['pass']);
153
        }
154
155 2
        return $transport;
156
    }
157 2
158 2
    /**
159
     * Sets HTTP basic authentication
160
     *
161
     * @param string $username
162
     * @param string $password
163
     */
164
    public function setAuthentication($username, $password)
165
    {
166
        $this->authentication = $username . ":" . $password;
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal : does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
167 6
    }
168
169 6
    /**
170 6
     * Enables HTTP proxy
171
     *
172
     * @param $proxyUri
173 6
     * @param bool $requestFullUri
174 6
     */
175 6
    public function setProxy($proxyUri, $requestFullUri = false)
176 6
    {
177 6
        $this->proxyUri = $proxyUri;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
178
        $this->requestFullUri = $requestFullUri;
179 6
180
        $this->socketClient->setContext($this->getContext());
181 6
    }
182 1
183 1
    /**
184
     * Sends a Message over this transport
185 6
     *
186 1
     * @param MessageInterface $message
187 1
     *
188
     * @return int the number of bytes sent
189 6
     */
190 6
    public function send(MessageInterface $message)
191
    {
192 6
        $messageEncoder = $this->getMessageEncoder();
193
        $rawMessage = $messageEncoder->encode($message);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
194 6
195 6
        $request = array(
196
            sprintf("POST %s HTTP/1.1", $this->path),
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal POST %s HTTP/1.1 does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
197
            sprintf("Host: %s:%d", $this->host, $this->port),
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Host: %s:%d does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
198
            sprintf("Content-Length: %d", strlen($rawMessage)),
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Content-Length: %d does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
199 6
            "Content-Type: application/json",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Content-Type: application/json does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
200 3
            "Connection: Keep-Alive",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Connection: Keep-Alive does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
201 3
            "Accept: */*"
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Accept: */* does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
202
        );
203 6
204 1
        if (null !== $this->authentication) {
205 1
            $request[] = "Authorization: Basic " . base64_encode($this->authentication);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Authorization: Basic does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
206 1
        }
207 1
208 1
        if ($messageEncoder instanceof CompressedJsonEncoder) {
209 1
            $request[] = "Content-Encoding: gzip";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Content-Encoding: gzip does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
210
        }
211
212 5
        $request[] = ""; // blank line to separate headers from body
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
213
        $request[] = $rawMessage;
214
215
        $request = implode($request, "\r\n");
0 ignored issues
show
Unused Code introduced by
The call to implode() has too many arguments starting with ' '. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

215
        $request = /** @scrutinizer ignore-call */ implode($request, "\r\n");

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
216
217
        $byteCount = $this->socketClient->write($request);
218 6
        $headers = $this->readResponseHeaders();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
219
220 6
        // if we don't have a HTTP/1.1 connection, or the server decided to close the connection
221 6
        // we should do so as well. next read/write-attempt will open a new socket in this case.
222 6
        if (strpos($headers, "HTTP/1.1") !== 0 || preg_match("!Connection:\s*Close!i", $headers)) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal HTTP/1.1 does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal !Connection:\s*Close!i does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
223
            $this->socketClient->close();
224
        }
225 6
226 6
        if (!preg_match("!^HTTP/1.\d 202 Accepted!i", $headers)) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal !^HTTP/1.\d 202 Accepted!i does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
227 6
            throw new RuntimeException(
228
                sprintf(
229 6
                    "Graylog-Server didn't answer properly, expected 'HTTP/1.x 202 Accepted', response is '%s'",
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 112 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
230
                    trim($headers)
231 6
                )
232
            );
233
        }
234
235
        return $byteCount;
236
    }
237 15
238
    /**
239 15
     * @return string
240
     */
241
    private function readResponseHeaders()
242
    {
243
        $chunkSize = 1024; // number of bytes to read at once
244
        $delimiter = "\r\n\r\n"; // delimiter between headers and response
245 15
        $response = "";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
246
247 15
        do {
248 15
            $chunk = $this->socketClient->read($chunkSize);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
249
            $response .= $chunk;
250
        } while (false === strpos($chunk, $delimiter) && strlen($chunk) > 0);
251 3
252
        $elements = explode($delimiter, $response, 2);
253
254
        return $elements[0];
255
    }
256
257
    /**
258
     * @return string
259 1
     */
260
    private function getScheme()
261 1
    {
262 1
        return null === $this->sslOptions ? 'tcp' : 'ssl';
263
    }
264
265
    /**
266
     * @return array
267
     */
268
    private function getContext()
269 1
    {
270
        $options = array();
271 1
272
        if (null !== $this->sslOptions) {
273
            $options = array_merge($options, $this->sslOptions->toStreamContext($this->host));
274
        }
275
276
        if (null !== $this->proxyUri) {
277
            $options['http'] = array(
278
                'proxy' => $this->proxyUri,
279
                'request_fulluri' => $this->requestFullUri
280
            );
281
        }
282
283
        return $options;
284
    }
285
286
    /**
287
     * Sets the connect-timeout
288
     *
289
     * @param int $timeout
290
     */
291
    public function setConnectTimeout($timeout)
292
    {
293
        $this->socketClient->setConnectTimeout($timeout);
294
    }
295
296
    /**
297
     * Returns the connect-timeout
298
     *
299
     * @return int
300
     */
301
    public function getConnectTimeout()
302
    {
303
        return $this->socketClient->getConnectTimeout();
304
    }
305
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
306