Skip to content

Commit 6c0fee2

Browse files
Merge pull request #14 from szepeviktor/further-impr
Further optimize the code
2 parents 36c1df8 + 5f3f41d commit 6c0fee2

File tree

2 files changed

+27
-44
lines changed

2 files changed

+27
-44
lines changed

src/Headers.php

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Headers
2727
public static function fromLines(array &$lines): Headers
2828
{
2929
$headers = new self();
30-
while (count($lines) !== 0) {
30+
while ($lines !== []) {
3131
$line = array_shift($lines);
3232
if (strpos($line, 'HTTP') === 0) {
3333
array_unshift($lines, $line); // Put the line back
@@ -63,60 +63,49 @@ public function __construct()
6363

6464
protected function put(string $name, string $value): void
6565
{
66-
$name = strtolower($name);
67-
$this->headers[$name] = $value;
66+
$this->headers[strtolower($name)] = $value;
6867
}
6968

7069
/**
7170
* Returns a header.
7271
*/
7372
public function get(string $name): string
7473
{
75-
$name = strtolower($name);
76-
77-
return $this->headers[$name] ?? '';
74+
return $this->headers[strtolower($name)] ?? '';
7875
}
7976

8077
public function contains(string $name, string $substring): bool
8178
{
82-
$name = strtolower($name);
83-
84-
return strpos($this->get($name), $substring) !== false;
79+
return strpos($this->get(strtolower($name)), $substring) !== false;
8580
}
8681

8782
public function has(string $name): bool
8883
{
89-
$name = strtolower($name);
90-
91-
return array_key_exists($name, $this->headers);
84+
return array_key_exists(strtolower($name), $this->headers);
9285
}
9386

9487
/**
9588
* @return list<string>
9689
*/
9790
public function map(callable $callable): array
9891
{
99-
$arr = [];
100-
foreach ($this->headers as $name => $value) {
101-
$arr[] = $callable($value, $name);
102-
}
103-
104-
return $arr;
92+
return array_map(
93+
fn (string $name, string $value) => $callable($value, $name),
94+
array_keys($this->headers),
95+
$this->headers
96+
);
10597
}
10698

10799
/**
108100
* @return array<string, string>
109101
*/
110102
public function filter(callable $callable): array
111103
{
112-
$arr = [];
113-
foreach ($this->headers as $name => $value) {
114-
if ($callable($value, $name) === true) {
115-
$arr[$name] = $value;
116-
}
117-
}
118-
119-
return $arr;
104+
return array_filter(
105+
$this->headers,
106+
fn (string $value, string $name) => $callable($value, $name),
107+
ARRAY_FILTER_USE_BOTH
108+
);
120109
}
121110

122111
/**

src/functions.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,22 @@
1717
/**
1818
* Fetches a url.
1919
*
20+
* @param array<string, mixed> $options
21+
*
2022
* @throws ProtocolError when the server responds with an error
2123
* @throws SocketError when a connection cannot be established
2224
*/
2325
function fetch(string $url, array $options = []): Response
2426
{
25-
$method = $options['method'] ?? 'GET';
26-
$headers = $options['headers'] ?? [];
27-
$body = $options['body'] ?? null;
28-
$followRedirects = $options['follow_redirects'] ?? true;
29-
$maxRedirects = $options['max_redirects'] ?? 20;
30-
$protocolVersion = $options['protocol_version'] ?? '1.1';
31-
3227
$context = [
3328
'http' => [
34-
'method' => $method,
35-
'header' => Headers::fromMap($headers)->toArray(),
36-
'contents' => $body,
29+
'method' => $options['method'] ?? 'GET',
30+
'header' => Headers::fromMap($options['headers'] ?? [])->toArray(),
31+
'contents' => $options['body'] ?? null,
3732
'ignore_errors' => true,
38-
'follow_location' => $followRedirects ? 1 : 0,
39-
'max_redirects' => $maxRedirects,
40-
'protocol_version' => (float) $protocolVersion,
33+
'follow_location' => ($options['follow_redirects'] ?? true) ? 1 : 0,
34+
'max_redirects' => $options['max_redirects'] ?? 20,
35+
'protocol_version' => (float) ($options['protocol_version'] ?? '1.1'),
4136
],
4237
];
4338

@@ -49,17 +44,16 @@ function fetch(string $url, array $options = []): Response
4944

5045
// We extract relevant stream meta data
5146
$meta = stream_get_meta_data($resource);
52-
$rawHeaders = $meta['wrapper_data'];
5347

5448
// We create objects out of that data.
55-
$partials = HttpPartialResponse::parseLines($rawHeaders);
49+
$partials = HttpPartialResponse::parseLines($meta['wrapper_data']);
50+
/** @var HttpPartialResponse $mainPartial */
5651
$mainPartial = array_pop($partials);
57-
$body = new ResourceReader($resource);
58-
$response = new HttpResponse($mainPartial, $body);
52+
$response = new HttpResponse($mainPartial, new ResourceReader($resource));
5953

6054
// If there are still partials, we are dealing with a redirect here.
6155
// We decorate the response on previous request.
62-
if (count($partials) > 0) {
56+
if ($partials !== []) {
6357
$response = new RedirectedHttpResponse($response, ...$partials);
6458
}
6559

0 commit comments

Comments
 (0)