Skip to content

Commit 63113cc

Browse files
committed
HttpClientFactory
1 parent 4e9b7c9 commit 63113cc

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

src/Command/BisectCommand.php

Lines changed: 7 additions & 11 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;
@@ -23,7 +24,6 @@
2324
use function chmod;
2425
use function count;
2526
use function escapeshellarg;
26-
use function extension_loaded;
2727
use function getenv;
2828
use function implode;
2929
use function is_array;
@@ -107,24 +107,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
107107
return 1;
108108
}
109109

110-
$headers = [
111-
'Authorization' => 'token ' . $token,
112-
'Accept' => 'application/vnd.github.v3+json',
113-
];
114-
if (extension_loaded('zlib')) {
115-
$headers['Accept-Encoding'] = 'gzip,deflate';
116-
}
117-
118-
$client = new Client([
110+
$client = HttpClientFactory::createClient([
119111
RequestOptions::TIMEOUT => 30,
120112
RequestOptions::CONNECT_TIMEOUT => 10,
121-
'headers' => $headers,
113+
'headers' => [
114+
'Authorization' => 'token ' . $token,
115+
'Accept' => 'application/vnd.github.v3+json',
116+
],
122117
]);
123118

124119
$io->section(sprintf('Fetching commits between %s and %s...', $good, $bad));
125120

126121
try {
127122
$commits = $this->getCommitsBetween($client, $good, $bad);
123+
exit;
128124
} catch (GuzzleException $e) {
129125
$io->error(sprintf('Failed to fetch commits from GitHub: %s', $e->getMessage()));
130126
return 1;

src/Internal/HttpClientFactory.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Internal;
4+
5+
use GuzzleHttp\Client;
6+
use function extension_loaded;
7+
8+
final class HttpClientFactory
9+
{
10+
11+
/**
12+
* @see \GuzzleHttp\RequestOptions
13+
*/
14+
public static function createClient(array $config): Client
15+
{
16+
if (
17+
!isset($config['headers']['Accept-Encoding'])
18+
&& extension_loaded('zlib')
19+
) {
20+
$config['headers']['Accept-Encoding'] = 'gzip,deflate';
21+
}
22+
23+
return new Client($config);
24+
}
25+
26+
}

0 commit comments

Comments
 (0)