Skip to content

Commit eb04098

Browse files
Minor improvements
Added marker exception interface. Some code cleaning. Closes #9, Closes #8, Closes #6, Closes #5
1 parent 90e29e4 commit eb04098

File tree

8 files changed

+49
-28
lines changed

8 files changed

+49
-28
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"psr-4": {
1515
"MNC\\Http\\": "src"
1616
},
17-
"files": ["functions.php"]
17+
"files": [
18+
"src/functions.php"
19+
]
1820
},
1921
"autoload-dev": {
2022
"psr-4": {

phpunit.xml.dist

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,8 @@
66
<coverage>
77
<include>
88
<directory>src</directory>
9-
<file>functions.php</file>
109
</include>
1110
</coverage>
12-
13-
<php>
14-
<server name="TEST_SERVER" value="http://127.0.0.1:10000/server.php"/>
15-
</php>
16-
1711
<testsuites>
1812
<testsuite name="Main Test Suite">
1913
<directory>tests</directory>

src/FetchError.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the https://github.com/mnavarrocarter/php-fetch project.
5+
* (c) Matías Navarro-Carter <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace MNC\Http;
11+
12+
use Throwable;
13+
14+
/**
15+
* Interface FetchError its just a marker for this library exceptions.
16+
*/
17+
interface FetchError extends Throwable
18+
{
19+
}

src/HttpPartialResponse.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class HttpPartialResponse implements PartialResponse
2121
private Headers $headers;
2222

2323
/**
24-
* @param list<string> $lines
24+
* @param array<string> $lines
2525
*
2626
* @return list<HttpPartialResponse>
2727
*/
@@ -30,11 +30,13 @@ public static function parseLines(array $lines): array
3030
$partials = [];
3131
while ($lines !== []) {
3232
$line = array_shift($lines);
33-
if (strpos($line, 'HTTP') === 0) {
34-
$status = Status::fromStatusLine($line);
35-
$headers = Headers::fromLines($lines);
36-
$partials[] = new HttpPartialResponse($status, $headers);
33+
if (strpos($line, 'HTTP') !== 0) {
34+
continue;
3735
}
36+
$partials[] = new HttpPartialResponse(
37+
Status::fromStatusLine($line),
38+
Headers::fromLines($lines)
39+
);
3840
}
3941

4042
return $partials;

src/ProtocolError.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* Not throwing exceptions means we are not following the rules of the protocol
2929
* we are trying to implement.
3030
*/
31-
class ProtocolError extends Exception
31+
class ProtocolError extends Exception implements FetchError
3232
{
3333
private Response $response;
3434

src/RedirectedHttpResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class RedirectedHttpResponse implements Response, Redirected
1818
{
1919
private Response $response;
2020
/**
21-
* @var list<PartialResponse>
21+
* @var array<PartialResponse>
2222
*/
2323
private array $previous;
2424

src/SocketError.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
* or network error), dns unable to resolve to an ip address or the server
2020
* being down.
2121
*/
22-
class SocketError extends Exception
22+
class SocketError extends Exception implements FetchError
2323
{
2424
}

functions.php renamed to src/functions.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22

33
declare(strict_types=1);
44

5+
/*
6+
* This file is part of the https://github.com/mnavarrocarter/php-fetch project.
7+
* (c) Matías Navarro-Carter <[email protected]>
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
512
namespace MNC\Http;
613

714
use MNC\Http\Io\Reader;
815
use MNC\Http\Io\ResourceReader;
916

1017
/**
11-
* Fetches a url
12-
*
13-
* @param string $url
14-
* @param array $options
15-
* @return Response
18+
* Fetches a url.
1619
*
1720
* @throws ProtocolError when the server responds with an error
18-
* @throws SocketError when a connection cannot be established
21+
* @throws SocketError when a connection cannot be established
1922
*/
2023
function fetch(string $url, array $options = []): Response
2124
{
@@ -26,7 +29,6 @@ function fetch(string $url, array $options = []): Response
2629
$maxRedirects = $options['max_redirects'] ?? 20;
2730
$protocolVersion = $options['protocol_version'] ?? '1.1';
2831

29-
// TODO: Need to add more options like cache, redirects following and others
3032
$context = [
3133
'http' => [
3234
'method' => $method,
@@ -35,13 +37,13 @@ function fetch(string $url, array $options = []): Response
3537
'ignore_errors' => true,
3638
'follow_location' => $followRedirects ? 1 : 0,
3739
'max_redirects' => $maxRedirects,
38-
'protocol_version' => (float) $protocolVersion
39-
]
40+
'protocol_version' => (float) $protocolVersion,
41+
],
4042
];
4143

4244
$resource = @fopen($url, 'rb', false, stream_context_create($context));
4345
if (!is_resource($resource)) {
44-
throw new SocketError(error_get_last()['message']);
46+
throw new SocketError(error_get_last()['message'] ?? 'Unknown error');
4547
}
4648
stream_set_blocking($resource, false);
4749

@@ -70,14 +72,16 @@ function fetch(string $url, array $options = []): Response
7072
}
7173

7274
/**
73-
* @param Reader $reader
7475
* @return string The buffered string
76+
*
7577
* @throws Io\ReaderError
7678
*/
77-
function buffer(Reader $reader) {
79+
function buffer(Reader $reader)
80+
{
7881
$buffer = '';
7982
while (($chunk = $reader->read()) !== null) {
8083
$buffer .= $chunk;
8184
}
85+
8286
return $buffer;
83-
}
87+
}

0 commit comments

Comments
 (0)