Skip to content

Commit 1d08605

Browse files
minor #48793 Leverage arrow function syntax for closure (tigitz)
This PR was merged into the 6.3 branch. Discussion ---------- Leverage arrow function syntax for closure | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #47658 <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT | Doc PR | <!-- required for new features --> Rationale in the RFC [here](https://wiki.php.net/rfc/arrow_functions_v2#introduction) It's also notable that using arrow function syntax rather than the classic one has been enforced in the past by symfony core member: symfony/symfony#48069 (comment) So this PR would be consistent. Commits ------- f5802d3a2a Leverage arrow function syntax for closure
2 parents 071bfef + af74942 commit 1d08605

13 files changed

+35
-79
lines changed

CurlHttpClient.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -389,14 +389,10 @@ private static function createRedirectResolver(array $options, string $host, int
389389
if (0 < $options['max_redirects']) {
390390
$redirectHeaders['host'] = $host;
391391
$redirectHeaders['port'] = $port;
392-
$redirectHeaders['with_auth'] = $redirectHeaders['no_auth'] = array_filter($options['headers'], static function ($h) {
393-
return 0 !== stripos($h, 'Host:');
394-
});
392+
$redirectHeaders['with_auth'] = $redirectHeaders['no_auth'] = array_filter($options['headers'], static fn ($h) => 0 !== stripos($h, 'Host:'));
395393

396394
if (isset($options['normalized_headers']['authorization'][0]) || isset($options['normalized_headers']['cookie'][0])) {
397-
$redirectHeaders['no_auth'] = array_filter($options['headers'], static function ($h) {
398-
return 0 !== stripos($h, 'Authorization:') && 0 !== stripos($h, 'Cookie:');
399-
});
395+
$redirectHeaders['no_auth'] = array_filter($options['headers'], static fn ($h) => 0 !== stripos($h, 'Authorization:') && 0 !== stripos($h, 'Cookie:'));
400396
}
401397
}
402398

@@ -408,9 +404,7 @@ private static function createRedirectResolver(array $options, string $host, int
408404
}
409405

410406
if ($noContent && $redirectHeaders) {
411-
$filterContentHeaders = static function ($h) {
412-
return 0 !== stripos($h, 'Content-Length:') && 0 !== stripos($h, 'Content-Type:') && 0 !== stripos($h, 'Transfer-Encoding:');
413-
};
407+
$filterContentHeaders = static fn ($h) => 0 !== stripos($h, 'Content-Length:') && 0 !== stripos($h, 'Content-Type:') && 0 !== stripos($h, 'Transfer-Encoding:');
414408
$redirectHeaders['no_auth'] = array_filter($redirectHeaders['no_auth'], $filterContentHeaders);
415409
$redirectHeaders['with_auth'] = array_filter($redirectHeaders['with_auth'], $filterContentHeaders);
416410
}
@@ -438,9 +432,7 @@ private static function createRedirectResolver(array $options, string $host, int
438432

439433
private function findConstantName(int $opt): ?string
440434
{
441-
$constants = array_filter(get_defined_constants(), static function ($v, $k) use ($opt) {
442-
return $v === $opt && 'C' === $k[0] && (str_starts_with($k, 'CURLOPT_') || str_starts_with($k, 'CURLINFO_'));
443-
}, \ARRAY_FILTER_USE_BOTH);
435+
$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);
444436

445437
return key($constants);
446438
}

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)