Skip to content

Commit

Permalink
update baselines
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm committed Sep 23, 2024
1 parent 21a69ef commit f0b4112
Show file tree
Hide file tree
Showing 27 changed files with 300 additions and 290 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.php_cs.cache
.php-cs-fixer.cache
bin
composer.lock
composer.phar
Expand Down
20 changes: 20 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

$finder = (new PhpCsFixer\Finder())
->in(__DIR__ . '/lib')
->in(__DIR__ . '/tests')
->exclude('vendor')
->name('*.php');

return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => array('syntax' => 'short'),
'protected_to_private' => false,
'declare_strict_types' => true,
'no_superfluous_phpdoc_tags' => true,
'nullable_type_declaration_for_default_null_value' => false,
])
->setFinder($finder);
21 changes: 0 additions & 21 deletions .php_cs

This file was deleted.

8 changes: 4 additions & 4 deletions lib/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Browser implements BuzzClientInterface
public function __construct(BuzzClientInterface $client, $requestFactory)
{
if (!$requestFactory instanceof RequestFactoryInterface && !$requestFactory instanceof RequestFactory) {
throw new InvalidArgumentException(sprintf('Second argument of %s must be an instance of %s or %s.', __CLASS__, RequestFactoryInterface::class, RequestFactory::class));
throw new InvalidArgumentException(\sprintf('Second argument of %s must be an instance of %s or %s.', __CLASS__, RequestFactoryInterface::class, RequestFactory::class));
}

$this->client = $client;
Expand Down Expand Up @@ -222,9 +222,9 @@ private function prepareMultipart(string $name, string $content, string $boundar
$fileHeaders = [];

// Set a default content-disposition header
$fileHeaders['Content-Disposition'] = sprintf('form-data; name="%s"', $name);
$fileHeaders['Content-Disposition'] = \sprintf('form-data; name="%s"', $name);
if (isset($data['filename'])) {
$fileHeaders['Content-Disposition'] .= sprintf('; filename="%s"', $data['filename']);
$fileHeaders['Content-Disposition'] .= \sprintf('; filename="%s"', $data['filename']);
}

// Set a default content-length header
Expand All @@ -239,7 +239,7 @@ private function prepareMultipart(string $name, string $content, string $boundar
// Add start
$output .= "--$boundary\r\n";
foreach ($fileHeaders as $key => $value) {
$output .= sprintf("%s: %s\r\n", $key, $value);
$output .= \sprintf("%s: %s\r\n", $key, $value);
}
$output .= "\r\n";
$output .= $content;
Expand Down
2 changes: 1 addition & 1 deletion lib/Client/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract class AbstractClient
public function __construct($responseFactory, array $options = [])
{
if (!$responseFactory instanceof ResponseFactoryInterface && !$responseFactory instanceof ResponseFactory) {
throw new InvalidArgumentException(sprintf('First argument of %s must be an instance of %s or %s.', __CLASS__, ResponseFactoryInterface::class, ResponseFactory::class));
throw new InvalidArgumentException(\sprintf('First argument of %s must be an instance of %s or %s.', __CLASS__, ResponseFactoryInterface::class, ResponseFactory::class));
}

$this->options = new ParameterBag();
Expand Down
80 changes: 40 additions & 40 deletions lib/Client/AbstractCurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ protected function releaseHandle($curl): void
// and are not cleaned up by curl_reset. Using curl_setopt_array
// does not work for some reason, so removing each one
// individually.
curl_setopt($curl, CURLOPT_HEADERFUNCTION, null);
curl_setopt($curl, CURLOPT_READFUNCTION, null);
curl_setopt($curl, CURLOPT_WRITEFUNCTION, null);
curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, null);
curl_setopt($curl, \CURLOPT_HEADERFUNCTION, null);
curl_setopt($curl, \CURLOPT_READFUNCTION, null);
curl_setopt($curl, \CURLOPT_WRITEFUNCTION, null);
curl_setopt($curl, \CURLOPT_PROGRESSFUNCTION, null);
curl_reset($curl);

if (!\in_array($curl, $this->handles)) {
Expand All @@ -84,22 +84,22 @@ protected function releaseHandle($curl): void
protected function prepare($curl, RequestInterface $request, ParameterBag $options): ResponseBuilder
{
if (\defined('CURLOPT_PROTOCOLS')) {
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
curl_setopt($curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
curl_setopt($curl, \CURLOPT_PROTOCOLS, \CURLPROTO_HTTP | \CURLPROTO_HTTPS);
curl_setopt($curl, \CURLOPT_REDIR_PROTOCOLS, \CURLPROTO_HTTP | \CURLPROTO_HTTPS);
}

curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, \CURLOPT_HEADER, false);
curl_setopt($curl, \CURLOPT_RETURNTRANSFER, false);
curl_setopt($curl, \CURLOPT_FAILONERROR, false);

$this->setOptionsFromParameterBag($curl, $options);
$this->setOptionsFromRequest($curl, $request);

$responseBuilder = new ResponseBuilder($this->responseFactory);
curl_setopt($curl, CURLOPT_HEADERFUNCTION, function ($ch, $data) use ($responseBuilder) {
curl_setopt($curl, \CURLOPT_HEADERFUNCTION, function ($ch, $data) use ($responseBuilder) {
$str = trim($data);
if ('' !== $str) {
if (0 === strpos(strtolower($str), 'http/')) {
if (str_starts_with(strtolower($str), 'http/')) {
$responseBuilder->setStatus($str);
} else {
$responseBuilder->addHeader($str);
Expand All @@ -109,7 +109,7 @@ protected function prepare($curl, RequestInterface $request, ParameterBag $optio
return \strlen($data);
});

curl_setopt($curl, CURLOPT_WRITEFUNCTION, function ($ch, $data) use ($responseBuilder) {
curl_setopt($curl, \CURLOPT_WRITEFUNCTION, function ($ch, $data) use ($responseBuilder) {
return $responseBuilder->writeBody($data);
});

Expand All @@ -128,27 +128,27 @@ protected function prepare($curl, RequestInterface $request, ParameterBag $optio
private function setOptionsFromRequest($curl, RequestInterface $request): void
{
$options = [
CURLOPT_CUSTOMREQUEST => $request->getMethod(),
CURLOPT_URL => $request->getUri()->__toString(),
CURLOPT_HTTPHEADER => HeaderConverter::toBuzzHeaders($request->getHeaders()),
\CURLOPT_CUSTOMREQUEST => $request->getMethod(),
\CURLOPT_URL => $request->getUri()->__toString(),
\CURLOPT_HTTPHEADER => HeaderConverter::toBuzzHeaders($request->getHeaders()),
];

if (0 !== $version = $this->getProtocolVersion($request)) {
$options[CURLOPT_HTTP_VERSION] = $version;
$options[\CURLOPT_HTTP_VERSION] = $version;
}

if ($request->getUri()->getUserInfo()) {
$options[CURLOPT_USERPWD] = $request->getUri()->getUserInfo();
$options[\CURLOPT_USERPWD] = $request->getUri()->getUserInfo();
}

switch (strtoupper($request->getMethod())) {
case 'HEAD':
$options[CURLOPT_NOBODY] = true;
$options[\CURLOPT_NOBODY] = true;

break;

case 'GET':
$options[CURLOPT_HTTPGET] = true;
$options[\CURLOPT_HTTPGET] = true;

break;

Expand All @@ -167,16 +167,16 @@ private function setOptionsFromRequest($curl, RequestInterface $request): void
// Message has non empty body.
if (null === $bodySize || $bodySize > 1024 * 1024) {
// Avoid full loading large or unknown size body into memory
$options[CURLOPT_UPLOAD] = true;
$options[\CURLOPT_UPLOAD] = true;
if (null !== $bodySize) {
$options[CURLOPT_INFILESIZE] = $bodySize;
$options[\CURLOPT_INFILESIZE] = $bodySize;
}
$options[CURLOPT_READFUNCTION] = function ($ch, $fd, $length) use ($body) {
$options[\CURLOPT_READFUNCTION] = function ($ch, $fd, $length) use ($body) {
return $body->read($length);
};
} else {
// Small body can be loaded into memory
$options[CURLOPT_POSTFIELDS] = (string) $body;
$options[\CURLOPT_POSTFIELDS] = (string) $body;
}
}
}
Expand All @@ -190,16 +190,16 @@ private function setOptionsFromRequest($curl, RequestInterface $request): void
private function setOptionsFromParameterBag($curl, ParameterBag $options): void
{
if (null !== $proxy = $options->get('proxy')) {
curl_setopt($curl, CURLOPT_PROXY, $proxy);
curl_setopt($curl, \CURLOPT_PROXY, $proxy);
}

$canFollow = !ini_get('safe_mode') && !ini_get('open_basedir') && $options->get('allow_redirects');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $canFollow);
curl_setopt($curl, CURLOPT_MAXREDIRS, $canFollow ? $options->get('max_redirects') : 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $options->get('verify') ? 1 : 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $options->get('verify') ? 2 : 0);
$canFollow = !\ini_get('safe_mode') && !\ini_get('open_basedir') && $options->get('allow_redirects');
curl_setopt($curl, \CURLOPT_FOLLOWLOCATION, $canFollow);
curl_setopt($curl, \CURLOPT_MAXREDIRS, $canFollow ? $options->get('max_redirects') : 0);
curl_setopt($curl, \CURLOPT_SSL_VERIFYPEER, $options->get('verify') ? 1 : 0);
curl_setopt($curl, \CURLOPT_SSL_VERIFYHOST, $options->get('verify') ? 2 : 0);
if (0 < $options->get('timeout')) {
curl_setopt($curl, CURLOPT_TIMEOUT, $options->get('timeout'));
curl_setopt($curl, \CURLOPT_TIMEOUT, $options->get('timeout'));
}
}

Expand All @@ -213,16 +213,16 @@ private function setOptionsFromParameterBag($curl, ParameterBag $options): void
protected function parseError(RequestInterface $request, int $errno, $curl): void
{
switch ($errno) {
case CURLE_OK:
case \CURLE_OK:
// All OK, create a response object
break;
case CURLE_COULDNT_RESOLVE_PROXY:
case CURLE_COULDNT_RESOLVE_HOST:
case CURLE_COULDNT_CONNECT:
case CURLE_OPERATION_TIMEOUTED:
case CURLE_SSL_CONNECT_ERROR:
case \CURLE_COULDNT_RESOLVE_PROXY:
case \CURLE_COULDNT_RESOLVE_HOST:
case \CURLE_COULDNT_CONNECT:
case \CURLE_OPERATION_TIMEOUTED:
case \CURLE_SSL_CONNECT_ERROR:
throw new NetworkException($request, curl_error($curl), $errno);
case CURLE_ABORTED_BY_CALLBACK:
case \CURLE_ABORTED_BY_CALLBACK:
throw new CallbackException($request, curl_error($curl), $errno);
default:
throw new RequestException($request, curl_error($curl), $errno);
Expand All @@ -233,12 +233,12 @@ private function getProtocolVersion(RequestInterface $request): int
{
switch ($request->getProtocolVersion()) {
case '1.0':
return CURL_HTTP_VERSION_1_0;
return \CURL_HTTP_VERSION_1_0;
case '1.1':
return CURL_HTTP_VERSION_1_1;
return \CURL_HTTP_VERSION_1_1;
case '2.0':
if (\defined('CURL_HTTP_VERSION_2_0')) {
return CURL_HTTP_VERSION_2_0;
return \CURL_HTTP_VERSION_2_0;
}

throw new \UnexpectedValueException('libcurl 7.33 needed for HTTP 2.0 support');
Expand Down
36 changes: 18 additions & 18 deletions lib/Client/MultiCurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public function __construct($responseFactory, array $options = [])
parent::__construct($responseFactory, $options);

if (
\PHP_VERSION_ID < 70215 ||
\PHP_VERSION_ID === 70300 ||
\PHP_VERSION_ID === 70301 ||
\PHP_VERSION_ID >= 80000 ||
!(CURL_VERSION_HTTP2 & curl_version()['features'])
\PHP_VERSION_ID < 70215
|| \PHP_VERSION_ID === 70300
|| \PHP_VERSION_ID === 70301
|| \PHP_VERSION_ID >= 80000
|| !(\CURL_VERSION_HTTP2 & curl_version()['features'])
) {
// Dont use HTTP/2 push when it's unsupported or buggy, see https://bugs.php.net/76675
$this->serverPushSupported = false;
Expand Down Expand Up @@ -126,7 +126,7 @@ protected function configureOptions(OptionsResolver $resolver): void
$resolver->setAllowedTypes('callback', 'callable');

$resolver->setDefault('push_function_callback', function ($parent, $pushed, $headers) {
return CURL_PUSH_OK;
return \CURL_PUSH_OK;
});
$resolver->setAllowedTypes('push_function_callback', ['callable', 'null']);

Expand Down Expand Up @@ -175,11 +175,11 @@ public function proceed(): void
do {
// Start processing each handler in the stack
$mrc = curl_multi_exec($this->curlm, $stillRunning);
} while (CURLM_CALL_MULTI_PERFORM === $mrc);
} while (\CURLM_CALL_MULTI_PERFORM === $mrc);

while ($info = curl_multi_info_read($this->curlm)) {
// handle any completed requests
if (CURLMSG_DONE !== $info['msg']) {
if (\CURLMSG_DONE !== $info['msg']) {
continue;
}

Expand Down Expand Up @@ -230,10 +230,10 @@ public function proceed(): void
private function addPushHandle($headers, $handle)
{
foreach ($headers as $header) {
if (0 === strpos($header, ':path:')) {
if (str_starts_with($header, ':path:')) {
$path = substr($header, 6);
$url = (string) curl_getinfo($handle)['url'];
$url = str_replace((string) parse_url($url, PHP_URL_PATH), $path, $url);
$url = str_replace((string) parse_url($url, \PHP_URL_PATH), $path, $url);
$this->pushResponseHandles[$url] = $handle;
break;
}
Expand All @@ -256,7 +256,7 @@ private function handlePushedResponse($handle)

$content = curl_multi_getcontent($handle);
// Check if we got some headers, if not, we do not bother to store it.
if (0 !== $headerSize = curl_getinfo($handle, CURLINFO_HEADER_SIZE)) {
if (0 !== $headerSize = curl_getinfo($handle, \CURLINFO_HEADER_SIZE)) {
$this->pushedResponses[$found] = ['content' => $content, 'headerSize' => $headerSize];
unset($this->pushResponseHandles[$found]);
}
Expand Down Expand Up @@ -297,24 +297,24 @@ private function initMultiCurlHandle(): void
if ($this->serverPushSupported) {
$userCallbacks = $this->pushFunctions;

curl_multi_setopt($this->curlm, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
curl_multi_setopt($this->curlm, \CURLMOPT_PIPELINING, \CURLPIPE_MULTIPLEX);
// We need to use $this->pushCb[] because of a bug in PHP
curl_multi_setopt(
$this->curlm,
CURLMOPT_PUSHFUNCTION,
\CURLMOPT_PUSHFUNCTION,
$this->pushCb[] = function ($parent, $pushed, $headers) use ($userCallbacks) {
// If any callback say no, then do not accept.
foreach ($userCallbacks as $callback) {
if (CURL_PUSH_DENY === $callback($parent, $pushed, $headers)) {
return CURL_PUSH_DENY;
if (\CURL_PUSH_DENY === $callback($parent, $pushed, $headers)) {
return \CURL_PUSH_DENY;
}
}

curl_setopt($pushed, CURLOPT_RETURNTRANSFER, true);
curl_setopt($pushed, CURLOPT_HEADER, true);
curl_setopt($pushed, \CURLOPT_RETURNTRANSFER, true);
curl_setopt($pushed, \CURLOPT_HEADER, true);
$this->addPushHandle($headers, $pushed);

return CURL_PUSH_OK;
return \CURL_PUSH_OK;
}
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Message/HeaderConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public static function toBuzzHeaders(array $headers): array

foreach ($headers as $key => $values) {
if (!\is_array($values)) {
$buzz[] = sprintf('%s: %s', $key, $values);
$buzz[] = \sprintf('%s: %s', $key, $values);
} else {
foreach ($values as $value) {
$buzz[] = sprintf('%s: %s', $key, $value);
$buzz[] = \sprintf('%s: %s', $key, $value);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Message/ResponseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private function filterHeaders(array $headers): array

// Make sure they are not empty
$trimmed = trim($header);
if (false === strpos($trimmed, ':')) {
if (!str_contains($trimmed, ':')) {
continue;
}

Expand All @@ -67,8 +67,8 @@ private function filterHeaders(array $headers): array
public function setStatus(string $input): void
{
$parts = explode(' ', $input, 3);
if (\count($parts) < 2 || 0 !== strpos(strtolower($parts[0]), 'http/')) {
throw new InvalidArgumentException(sprintf('"%s" is not a valid HTTP status line', $input));
if (\count($parts) < 2 || !str_starts_with(strtolower($parts[0]), 'http/')) {
throw new InvalidArgumentException(\sprintf('"%s" is not a valid HTTP status line', $input));
}

$this->response = $this->response->withStatus((int) $parts[1], isset($parts[2]) ? $parts[2] : '');
Expand Down
2 changes: 1 addition & 1 deletion lib/Middleware/BasicAuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(string $username, string $password)

public function handleRequest(RequestInterface $request, callable $next)
{
$request = $request->withAddedHeader('Authorization', sprintf('Basic %s', base64_encode($this->username.':'.$this->password)));
$request = $request->withAddedHeader('Authorization', \sprintf('Basic %s', base64_encode($this->username.':'.$this->password)));

return $next($request);
}
Expand Down
Loading

0 comments on commit f0b4112

Please sign in to comment.