Skip to content

Commit 8976aa5

Browse files
Replace "use-by-ref" by static vars when possible in closures
1 parent 21e3d7d commit 8976aa5

File tree

6 files changed

+21
-19
lines changed

6 files changed

+21
-19
lines changed

CurlHttpClient.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,10 @@ public function request(string $method, string $url, array $options = []): Respo
224224
if (\is_resource($body)) {
225225
$curlopts[\CURLOPT_INFILE] = $body;
226226
} else {
227-
$eof = false;
228-
$buffer = '';
229-
$curlopts[\CURLOPT_READFUNCTION] = static function ($ch, $fd, $length) use ($body, &$buffer, &$eof) {
227+
$curlopts[\CURLOPT_READFUNCTION] = static function ($ch, $fd, $length) use ($body) {
228+
static $eof = false;
229+
static $buffer = '';
230+
230231
return self::readRequestBody($length, $body, $buffer, $eof);
231232
};
232233
}

NativeHttpClient.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,8 @@ public function request(string $method, string $url, array $options = []): Respo
132132
];
133133

134134
if ($onProgress = $options['on_progress']) {
135-
// Memoize the last progress to ease calling the callback periodically when no network transfer happens
136-
$lastProgress = [0, 0];
137135
$maxDuration = 0 < $options['max_duration'] ? $options['max_duration'] : \INF;
138-
$onProgress = static function (...$progress) use ($onProgress, &$lastProgress, &$info, $maxDuration) {
136+
$onProgress = static function (...$progress) use ($onProgress, &$info, $maxDuration) {
139137
if ($info['total_time'] >= $maxDuration) {
140138
throw new TransportException(sprintf('Max duration was reached for "%s".', implode('', $info['url'])));
141139
}
@@ -144,6 +142,9 @@ public function request(string $method, string $url, array $options = []): Respo
144142
$progressInfo['url'] = implode('', $info['url']);
145143
unset($progressInfo['size_body']);
146144

145+
// Memoize the last progress to ease calling the callback periodically when no network transfer happens
146+
static $lastProgress = [0, 0];
147+
147148
if ($progress && -1 === $progress[0]) {
148149
// Response completed
149150
$lastProgress[0] = max($lastProgress);

NoPrivateNetworkHttpClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ public function request(string $method, string $url, array $options = []): Respo
7070
}
7171

7272
$subnets = $this->subnets;
73-
$lastPrimaryIp = '';
7473

75-
$options['on_progress'] = function (int $dlNow, int $dlSize, array $info) use ($onProgress, $subnets, &$lastPrimaryIp): void {
74+
$options['on_progress'] = function (int $dlNow, int $dlSize, array $info) use ($onProgress, $subnets): void {
75+
static $lastPrimaryIp = '';
7676
if ($info['primary_ip'] !== $lastPrimaryIp) {
7777
if ($info['primary_ip'] && IpUtils::checkIp($info['primary_ip'], $subnets ?? self::PRIVATE_SUBNETS)) {
7878
throw new TransportException(sprintf('IP "%s" is blocked for "%s".', $info['primary_ip'], $info['url']));

Response/AmpResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function __construct(AmpClientState $multi, Request $request, array $opti
101101
$throttleWatcher = null;
102102

103103
$this->id = $id = self::$nextId++;
104-
Loop::defer(static function () use ($request, $multi, &$id, &$info, &$headers, $canceller, &$options, $onProgress, &$handle, $logger, &$pause) {
104+
Loop::defer(static function () use ($request, $multi, $id, &$info, &$headers, $canceller, &$options, $onProgress, &$handle, $logger, &$pause) {
105105
return new Coroutine(self::generateResponse($request, $multi, $id, $info, $headers, $canceller, $options, $onProgress, $handle, $logger, $pause));
106106
});
107107

RetryableHttpClient.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public function request(string $method, string $url, array $options = []): Respo
5353
return new AsyncResponse($this->client, $method, $url, $options);
5454
}
5555

56-
$retryCount = 0;
57-
$content = '';
58-
$firstChunk = null;
56+
return new AsyncResponse($this->client, $method, $url, $options, function (ChunkInterface $chunk, AsyncContext $context) use ($method, $url, $options) {
57+
static $retryCount = 0;
58+
static $content = '';
59+
static $firstChunk;
5960

60-
return new AsyncResponse($this->client, $method, $url, $options, function (ChunkInterface $chunk, AsyncContext $context) use ($method, $url, $options, &$retryCount, &$content, &$firstChunk) {
6161
$exception = null;
6262
try {
6363
if ($context->getInfo('canceled') || $chunk->isTimeout() || null !== $chunk->getInformationalStatus()) {

Tests/AsyncDecoratorTraitTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ public function testBufferPurePassthru()
235235

236236
public function testRetryTimeout()
237237
{
238-
$cpt = 0;
239-
$client = $this->getHttpClient(__FUNCTION__, function (ChunkInterface $chunk, AsyncContext $context) use (&$cpt) {
238+
$client = $this->getHttpClient(__FUNCTION__, function (ChunkInterface $chunk, AsyncContext $context) {
239+
static $cpt = 0;
240240
try {
241241
$this->assertTrue($chunk->isTimeout());
242242
yield $chunk;
@@ -301,8 +301,8 @@ public function testInfoPassToDecorator()
301301

302302
public function testMultipleYieldInInitializer()
303303
{
304-
$first = null;
305-
$client = $this->getHttpClient(__FUNCTION__, function (ChunkInterface $chunk, AsyncContext $context) use (&$first) {
304+
$client = $this->getHttpClient(__FUNCTION__, function (ChunkInterface $chunk, AsyncContext $context) {
305+
static $first;
306306
if ($chunk->isFirst()) {
307307
$first = $chunk;
308308

@@ -343,8 +343,8 @@ public function request(string $method, string $url, array $options = []): Respo
343343

344344
public function testMaxDuration()
345345
{
346-
$sawFirst = false;
347-
$client = $this->getHttpClient(__FUNCTION__, function (ChunkInterface $chunk, AsyncContext $context) use (&$sawFirst) {
346+
$client = $this->getHttpClient(__FUNCTION__, function (ChunkInterface $chunk, AsyncContext $context) {
347+
static $sawFirst = false;
348348
try {
349349
if (!$chunk->isFirst() || !$sawFirst) {
350350
$sawFirst = $sawFirst || $chunk->isFirst();

0 commit comments

Comments
 (0)