Skip to content

Commit c2d3401

Browse files
committed
Merge pull request #11 from php-http/analysis-Xaj62k
Applied fixes from StyleCI
2 parents b6ae251 + 5583ef1 commit c2d3401

File tree

4 files changed

+69
-69
lines changed

4 files changed

+69
-69
lines changed

src/Client.php

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Symfony\Component\OptionsResolver\OptionsResolver;
1313

1414
/**
15-
* Socket Http Client
15+
* Socket Http Client.
1616
*
1717
* Use stream and socket capabilities of the core of PHP to send HTTP requests
1818
*
@@ -24,20 +24,21 @@ class Client implements HttpClient
2424
use ResponseReader;
2525

2626
private $config = [
27-
'remote_socket' => null,
28-
'timeout' => null,
29-
'stream_context_options' => array(),
30-
'stream_context_param' => array(),
31-
'ssl' => null,
32-
'write_buffer_size' => 8192,
33-
'ssl_method' => STREAM_CRYPTO_METHOD_TLS_CLIENT
27+
'remote_socket' => null,
28+
'timeout' => null,
29+
'stream_context_options' => [],
30+
'stream_context_param' => [],
31+
'ssl' => null,
32+
'write_buffer_size' => 8192,
33+
'ssl_method' => STREAM_CRYPTO_METHOD_TLS_CLIENT,
3434
];
3535

3636
/**
3737
* Constructor.
3838
*
3939
* @param ResponseFactory $responseFactory Response factory for creating response
40-
* @param array $config {
40+
* @param array $config {
41+
*
4142
* @var string $remote_socket Remote entrypoint (can be a tcp or unix domain address)
4243
* @var int $timeout Timeout before canceling request
4344
* @var array $stream_context_options Context options as defined in the PHP documentation
@@ -68,8 +69,8 @@ public function __construct(ResponseFactory $responseFactory = null, array $conf
6869
*/
6970
public function sendRequest(RequestInterface $request)
7071
{
71-
$remote = $this->config['remote_socket'];
72-
$useSsl = $this->config['ssl'];
72+
$remote = $this->config['remote_socket'];
73+
$useSsl = $this->config['ssl'];
7374

7475
if (!$request->hasHeader('Connection')) {
7576
$request = $request->withHeader('Connection', 'close');
@@ -98,19 +99,19 @@ public function sendRequest(RequestInterface $request)
9899
}
99100

100101
/**
101-
* Create the socket to write request and read response on it
102+
* Create the socket to write request and read response on it.
102103
*
103104
* @param RequestInterface $request Request for
104105
* @param string $remote Entrypoint for the connection
105-
* @param boolean $useSsl Whether to use ssl or not
106+
* @param bool $useSsl Whether to use ssl or not
106107
*
107108
* @throws NetworkException When the connection fail
108109
*
109110
* @return resource Socket resource
110111
*/
111112
protected function createSocket(RequestInterface $request, $remote, $useSsl)
112113
{
113-
$errNo = null;
114+
$errNo = null;
114115
$errMsg = null;
115116
$socket = @stream_socket_client($remote, $errNo, $errMsg, floor($this->config['timeout'] / 1000), STREAM_CLIENT_CONNECT, $this->config['stream_context']);
116117

@@ -130,7 +131,7 @@ protected function createSocket(RequestInterface $request, $remote, $useSsl)
130131
}
131132

132133
/**
133-
* Close the socket, used when having an error
134+
* Close the socket, used when having an error.
134135
*
135136
* @param resource $socket
136137
*/
@@ -140,7 +141,7 @@ protected function closeSocket($socket)
140141
}
141142

142143
/**
143-
* Return configuration for the socket client
144+
* Return configuration for the socket client.
144145
*
145146
* @param array $config Configuration from user
146147
*
@@ -165,7 +166,7 @@ protected function configure(array $config = [])
165166
}
166167

167168
/**
168-
* Return remote socket from the request
169+
* Return remote socket from the request.
169170
*
170171
* @param RequestInterface $request
171172
*
@@ -175,13 +176,13 @@ protected function configure(array $config = [])
175176
*/
176177
private function determineRemoteFromRequest(RequestInterface $request)
177178
{
178-
if ($request->getUri()->getHost() == "" && !$request->hasHeader('Host')) {
179-
throw new NetworkException("Cannot find connection endpoint for this request", $request);
179+
if ($request->getUri()->getHost() == '' && !$request->hasHeader('Host')) {
180+
throw new NetworkException('Cannot find connection endpoint for this request', $request);
180181
}
181182

182183
$host = $request->getUri()->getHost();
183-
$port = $request->getUri()->getPort() ?: ($request->getUri()->getScheme() == "https" ? 443 : 80);
184-
$endpoint = sprintf("%s:%s", $host, $port);
184+
$port = $request->getUri()->getPort() ?: ($request->getUri()->getScheme() == 'https' ? 443 : 80);
185+
$endpoint = sprintf('%s:%s', $host, $port);
185186

186187
// If use the host header if present for the endpoint
187188
if (empty($host) && $request->hasHeader('Host')) {

src/RequestWriter.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Psr\Http\Message\RequestInterface;
77

88
/**
9-
* Method for writing request
9+
* Method for writing request.
1010
*
1111
* Mainly used by SocketHttpClient
1212
*
@@ -15,18 +15,18 @@
1515
trait RequestWriter
1616
{
1717
/**
18-
* Write a request to a socket
18+
* Write a request to a socket.
1919
*
2020
* @param resource $socket
2121
* @param RequestInterface $request
22-
* @param integer $bufferSize
22+
* @param int $bufferSize
2323
*
2424
* @throws \Http\Client\Exception\NetworkException
2525
*/
2626
protected function writeRequest($socket, RequestInterface $request, $bufferSize = 8192)
2727
{
2828
if (false === $this->fwrite($socket, $this->transformRequestHeadersToString($request))) {
29-
throw new NetworkException("Failed to send request, underlying socket not accessible, (BROKEN EPIPE)", $request);
29+
throw new NetworkException('Failed to send request, underlying socket not accessible, (BROKEN EPIPE)', $request);
3030
}
3131

3232
if ($request->getBody()->isReadable()) {
@@ -35,11 +35,11 @@ protected function writeRequest($socket, RequestInterface $request, $bufferSize
3535
}
3636

3737
/**
38-
* Write Body of the request
38+
* Write Body of the request.
3939
*
4040
* @param resource $socket
4141
* @param RequestInterface $request
42-
* @param integer $bufferSize
42+
* @param int $bufferSize
4343
*
4444
* @throws \Http\Client\Exception\NetworkException
4545
* @throws \Http\Client\Exception\RequestException
@@ -56,24 +56,24 @@ protected function writeBody($socket, RequestInterface $request, $bufferSize = 8
5656
$buffer = $body->read($bufferSize);
5757

5858
if (false === $this->fwrite($socket, $buffer)) {
59-
throw new NetworkException("An error occur when writing request to client (BROKEN EPIPE)", $request);
59+
throw new NetworkException('An error occur when writing request to client (BROKEN EPIPE)', $request);
6060
}
6161
}
6262
}
6363

6464
/**
65-
* Produce the header of request as a string based on a PSR Request
65+
* Produce the header of request as a string based on a PSR Request.
6666
*
6767
* @param RequestInterface $request
6868
*
6969
* @return string
7070
*/
7171
protected function transformRequestHeadersToString(RequestInterface $request)
7272
{
73-
$message = vsprintf('%s %s HTTP/%s', [
73+
$message = vsprintf('%s %s HTTP/%s', [
7474
strtoupper($request->getMethod()),
7575
$request->getRequestTarget(),
76-
$request->getProtocolVersion()
76+
$request->getProtocolVersion(),
7777
])."\r\n";
7878

7979
foreach ($request->getHeaders() as $name => $values) {
@@ -86,7 +86,7 @@ protected function transformRequestHeadersToString(RequestInterface $request)
8686
}
8787

8888
/**
89-
* Replace fwrite behavior as api is broken in PHP
89+
* Replace fwrite behavior as api is broken in PHP.
9090
*
9191
* @see https://secure.phabricator.com/rPHU69490c53c9c2ef2002bc2dd4cecfe9a4b080b497
9292
*

src/ResponseReader.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
namespace Http\Client\Socket;
44

55
use Http\Client\Exception\NetworkException;
6-
use Http\Message\MessageFactory;
76
use Http\Message\ResponseFactory;
87
use Psr\Http\Message\RequestInterface;
98
use Psr\Http\Message\ResponseInterface;
109

1110
/**
12-
* Method for reading response
11+
* Method for reading response.
1312
*
1413
* Mainly used by SocketHttpClient
1514
*
@@ -23,7 +22,7 @@ trait ResponseReader
2322
protected $responseFactory;
2423

2524
/**
26-
* Read a response from a socket
25+
* Read a response from a socket.
2726
*
2827
* @param RequestInterface $request
2928
* @param resource $socket
@@ -34,8 +33,8 @@ trait ResponseReader
3433
*/
3534
protected function readResponse(RequestInterface $request, $socket)
3635
{
37-
$headers = [];
38-
$reason = null;
36+
$headers = [];
37+
$reason = null;
3938

4039
while (($line = fgets($socket)) !== false) {
4140
if (rtrim($line) === '') {
@@ -47,7 +46,7 @@ protected function readResponse(RequestInterface $request, $socket)
4746
$metadatas = stream_get_meta_data($socket);
4847

4948
if (array_key_exists('timed_out', $metadatas) && true === $metadatas['timed_out']) {
50-
throw new NetworkException("Error while reading response, stream timed out", $request);
49+
throw new NetworkException('Error while reading response, stream timed out', $request);
5150
}
5251

5352
$parts = explode(' ', array_shift($headers), 3);
@@ -57,7 +56,7 @@ protected function readResponse(RequestInterface $request, $socket)
5756
}
5857

5958
$protocol = substr($parts[0], -3);
60-
$status = $parts[1];
59+
$status = $parts[1];
6160

6261
if (isset($parts[2])) {
6362
$reason = $parts[2];
@@ -79,13 +78,13 @@ protected function readResponse(RequestInterface $request, $socket)
7978
}
8079

8180
$response = $this->responseFactory->createResponse($status, $reason, $responseHeaders, null, $protocol);
82-
$stream = $this->createStream($socket, $response);
81+
$stream = $this->createStream($socket, $response);
8382

8483
return $response->withBody($stream);
8584
}
8685

8786
/**
88-
* Create the stream
87+
* Create the stream.
8988
*
9089
* @param $socket
9190
* @param ResponseInterface $response
@@ -97,7 +96,7 @@ protected function createStream($socket, ResponseInterface $response)
9796
$size = null;
9897

9998
if ($response->hasHeader('Content-Length')) {
100-
$size = (int)$response->getHeaderLine('Content-Length');
99+
$size = (int) $response->getHeaderLine('Content-Length');
101100
}
102101

103102
return new Stream($socket, $size);

0 commit comments

Comments
 (0)