diff --git a/src/Command/BisectCommand.php b/src/Command/BisectCommand.php index 8cb0c0382d..9c1e3932d0 100644 --- a/src/Command/BisectCommand.php +++ b/src/Command/BisectCommand.php @@ -9,6 +9,7 @@ use Override; use PHPStan\Command\Bisect\BinarySearch; use PHPStan\File\FileReader; +use PHPStan\Internal\HttpClientFactory; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Input\InputArgument; @@ -106,9 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 1; } - $client = new Client([ - RequestOptions::TIMEOUT => 30, - RequestOptions::CONNECT_TIMEOUT => 10, + $client = (new HttpClientFactory())->createClient([ 'headers' => [ 'Authorization' => 'token ' . $token, 'Accept' => 'application/vnd.github.v3+json', diff --git a/src/Command/FixerApplication.php b/src/Command/FixerApplication.php index 8d6432258c..61f89f155b 100644 --- a/src/Command/FixerApplication.php +++ b/src/Command/FixerApplication.php @@ -7,7 +7,6 @@ use DateTime; use DateTimeImmutable; use DateTimeZone; -use GuzzleHttp\Client; use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\RequestOptions; use Nette\Utils\Json; @@ -23,6 +22,7 @@ use PHPStan\Internal\ComposerHelper; use PHPStan\Internal\DirectoryCreator; use PHPStan\Internal\DirectoryCreatorException; +use PHPStan\Internal\HttpClientFactory; use PHPStan\PhpDoc\StubFilesProvider; use PHPStan\Process\ProcessCanceledException; use PHPStan\Process\ProcessCrashedException; @@ -93,6 +93,7 @@ public function __construct( private ?string $editorUrl, #[AutowiredParameter] private string $usedLevel, + private HttpClientFactory $httpClientFactory, ) { } @@ -325,10 +326,7 @@ private function downloadPhar( $output->writeln('Checking if there\'s a new PHPStan Pro release...'); } - $client = new Client([ - RequestOptions::TIMEOUT => 30, - RequestOptions::CONNECT_TIMEOUT => 5, - ]); + $client = $this->httpClientFactory->createClient([]); $latestUrl = sprintf('https://fixer-download-api.phpstan.com/latest?%s', http_build_query(['phpVersion' => PHP_VERSION_ID, 'branch' => $branch])); diff --git a/src/Internal/HttpClientFactory.php b/src/Internal/HttpClientFactory.php new file mode 100644 index 0000000000..335b45b950 --- /dev/null +++ b/src/Internal/HttpClientFactory.php @@ -0,0 +1,43 @@ + $config + * + * @see \GuzzleHttp\RequestOptions + */ + public function createClient(array $config): Client + { + if ( + !isset($config['headers']['Accept-Encoding']) + && extension_loaded('zlib') + ) { + $config['headers']['Accept-Encoding'] = 'gzip,deflate'; + } + + $defaults = [ + RequestOptions::TIMEOUT => $this->timeout, + RequestOptions::CONNECT_TIMEOUT => $this->connectTimeout, + ]; + + return new Client($config + $defaults); + } + +}