Passed
Push — master ( 9ca9c4...998b9e )
by Benjamin
09:01 queued 07:19
created

StreamSocketClient::isClosed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
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 RuntimeException;
15
use ParagonIE\ConstantTime\Binary;
16
17
/**
18
 * StreamSocketClient is a very simple OO-Wrapper around the PHP
19
 * stream_socket-library and some specific stream-functions like
20
 * fwrite, etc.
21
 *
22
 * @author Benjamin Zikarsky <[email protected]>
23
 */
24
class StreamSocketClient
25
{
26
    /**
27
     * @deprecated deprecated since v1.4.0
28
     */
29
    const SOCKET_TIMEOUT = 30;
30
31
    /**
32
     * @var string
33
     */
34
    protected $host;
35
36
    /**
37
     * @var integer
38
     */
39
    protected $port;
40
41
    /**
42
     * @var string
43
     */
44
    protected $scheme;
45
46
    /**
47
     * @var array
48
     */
49
    protected $context;
50
51
    /**
52
     * @var resource
53
     */
54
    protected $socket;
55
56
    /**
57
     * @var int
58
     */
59
    protected $connectTimeout = self::SOCKET_TIMEOUT;
60
61
    /**
62
     * @param string  $scheme
63
     * @param string  $host
64
     * @param integer $port
65
     * @param array   $context
66
     */
67 43
    public function __construct($scheme, $host, $port, array $context = array())
68
    {
69 43
        $this->scheme = $scheme;
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...
70 43
        $this->host = $host;
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...
71 43
        $this->port = $port;
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...
72 43
        $this->context = $context;
73 43
    }
74
75
    /**
76
     * Destructor, closes socket if possible
77
     */
78 43
    public function __destruct()
79
    {
80 43
        $this->close();
81 43
    }
82
83
    /**
84
     * Initializes socket-client
85
     *
86
     * @deprecated deprecated since v1.4.0
87
     *
88
     * @param string  $scheme  like "udp" or "tcp"
89
     * @param string  $host
90
     * @param integer $port
91
     * @param array   $context
92
     *
93
     * @return resource
94
     *
95
     * @throws RuntimeException on connection-failure
96
     */
97
    protected static function initSocket($scheme, $host, $port, array $context)
98
    {
99
        $socketDescriptor = sprintf("%s://%s:%d", $scheme, $host, $port);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal %s://%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...
100
        $socket = @stream_socket_client(
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 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
            $socketDescriptor,
102
            $errNo,
103
            $errStr,
104
            static::SOCKET_TIMEOUT,
0 ignored issues
show
Deprecated Code introduced by
The constant Gelf\Transport\StreamSocketClient::SOCKET_TIMEOUT has been deprecated: deprecated since v1.4.0 ( Ignorable by Annotation )

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

104
            /** @scrutinizer ignore-deprecated */ static::SOCKET_TIMEOUT,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
105
            \STREAM_CLIENT_CONNECT,
106
            stream_context_create($context)
107
        );
108
109
        if ($socket === false) {
110
            throw new RuntimeException(
111
                sprintf(
112
                    "Failed to create socket-client for %s: %s (%s)",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Failed to create socket-client for %s: %s (%s) 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...
113
                    $socketDescriptor,
114
                    $errStr,
115
                    $errNo
116
                )
117
            );
118
        }
119
120
        // set non-blocking for UDP
121
        if (strcasecmp("udp", $scheme) == 0) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal udp 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...
122
            stream_set_blocking($socket, 0);
123
        }
124
125
        return $socket;
126
    }
127
128
129
    /**
130
     * Internal function mimicking the behaviour of static::initSocket
131
     * which will get new functionality instead of the deprecated
132
     * "factory"
133
     *
134
     * @return resource
135
     *
136
     * @throws RuntimeException on connection-failure
137
     */
138 14
    private function buildSocket()
139
    {
140 14
        $socketDescriptor = sprintf(
141 14
            "%s://%s:%d",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal %s://%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...
142 14
            $this->scheme,
143 14
            $this->host,
144 14
            $this->port
145 14
        );
146
147 14
        $socket = @stream_socket_client(
148 14
            $socketDescriptor,
149 14
            $errNo,
150 14
            $errStr,
151 14
            $this->connectTimeout,
152 14
            \STREAM_CLIENT_CONNECT,
153 14
            stream_context_create($this->context)
154 14
        );
155
156 14
        if ($socket === false) {
157 1
            throw new RuntimeException(
158 1
                sprintf(
159 1
                    "Failed to create socket-client for %s: %s (%s)",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Failed to create socket-client for %s: %s (%s) 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...
160 1
                    $socketDescriptor,
161 1
                    $errStr,
162
                    $errNo
163 1
                )
164 1
            );
165
        }
166
167
        // set non-blocking for UDP
168 13
        if (strcasecmp("udp", $this->scheme) == 0) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal udp 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...
169 3
            stream_set_blocking($socket, 0);
170 3
        }
171
172 13
        return $socket;
173
    }
174
175
    /**
176
     * Returns raw-socket-resource
177
     *
178
     * @return resource
179
     */
180 14
    public function getSocket()
181
    {
182
        // lazy initializing of socket-descriptor
183 14
        if (!$this->socket) {
184 14
            $this->socket = $this->buildSocket();
185 13
        }
186
187 13
        return $this->socket;
188
    }
189
190
    /**
191
     * Writes a given string to the socket and returns the
192
     * number of written bytes
193
     *
194
     * @param string $buffer
195
     *
196
     * @return int
197
     *
198
     * @throws RuntimeException on write-failure
199
     */
200 8
    public function write($buffer)
201
    {
202 8
        $buffer = (string) $buffer;
203 8
        $bufLen = Binary::safeStrlen($buffer);
204
205 8
        $socket = $this->getSocket();
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...
206 8
        $written = 0;
207
208 8
        while ($written < $bufLen) {
209
            // PHP's fwrite does not behave nice in regards to errors, so we wrap
210
            // it with a temporary error handler and treat every warning/notice as
211
            // a error
212 8
            $failed = false;
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...
213 8
            $errorMessage = "Failed to write to socket";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Failed to write to socket 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...
214 8
            set_error_handler(function ($errno, $errstr) use (&$failed, &$errorMessage) {
215 1
                $failed = true;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
216 1
                $errorMessage .= ": $errstr ($errno)";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $errstr 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...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $errno 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...
217 8
            });
218 8
            $byteCount = fwrite($socket, Binary::safeSubstr($buffer, $written));
219 8
            restore_error_handler();
220
221 8
            if ($byteCount === 0 && defined('HHVM_VERSION')) {
222
                $failed = true;
223
            }
224
225 8
            if ($failed || $byteCount === false) {
226 1
                throw new \RuntimeException($errorMessage);
227
            }
228
229 8
            $written += $byteCount;
230 8
        }
231
232
233 8
        return $written;
234
    }
235
236
    /**
237
     * Reads a given number of bytes from the socket
238
     *
239
     * @param integer $byteCount
240
     *
241
     * @return string
242
     */
243 2
    public function read($byteCount)
244
    {
245 2
        return fread($this->getSocket(), $byteCount);
246
    }
247
248
    /**
249
     * Closes underlying socket explicitly
250
     */
251 43
    public function close()
252
    {
253 43
        if (!is_resource($this->socket)) {
254 31
            return;
255
        }
256
257 13
        fclose($this->socket);
258 13
        $this->socket = null;
259 13
    }
260
261
    /**
262
     * Checks if the socket is closed
263
     *
264
     * @return bool
265
     */
266 1
    public function isClosed()
267
    {
268 1
        return $this->socket === null;
269
    }
270
271
    /**
272
     * Returns the current connect-timeout
273
     *
274
     * @return int
275
     */
276 1
    public function getConnectTimeout()
277
    {
278 1
        return $this->connectTimeout;
279 1
    }
280
281
    /**
282
     * Sets the connect-timeout
283
     *
284
     * @param int $timeout
285
     */
286
    public function setConnectTimeout($timeout)
287
    {
288
        if (!$this->isClosed()) {
289
            throw new \LogicException("Cannot change socket properties with an open connection");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Cannot change socket pro...with an open connection 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...
290
        }
291
292
        $this->connectTimeout = $timeout;
293
    }
294
295
    /**
296
     * Returns the stream context
297
     *
298
     * @return array
299
     */
300
    public function getContext()
301
    {
302
        return $this->context;
303
    }
304
305
    /**
306
     * Sets the stream context
307
     *
308
     * @param array $context
309
     */
310
    public function setContext(array $context)
311
    {
312
        if (!$this->isClosed()) {
313
            throw new \LogicException("Cannot change socket properties with an open connection");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Cannot change socket pro...with an open connection 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...
314
        }
315
316
        $this->context = $context;
317
    }
318
}
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...
319