Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/Command/BisectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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([
Copy link
Copy Markdown
Contributor Author

@staabm staabm Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to make use of this service via DI in this command we would need to call CommandHelpe::begin, and this might require more changes.

I feel its not the right time todo this now. as this command is using cli options to pass it into a subshell, which CommandHelpe::begin seems to expect to be used for the main-process

'headers' => [
'Authorization' => 'token ' . $token,
'Accept' => 'application/vnd.github.v3+json',
Expand Down
8 changes: 3 additions & 5 deletions src/Command/FixerApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use DateTime;
use DateTimeImmutable;
use DateTimeZone;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use Nette\Utils\Json;
Expand All @@ -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;
Expand Down Expand Up @@ -93,6 +93,7 @@ public function __construct(
private ?string $editorUrl,
#[AutowiredParameter]
private string $usedLevel,
private HttpClientFactory $httpClientFactory,
)
{
}
Expand Down Expand Up @@ -325,10 +326,7 @@ private function downloadPhar(
$output->writeln('<fg=green>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]));

Expand Down
43 changes: 43 additions & 0 deletions src/Internal/HttpClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types = 1);

namespace PHPStan\Internal;

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use PHPStan\DependencyInjection\AutowiredService;
use function extension_loaded;

#[AutowiredService]
final class HttpClientFactory
{

public function __construct(
private int $timeout = 30,
private int $connectTimeout = 10,
)
{
}

/**
* @param array<mixed> $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);
}

}
Loading