Skip to content

Commit af74942

Browse files
tigitznicolas-grekas
authored andcommitted
Leverage arrow function syntax for closure
1 parent e602b73 commit af74942

13 files changed

+35
-79
lines changed

CurlHttpClient.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -382,14 +382,10 @@ private static function createRedirectResolver(array $options, string $host, int
382382
if (0 < $options['max_redirects']) {
383383
$redirectHeaders['host'] = $host;
384384
$redirectHeaders['port'] = $port;
385-
$redirectHeaders['with_auth'] = $redirectHeaders['no_auth'] = array_filter($options['headers'], static function ($h) {
386-
return 0 !== stripos($h, 'Host:');
387-
});
385+
$redirectHeaders['with_auth'] = $redirectHeaders['no_auth'] = array_filter($options['headers'], static fn ($h) => 0 !== stripos($h, 'Host:'));
388386

389387
if (isset($options['normalized_headers']['authorization'][0]) || isset($options['normalized_headers']['cookie'][0])) {
390-
$redirectHeaders['no_auth'] = array_filter($options['headers'], static function ($h) {
391-
return 0 !== stripos($h, 'Authorization:') && 0 !== stripos($h, 'Cookie:');
392-
});
388+
$redirectHeaders['no_auth'] = array_filter($options['headers'], static fn ($h) => 0 !== stripos($h, 'Authorization:') && 0 !== stripos($h, 'Cookie:'));
393389
}
394390
}
395391

@@ -401,9 +397,7 @@ private static function createRedirectResolver(array $options, string $host, int
401397
}
402398

403399
if ($noContent && $redirectHeaders) {
404-
$filterContentHeaders = static function ($h) {
405-
return 0 !== stripos($h, 'Content-Length:') && 0 !== stripos($h, 'Content-Type:') && 0 !== stripos($h, 'Transfer-Encoding:');
406-
};
400+
$filterContentHeaders = static fn ($h) => 0 !== stripos($h, 'Content-Length:') && 0 !== stripos($h, 'Content-Type:') && 0 !== stripos($h, 'Transfer-Encoding:');
407401
$redirectHeaders['no_auth'] = array_filter($redirectHeaders['no_auth'], $filterContentHeaders);
408402
$redirectHeaders['with_auth'] = array_filter($redirectHeaders['with_auth'], $filterContentHeaders);
409403
}
@@ -431,9 +425,7 @@ private static function createRedirectResolver(array $options, string $host, int
431425

432426
private function findConstantName(int $opt): ?string
433427
{
434-
$constants = array_filter(get_defined_constants(), static function ($v, $k) use ($opt) {
435-
return $v === $opt && 'C' === $k[0] && (str_starts_with($k, 'CURLOPT_') || str_starts_with($k, 'CURLINFO_'));
436-
}, \ARRAY_FILTER_USE_BOTH);
428+
$constants = array_filter(get_defined_constants(), static fn ($v, $k) => $v === $opt && 'C' === $k[0] && (str_starts_with($k, 'CURLOPT_') || str_starts_with($k, 'CURLINFO_')), \ARRAY_FILTER_USE_BOTH);
437429

438430
return key($constants);
439431
}

HttpClientTrait.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -330,19 +330,17 @@ private static function normalizeBody($body)
330330
return $body;
331331
}
332332

333-
$generatorToCallable = static function (\Generator $body): \Closure {
334-
return static function () use ($body) {
335-
while ($body->valid()) {
336-
$chunk = $body->current();
337-
$body->next();
338-
339-
if ('' !== $chunk) {
340-
return $chunk;
341-
}
333+
$generatorToCallable = static fn (\Generator $body): \Closure => static function () use ($body) {
334+
while ($body->valid()) {
335+
$chunk = $body->current();
336+
$body->next();
337+
338+
if ('' !== $chunk) {
339+
return $chunk;
342340
}
341+
}
343342

344-
return '';
345-
};
343+
return '';
346344
};
347345

348346
if ($body instanceof \Generator) {
@@ -536,11 +534,11 @@ private static function parseUrl(string $url, array $query = [], array $allowedS
536534

537535
if (str_contains($parts[$part], '%')) {
538536
// https://tools.ietf.org/html/rfc3986#section-2.3
539-
$parts[$part] = preg_replace_callback('/%(?:2[DE]|3[0-9]|[46][1-9A-F]|5F|[57][0-9A]|7E)++/i', function ($m) { return rawurldecode($m[0]); }, $parts[$part]);
537+
$parts[$part] = preg_replace_callback('/%(?:2[DE]|3[0-9]|[46][1-9A-F]|5F|[57][0-9A]|7E)++/i', fn ($m) => rawurldecode($m[0]), $parts[$part]);
540538
}
541539

542540
// https://tools.ietf.org/html/rfc3986#section-3.3
543-
$parts[$part] = preg_replace_callback("#[^-A-Za-z0-9._~!$&/'()*+,;=:@%]++#", function ($m) { return rawurlencode($m[0]); }, $parts[$part]);
541+
$parts[$part] = preg_replace_callback("#[^-A-Za-z0-9._~!$&/'()*+,;=:@%]++#", fn ($m) => rawurlencode($m[0]), $parts[$part]);
544542
}
545543

546544
return [

Internal/AmpClientState.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ public function request(array $options, Request $request, CancellationToken $can
9494
}
9595

9696
$request->addEventListener(new AmpListener($info, $options['peer_fingerprint']['pin-sha256'] ?? [], $onProgress, $handle));
97-
$request->setPushHandler(function ($request, $response) use ($options): Promise {
98-
return $this->handlePush($request, $response, $options);
99-
});
97+
$request->setPushHandler(fn ($request, $response): Promise => $this->handlePush($request, $response, $options));
10098

10199
($request->hasHeader('content-length') ? new Success((int) $request->getHeader('content-length')) : $request->getBody()->getBodyLength())
102100
->onResolve(static function ($e, $bodySize) use (&$info) {

Internal/CurlClientState.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ public function __construct(int $maxHostConnections, int $maxPendingPushes)
7474
$multi->handlesActivity = &$this->handlesActivity;
7575
$multi->openHandles = &$this->openHandles;
7676

77-
curl_multi_setopt($this->handle, \CURLMOPT_PUSHFUNCTION, static function ($parent, $pushed, array $requestHeaders) use ($multi, $maxPendingPushes) {
78-
return $multi->handlePush($parent, $pushed, $requestHeaders, $maxPendingPushes);
79-
});
77+
curl_multi_setopt($this->handle, \CURLMOPT_PUSHFUNCTION, static fn ($parent, $pushed, array $requestHeaders) => $multi->handlePush($parent, $pushed, $requestHeaders, $maxPendingPushes));
8078
}
8179

8280
public function reset()

NativeHttpClient.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public function request(string $method, string $url, array $options = []): Respo
223223
'allow_self_signed' => (bool) $options['peer_fingerprint'],
224224
'SNI_enabled' => true,
225225
'disable_compression' => true,
226-
], static function ($v) { return null !== $v; }),
226+
], static fn ($v) => null !== $v),
227227
'socket' => [
228228
'bindto' => $options['bindto'],
229229
'tcp_nodelay' => true,
@@ -342,14 +342,10 @@ private static function createRedirectResolver(array $options, string $host, str
342342
$redirectHeaders = [];
343343
if (0 < $maxRedirects = $options['max_redirects']) {
344344
$redirectHeaders = ['host' => $host, 'port' => $port];
345-
$redirectHeaders['with_auth'] = $redirectHeaders['no_auth'] = array_filter($options['headers'], static function ($h) {
346-
return 0 !== stripos($h, 'Host:');
347-
});
345+
$redirectHeaders['with_auth'] = $redirectHeaders['no_auth'] = array_filter($options['headers'], static fn ($h) => 0 !== stripos($h, 'Host:'));
348346

349347
if (isset($options['normalized_headers']['authorization']) || isset($options['normalized_headers']['cookie'])) {
350-
$redirectHeaders['no_auth'] = array_filter($redirectHeaders['no_auth'], static function ($h) {
351-
return 0 !== stripos($h, 'Authorization:') && 0 !== stripos($h, 'Cookie:');
352-
});
348+
$redirectHeaders['no_auth'] = array_filter($redirectHeaders['no_auth'], static fn ($h) => 0 !== stripos($h, 'Authorization:') && 0 !== stripos($h, 'Cookie:'));
353349
}
354350
}
355351

@@ -386,9 +382,7 @@ private static function createRedirectResolver(array $options, string $host, str
386382
if ('POST' === $options['method'] || 303 === $info['http_code']) {
387383
$info['http_method'] = $options['method'] = 'HEAD' === $options['method'] ? 'HEAD' : 'GET';
388384
$options['content'] = '';
389-
$filterContentHeaders = static function ($h) {
390-
return 0 !== stripos($h, 'Content-Length:') && 0 !== stripos($h, 'Content-Type:') && 0 !== stripos($h, 'Transfer-Encoding:');
391-
};
385+
$filterContentHeaders = static fn ($h) => 0 !== stripos($h, 'Content-Length:') && 0 !== stripos($h, 'Content-Type:') && 0 !== stripos($h, 'Transfer-Encoding:');
392386
$options['header'] = array_filter($options['header'], $filterContentHeaders);
393387
$redirectHeaders['no_auth'] = array_filter($redirectHeaders['no_auth'], $filterContentHeaders);
394388
$redirectHeaders['with_auth'] = array_filter($redirectHeaders['with_auth'], $filterContentHeaders);

Response/AmpResponse.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ public function __construct(AmpClientState $multi, Request $request, array $opti
6767
$request->setHeader('Accept-Encoding', 'gzip');
6868
}
6969

70-
$this->initializer = static function (self $response) {
71-
return null !== $response->options;
72-
};
70+
$this->initializer = static fn (self $response) => null !== $response->options;
7371

7472
$info = &$this->info;
7573
$headers = &$this->headers;

Response/HttplugPromise.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ private function wrapThenCallback(?callable $callback): ?callable
6868
return null;
6969
}
7070

71-
return static function ($value) use ($callback) {
72-
return Create::promiseFor($callback($value));
73-
};
71+
return static fn ($value) => Create::promiseFor($callback($value));
7472
}
7573
}

Response/MockResponse.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ public static function fromRequest(string $method, string $url, array $options,
121121
$response->requestOptions = $options;
122122
$response->id = ++self::$idSequence;
123123
$response->shouldBuffer = $options['buffer'] ?? true;
124-
$response->initializer = static function (self $response) {
125-
return \is_array($response->body[0] ?? null);
126-
};
124+
$response->initializer = static fn (self $response) => \is_array($response->body[0] ?? null);
127125

128126
$response->info['redirect_count'] = 0;
129127
$response->info['redirect_url'] = null;

Response/NativeResponse.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ public function __construct(NativeClientState $multi, $context, string $url, arr
7171
$info['max_duration'] = $options['max_duration'];
7272
++$multi->responseCount;
7373

74-
$this->initializer = static function (self $response) {
75-
return null === $response->remaining;
76-
};
74+
$this->initializer = static fn (self $response) => null === $response->remaining;
7775

7876
$pauseExpiry = &$this->pauseExpiry;
7977
$info['pause_handler'] = static function (float $duration) use (&$pauseExpiry) {

Tests/HttplugClientTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,7 @@ function (\Exception $exception) use ($errorMessage, &$failureCallableCalled, $c
251251
$failureCallableCalled = true;
252252

253253
// Ensure arbitrary levels of promises work.
254-
return (new FulfilledPromise(null))->then(function () use ($client, $request) {
255-
return (new GuzzleFulfilledPromise(null))->then(function () use ($client, $request) {
256-
return $client->sendAsyncRequest($request);
257-
});
258-
});
254+
return (new FulfilledPromise(null))->then(fn () => (new GuzzleFulfilledPromise(null))->then(fn () => $client->sendAsyncRequest($request)));
259255
}
260256
)
261257
;

0 commit comments

Comments
 (0)