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.
Passed
Push — master ( 5cefd1...492078 )
by Anton
04:08
created

SocketServer::errno()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 9.6111
1
<?php
2
3
namespace React\Socket;
4
5
use Evenement\EventEmitter;
6
use React\EventLoop\LoopInterface;
7
8
final class SocketServer extends EventEmitter implements ServerInterface
9
{
10
    private $server;
11
12
    /**
13
     * The `SocketServer` class is the main class in this package that implements the `ServerInterface` and
14
     * allows you to accept incoming streaming connections, such as plaintext TCP/IP or secure TLS connection streams.
15
     *
16
     * ```php
17
     * $socket = new React\Socket\SocketServer('127.0.0.1:0');
18
     * $socket = new React\Socket\SocketServer('127.0.0.1:8000');
19
     * $socket = new React\Socket\SocketServer('127.0.0.1:8000', $context);
20
     * ```
21
     *
22
     * This class takes an optional `LoopInterface|null $loop` parameter that can be used to
23
     * pass the event loop instance to use for this object. You can use a `null` value
24
     * here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
25
     * This value SHOULD NOT be given unless you're sure you want to explicitly use a
26
     * given event loop instance.
27
     *
28
     * @param string         $uri
29
     * @param array          $context
30
     * @param ?LoopInterface $loop
31
     * @throws \InvalidArgumentException if the listening address is invalid
32
     * @throws \RuntimeException if listening on this address fails (already in use etc.)
33
     */
34
    public function __construct($uri, array $context = array(), LoopInterface $loop = null)
35
    {
36
        // apply default options if not explicitly given
37
        $context += array(
38
            'tcp' => array(),
39
            'tls' => array(),
40
            'unix' => array()
41
        );
42
43
        $scheme = 'tcp';
44
        $pos = \strpos($uri, '://');
45
        if ($pos !== false) {
46
            $scheme = \substr($uri, 0, $pos);
47
        }
48
49
        if ($scheme === 'unix') {
50
            $server = new UnixServer($uri, $loop, $context['unix']);
51
        } elseif ($scheme === 'php') {
52
            $server = new FdServer($uri, $loop);
53
        } else {
54
            if (preg_match('#^(?:\w+://)?\d+$#', $uri)) {
55
                throw new \InvalidArgumentException(
56
                    'Invalid URI given (EINVAL)',
57
                    \defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
58
                );
59
            }
60
61
            $server = new TcpServer(str_replace('tls://', '', $uri), $loop, $context['tcp']);
62
63
            if ($scheme === 'tls') {
64
                $server = new SecureServer($server, $loop, $context['tls']);
65
            }
66
        }
67
68
        $this->server = $server;
69
70
        $that = $this;
71
        $server->on('connection', function (ConnectionInterface $conn) use ($that) {
72
            $that->emit('connection', array($conn));
73
        });
74
        $server->on('error', function (\Exception $error) use ($that) {
75
            $that->emit('error', array($error));
76
        });
77
    }
78
79
    public function getAddress()
80
    {
81
        return $this->server->getAddress();
82
    }
83
84
    public function pause()
85
    {
86
        $this->server->pause();
87
    }
88
89
    public function resume()
90
    {
91
        $this->server->resume();
92
    }
93
94
    public function close()
95
    {
96
        $this->server->close();
97
    }
98
99
    /**
100
     * [internal] Internal helper method to accept new connection from given server socket
101
     *
102
     * @param resource $socket server socket to accept connection from
103
     * @return resource new client socket if any
104
     * @throws \RuntimeException if accepting fails
105
     * @internal
106
     */
107
    public static function accept($socket)
108
    {
109
        $newSocket = @\stream_socket_accept($socket, 0);
110
111
        if (false === $newSocket) {
112
            // Match errstr from PHP's warning message.
113
            // stream_socket_accept(): accept failed: Connection timed out
114
            $error = \error_get_last();
115
            $errstr = \preg_replace('#.*: #', '', $error['message']);
116
            $errno = self::errno($errstr);
117
118
            throw new \RuntimeException(
119
                'Unable to accept new connection: ' . $errstr . self::errconst($errno),
120
                $errno
121
            );
122
        }
123
124
        return $newSocket;
125
    }
126
127
    /**
128
     * [Internal] Returns errno value for given errstr
129
     *
130
     * The errno and errstr values describes the type of error that has been
131
     * encountered. This method tries to look up the given errstr and find a
132
     * matching errno value which can be useful to provide more context to error
133
     * messages. It goes through the list of known errno constants when
134
     * ext-sockets is available to find an errno matching the given errstr.
135
     *
136
     * @param string $errstr
137
     * @return int errno value (e.g. value of `SOCKET_ECONNREFUSED`) or 0 if not found
138
     * @internal
139
     * @copyright Copyright (c) 2018 Christian Lück, taken from https://github.com/clue/errno with permission
140
     * @codeCoverageIgnore
141
     */
142
    public static function errno($errstr)
143
    {
144
        if (\function_exists('socket_strerror')) {
145
            foreach (\get_defined_constants(false) as $name => $value) {
146
                if (\strpos($name, 'SOCKET_E') === 0 && \socket_strerror($value) === $errstr) {
147
                    return $value;
148
                }
149
            }
150
        }
151
152
        return 0;
153
    }
154
155
    /**
156
     * [Internal] Returns errno constant name for given errno value
157
     *
158
     * The errno value describes the type of error that has been encountered.
159
     * This method tries to look up the given errno value and find a matching
160
     * errno constant name which can be useful to provide more context and more
161
     * descriptive error messages. It goes through the list of known errno
162
     * constants when ext-sockets is available to find the matching errno
163
     * constant name.
164
     *
165
     * Because this method is used to append more context to error messages, the
166
     * constant name will be prefixed with a space and put between parenthesis
167
     * when found.
168
     *
169
     * @param int $errno
170
     * @return string e.g. ` (ECONNREFUSED)` or empty string if no matching const for the given errno could be found
171
     * @internal
172
     * @copyright Copyright (c) 2018 Christian Lück, taken from https://github.com/clue/errno with permission
173
     * @codeCoverageIgnore
174
     */
175
    public static function errconst($errno)
176
    {
177
        if (\function_exists('socket_strerror')) {
178
            foreach (\get_defined_constants(false) as $name => $value) {
179
                if ($value === $errno && \strpos($name, 'SOCKET_E') === 0) {
180
                    return ' (' . \substr($name, 7) . ')';
181
                }
182
            }
183
        }
184
185
        return '';
186
    }
187
}
188