Skip to content

Commit 4e15f5e

Browse files
authored
bisect: advertise gzip,deflate encoding when supported
1 parent 52704a4 commit 4e15f5e

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

src/Command/BisectCommand.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Override;
1010
use PHPStan\Command\Bisect\BinarySearch;
1111
use PHPStan\File\FileReader;
12+
use PHPStan\Internal\HttpClientFactory;
1213
use Symfony\Component\Console\Command\Command;
1314
use Symfony\Component\Console\Helper\ProgressBar;
1415
use Symfony\Component\Console\Input\InputArgument;
@@ -106,9 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
106107
return 1;
107108
}
108109

109-
$client = new Client([
110-
RequestOptions::TIMEOUT => 30,
111-
RequestOptions::CONNECT_TIMEOUT => 10,
110+
$client = (new HttpClientFactory())->createClient([
112111
'headers' => [
113112
'Authorization' => 'token ' . $token,
114113
'Accept' => 'application/vnd.github.v3+json',

src/Command/FixerApplication.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use DateTime;
88
use DateTimeImmutable;
99
use DateTimeZone;
10-
use GuzzleHttp\Client;
1110
use GuzzleHttp\Exception\GuzzleException;
1211
use GuzzleHttp\RequestOptions;
1312
use Nette\Utils\Json;
@@ -23,6 +22,7 @@
2322
use PHPStan\Internal\ComposerHelper;
2423
use PHPStan\Internal\DirectoryCreator;
2524
use PHPStan\Internal\DirectoryCreatorException;
25+
use PHPStan\Internal\HttpClientFactory;
2626
use PHPStan\PhpDoc\StubFilesProvider;
2727
use PHPStan\Process\ProcessCanceledException;
2828
use PHPStan\Process\ProcessCrashedException;
@@ -93,6 +93,7 @@ public function __construct(
9393
private ?string $editorUrl,
9494
#[AutowiredParameter]
9595
private string $usedLevel,
96+
private HttpClientFactory $httpClientFactory,
9697
)
9798
{
9899
}
@@ -325,10 +326,7 @@ private function downloadPhar(
325326
$output->writeln('<fg=green>Checking if there\'s a new PHPStan Pro release...</>');
326327
}
327328

328-
$client = new Client([
329-
RequestOptions::TIMEOUT => 30,
330-
RequestOptions::CONNECT_TIMEOUT => 5,
331-
]);
329+
$client = $this->httpClientFactory->createClient([]);
332330

333331
$latestUrl = sprintf('https://fixer-download-api.phpstan.com/latest?%s', http_build_query(['phpVersion' => PHP_VERSION_ID, 'branch' => $branch]));
334332

src/Internal/HttpClientFactory.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Internal;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\RequestOptions;
7+
use PHPStan\DependencyInjection\AutowiredService;
8+
use function extension_loaded;
9+
10+
#[AutowiredService]
11+
final class HttpClientFactory
12+
{
13+
14+
public function __construct(
15+
private int $timeout = 30,
16+
private int $connectTimeout = 10,
17+
)
18+
{
19+
}
20+
21+
/**
22+
* @param array<mixed> $config
23+
*
24+
* @see \GuzzleHttp\RequestOptions
25+
*/
26+
public function createClient(array $config): Client
27+
{
28+
if (
29+
!isset($config['headers']['Accept-Encoding'])
30+
&& extension_loaded('zlib')
31+
) {
32+
$config['headers']['Accept-Encoding'] = 'gzip,deflate';
33+
}
34+
35+
$defaults = [
36+
RequestOptions::TIMEOUT => $this->timeout,
37+
RequestOptions::CONNECT_TIMEOUT => $this->connectTimeout,
38+
];
39+
40+
return new Client($config + $defaults);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)