Skip to content

Commit c32cd5d

Browse files
committed
🛀 cleanup
1 parent ee5b117 commit c32cd5d

21 files changed

+242
-290
lines changed

src/CurlUtils/CurlHandleInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
use const CURLE_COULDNT_CONNECT, CURLE_COULDNT_RESOLVE_HOST, CURLE_COULDNT_RESOLVE_PROXY,
1919
CURLE_GOT_NOTHING, CURLE_OPERATION_TIMEOUTED, CURLE_SSL_CONNECT_ERROR;
2020

21+
/**
22+
* @property resource $curl
23+
* @property int $id
24+
* @property int $retries
25+
* @property \Psr\Http\Message\RequestInterface $request
26+
* @property \Psr\Http\Message\ResponseInterface $response
27+
*/
2128
interface CurlHandleInterface{
2229

2330
const CURL_NETWORK_ERRORS = [

src/CurlUtils/CurlMultiClient.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ public function __construct(
7878
$this->logger = $logger ?? new NullLogger;
7979
$this->curl_multi = curl_multi_init();
8080

81-
curl_multi_setopt($this->curl_multi, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
82-
curl_multi_setopt($this->curl_multi, CURLMOPT_MAXCONNECTS, $this->options->windowSize);
81+
$curl_multi_options = [
82+
CURLMOPT_PIPELINING => CURLPIPE_MULTIPLEX,
83+
CURLMOPT_MAXCONNECTS => $this->options->windowSize,
84+
] + $this->options->curl_multi_options;
8385

84-
foreach($this->options->curl_multi_options as $k => $v){
86+
foreach($curl_multi_options as $k => $v){
8587
curl_multi_setopt($this->curl_multi, $k, $v);
8688
}
8789

src/Psr18/CurlClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class CurlClient extends HTTPClientAbstract{
2424
* @inheritDoc
2525
*/
2626
public function sendRequest(RequestInterface $request):ResponseInterface{
27-
/** @var \chillerlan\HTTP\CurlUtils\CurlHandle $handle */
27+
/** @var \chillerlan\HTTP\CurlUtils\CurlHandleInterface $handle */
2828
$handle = new $this->options->curlHandle($request, $this->responseFactory->createResponse(), $this->options);
2929
$handle->init();
3030

src/Psr7/Stream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public function read($length):string{
259259
$string = fread($this->stream, $length);
260260

261261
if($string === false){
262-
throw new RuntimeException('Unable to read from stream');
262+
throw new RuntimeException('Unable to read from stream'); // @codeCoverageIgnore
263263
}
264264

265265
return $string;

src/Psr7/UploadedFile.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use InvalidArgumentException, RuntimeException;
1818

1919
use function chillerlan\HTTP\Psr17\create_stream_from_input;
20-
use function in_array, is_string, is_writable, move_uploaded_file, php_sapi_name,rename;
20+
use function in_array, is_file, is_string, is_writable, move_uploaded_file, php_sapi_name,rename;
2121

2222
use const UPLOAD_ERR_CANT_WRITE, UPLOAD_ERR_EXTENSION, UPLOAD_ERR_FORM_SIZE, UPLOAD_ERR_INI_SIZE,
2323
UPLOAD_ERR_NO_FILE, UPLOAD_ERR_NO_TMP_DIR, UPLOAD_ERR_OK, UPLOAD_ERR_PARTIAL;
@@ -87,7 +87,7 @@ final class UploadedFile implements UploadedFileInterface{
8787
*
8888
* @throws \InvalidArgumentException
8989
*/
90-
public function __construct($file, int $size, int $error, string $filename = null, string $mediaType = null){
90+
public function __construct($file, int $size, int $error = UPLOAD_ERR_OK, string $filename = null, string $mediaType = null){
9191

9292
if(!in_array($error, $this::UPLOAD_ERRORS, true)){
9393
throw new InvalidArgumentException('Invalid error status for UploadedFile');
@@ -101,12 +101,9 @@ public function __construct($file, int $size, int $error, string $filename = nul
101101

102102
if($this->error === UPLOAD_ERR_OK){
103103

104-
if(is_string($file)){
105-
$this->file = $file;
106-
}
107-
else{
108-
$this->stream = create_stream_from_input($file);
109-
}
104+
is_string($file)
105+
? $this->file = $file
106+
: $this->stream = create_stream_from_input($file);
110107

111108
}
112109

@@ -123,7 +120,11 @@ public function getStream():StreamInterface{
123120
return $this->stream;
124121
}
125122

126-
return $this->streamFactory->createStreamFromFile($this->file, 'r+');
123+
if(is_file($this->file)){
124+
return $this->streamFactory->createStreamFromFile($this->file, 'r+');
125+
}
126+
127+
return $this->streamFactory->createStream($this->file);
127128
}
128129

129130
/**
@@ -152,7 +153,7 @@ public function moveTo($targetPath):void{
152153
}
153154

154155
if($this->moved === false){
155-
throw new RuntimeException('Uploaded file could not be moved to '.$targetPath);
156+
throw new RuntimeException('Uploaded file could not be moved to '.$targetPath); // @codeCoverageIgnore
156157
}
157158

158159
}

tests/CurlUtils/CurlMultiClientTest.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
use PHPUnit\Framework\TestCase;
1919
use Psr\Http\Client\ClientExceptionInterface;
2020
use Psr\Http\Message\{RequestInterface, ResponseInterface};
21+
2122
use function chillerlan\HTTP\Psr7\build_http_query;
23+
use function array_column, implode, in_array, ksort;
2224

2325
class CurlMultiClientTest extends TestCase{
2426

@@ -40,15 +42,18 @@ protected function setUp():void{
4042
protected function getRequests():array{
4143

4244
$ids = [
43-
[1,2,6,11,15,23,24,56,57,58,59,60,61,62,63,64,68,69,70,71,72,73,74,75,76],
44-
[77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101],
45+
[1, 2, 6, 11, 15, 23, 24, 56, 57, 58, 59, 60, 61, 62, 63, 64, 68, 69, 70, 71, 72, 73, 74, 75, 76],
46+
[77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101],
4547
];
4648

4749
$requests = [];
4850

4951
foreach($ids as $chunk){
5052
foreach(['de', 'en', 'es', 'fr', 'zh'] as $lang){
51-
$requests[] = new Request(Request::METHOD_GET, 'https://api.guildwars2.com/v2/items?'.build_http_query(['lang' => $lang, 'ids' => implode(',', $chunk)]));
53+
$requests[] = new Request(
54+
Request::METHOD_GET,
55+
'https://api.guildwars2.com/v2/items?'.build_http_query(['lang' => $lang, 'ids' => implode(',', $chunk)])
56+
);
5257
}
5358
}
5459

@@ -63,7 +68,7 @@ protected function getTestResponseHandler():MultiResponseHandlerInterface{
6368

6469
public function handleResponse(ResponseInterface $response, RequestInterface $request, int $id, array $curl_info):?RequestInterface{
6570

66-
if(\in_array($response->getStatusCode(), [200, 206], true)){
71+
if(in_array($response->getStatusCode(), [200, 206], true)){
6772
$this->responses[$id]['lang'] = $response->getHeaderLine('content-language');
6873
// ok, so the headers are empty on travis???
6974
# \var_dump($response->getHeaders());
@@ -76,7 +81,7 @@ public function handleResponse(ResponseInterface $response, RequestInterface $re
7681
}
7782

7883
public function getResponses():array{
79-
\ksort($this->responses);
84+
ksort($this->responses);
8085

8186
return $this->responses;
8287
}
@@ -105,7 +110,7 @@ public function testMultiRequest(){
105110

106111
// the responses are ordered
107112
// i'll probably never know why this fails on travis
108-
# $this->assertSame(['de','en','es','fr','zh','de','en','es','fr','zh'], \array_column($responses, 'lang'));
113+
# $this->assertSame(['de', 'en', 'es', 'fr', 'zh', 'de', 'en', 'es', 'fr', 'zh'], array_column($responses, 'lang'));
109114

110115
// cover the destructor
111116
unset($this->http);
@@ -119,7 +124,6 @@ public function testEmptyStackException(){
119124
$this->http->process();
120125
}
121126

122-
123127
public function testNoResponseHandlerException(){
124128
$this->expectException(ClientExceptionInterface::class);
125129
$this->expectExceptionMessage('no response handler set');

tests/HTTPOptionsTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
use PHPUnit\Framework\TestCase;
1717
use Psr\Http\Client\ClientExceptionInterface;
1818

19+
use function file_exists, ini_get, is_array;
20+
21+
use const CURLOPT_CAINFO, CURLOPT_CAPATH, CURLOPT_SSL_VERIFYHOST, CURLOPT_SSL_VERIFYPEER;
22+
1923
class HTTPOptionsTest extends TestCase{
2024

2125
public function testConvertInvalidCurlOptionsValueToArray(){
@@ -46,7 +50,7 @@ public function testCaDisable(){
4650

4751
public function testCaInfoFile(){
4852
$file = __DIR__.'/cacert.pem';
49-
$o = new HTTPOptions(['ca_info' => $file]);
53+
$o = new HTTPOptions(['ca_info' => $file]);
5054

5155
$this->assertSame($file, $o->curl_options[CURLOPT_CAINFO]);
5256
$this->assertSame(2, $o->curl_options[CURLOPT_SSL_VERIFYHOST]);
@@ -56,7 +60,7 @@ public function testCaInfoFile(){
5660

5761
public function testCaInfoDir(){
5862
$dir = __DIR__;
59-
$o = new HTTPOptions(['ca_info' => $dir]);
63+
$o = new HTTPOptions(['ca_info' => $dir]);
6064

6165
$this->assertSame($dir, $o->curl_options[CURLOPT_CAPATH]);
6266
$this->assertSame(2, $o->curl_options[CURLOPT_SSL_VERIFYHOST]);
@@ -73,7 +77,7 @@ public function testCaInfoInvalidException(){
7377

7478
public function testCurloptCaInfoFile(){
7579
$file = __DIR__.'/cacert.pem';
76-
$o = new HTTPOptions(['curl_options' => [CURLOPT_CAINFO => $file]]);
80+
$o = new HTTPOptions(['curl_options' => [CURLOPT_CAINFO => $file]]);
7781

7882
$this->assertSame($file, $o->curl_options[CURLOPT_CAINFO]);
7983
$this->assertSame(2, $o->curl_options[CURLOPT_SSL_VERIFYHOST]);
@@ -83,7 +87,7 @@ public function testCurloptCaInfoFile(){
8387

8488
public function testCurloptCaInfoDir(){
8589
$dir = __DIR__;
86-
$o = new HTTPOptions(['curl_options' => [CURLOPT_CAPATH => $dir]]);
90+
$o = new HTTPOptions(['curl_options' => [CURLOPT_CAPATH => $dir]]);
8791

8892
$this->assertSame($dir, $o->curl_options[CURLOPT_CAPATH]);
8993
$this->assertSame(2, $o->curl_options[CURLOPT_SSL_VERIFYHOST]);

tests/Psr15/PriorityQueueRequestHandlerTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212

1313
namespace chillerlan\HTTPTest\Psr15;
1414

15-
use chillerlan\HTTP\Psr17;
1615
use chillerlan\HTTP\Psr15\Middleware\{MiddlewareException, PriorityMiddleware};
1716
use chillerlan\HTTP\Psr15\PriorityQueueRequestHandler;
1817
use PHPUnit\Framework\TestCase;
1918
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
2019
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface};
2120

21+
use function array_keys;
22+
use function chillerlan\HTTP\Psr17\create_server_request_from_globals;
23+
2224
class PriorityQueueRequestHandlerTest extends TestCase{
2325

2426
public function testHandler(){
@@ -46,7 +48,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
4648
});
4749

4850
// execute it:
49-
$response = $handler->handle(Psr17\create_server_request_from_globals());
51+
$response = $handler->handle(create_server_request_from_globals());
5052

5153
// highest priority shall be processed first and go out last
5254
$this->assertSame(
@@ -72,7 +74,7 @@ public function testNestedHandler(){
7274
];
7375

7476
$handler = new PriorityQueueRequestHandler($middlewareStack);
75-
$response = $handler->handle(Psr17\create_server_request_from_globals());
77+
$response = $handler->handle(create_server_request_from_globals());
7678

7779
$this->assertSame(
7880
['X-Priority-1', 'X-Priority-11', 'X-Priority-22', 'X-Priority-33', 'X-Priority-3', 'X-Priority-4'],

tests/Psr15/QueueRequestHandlerTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
use chillerlan\HTTP\Psr15\{EmptyResponseHandler, QueueRequestHandler};
1616
use chillerlan\HTTP\Psr15\Middleware\MiddlewareException;
17-
use chillerlan\HTTP\Psr17;
1817
use chillerlan\HTTP\Psr17\ResponseFactory;
1918
use PHPUnit\Framework\TestCase;
2019
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
2120
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface};
2221

22+
use function array_keys;
23+
use function chillerlan\HTTP\Psr17\create_server_request_from_globals;
24+
2325
class QueueRequestHandlerTest extends TestCase{
2426

2527
public function testHandler(){
@@ -61,7 +63,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
6163
});
6264

6365
// execute it:
64-
$response = $handler->handle(Psr17\create_server_request_from_globals());
66+
$response = $handler->handle(create_server_request_from_globals());
6567

6668
$this->assertSame(
6769
['X-Out-First', 'X-Out-Second', 'X-Out-Third'],

tests/Psr17/FactoryHelpersTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
use function chillerlan\HTTP\Psr17\{
2121
create_uri_from_globals, create_server_request_from_globals, create_stream, create_stream_from_input
2222
};
23+
use function fopen, fseek, fwrite, simplexml_load_string, time;
24+
25+
use const UPLOAD_ERR_OK;
2326

2427
class FactoryHelpersTest extends TestCase{
2528

@@ -161,13 +164,7 @@ public function testCreateServerRequestFromGlobals(){
161164
);
162165

163166
$expectedFiles = [
164-
'file' => new UploadedFile(
165-
'/tmp/php/php1h4j1o',
166-
123,
167-
UPLOAD_ERR_OK,
168-
'MyFile.txt',
169-
'text/plain'
170-
),
167+
'file' => new UploadedFile('/tmp/php/php1h4j1o', 123, UPLOAD_ERR_OK, 'MyFile.txt', 'text/plain'),
171168
];
172169

173170
$this->assertEquals($expectedFiles, $server->getUploadedFiles());

0 commit comments

Comments
 (0)