Skip to content

Commit

Permalink
Adding some highly opinionated CS fixes (guzzle#2797)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm authored Oct 9, 2020
1 parent 8d850e6 commit c8c99e3
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 241 deletions.
5 changes: 1 addition & 4 deletions src/Cookie/CookieJarInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ public function withCookieHeader(RequestInterface $request): RequestInterface;
* @param RequestInterface $request Request that was sent
* @param ResponseInterface $response Response that was received
*/
public function extractCookies(
RequestInterface $request,
ResponseInterface $response
): void;
public function extractCookies(RequestInterface $request, ResponseInterface $response): void;

/**
* Sets a cookie in the cookie jar.
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/RequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public function __construct(
*/
public static function wrapException(RequestInterface $request, \Throwable $e): RequestException
{
return $e instanceof RequestException
? $e
: new RequestException($e->getMessage(), $request, null, $e);
return $e instanceof RequestException ? $e : new RequestException($e->getMessage(), $request, null, $e);
}

/**
Expand Down
59 changes: 15 additions & 44 deletions src/Handler/CurlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ public function create(RequestInterface $request, array $options): EasyHandle
}

$conf[\CURLOPT_HEADERFUNCTION] = $this->createHeaderFn($easy);
$easy->handle = $this->handles
? \array_pop($this->handles)
: \curl_init();
$easy->handle = $this->handles ? \array_pop($this->handles) : \curl_init();
curl_setopt_array($easy->handle, $conf);

return $easy;
Expand Down Expand Up @@ -102,11 +100,8 @@ public function release(EasyHandle $easy): void
* @param callable(RequestInterface, array): PromiseInterface $handler
* @param CurlFactoryInterface $factory Dictates how the handle is released
*/
public static function finish(
callable $handler,
EasyHandle $easy,
CurlFactoryInterface $factory
): PromiseInterface {
public static function finish(callable $handler, EasyHandle $easy, CurlFactoryInterface $factory): PromiseInterface
{
if (isset($easy->options['on_stats'])) {
self::invokeStats($easy);
}
Expand Down Expand Up @@ -144,11 +139,8 @@ private static function invokeStats(EasyHandle $easy): void
/**
* @param callable(RequestInterface, array): PromiseInterface $handler
*/
private static function finishError(
callable $handler,
EasyHandle $easy,
CurlFactoryInterface $factory
): PromiseInterface {
private static function finishError(callable $handler, EasyHandle $easy, CurlFactoryInterface $factory): PromiseInterface
{
// Get error information and release the handle to the factory.
$ctx = [
'errno' => $easy->errno,
Expand All @@ -159,9 +151,7 @@ private static function finishError(
$factory->release($easy);

// Retry when nothing is present or when curl failed to rewind.
if (empty($easy->options['_err_message'])
&& (!$easy->errno || $easy->errno == 65)
) {
if (empty($easy->options['_err_message']) && (!$easy->errno || $easy->errno == 65)) {
return self::retryFailedRewind($handler, $easy, $ctx);
}

Expand Down Expand Up @@ -288,9 +278,7 @@ private function applyBody(RequestInterface $request, array $options, array &$co

// Send the body as a string if the size is less than 1MB OR if the
// [curl][body_as_string] request value is set.
if (($size !== null && $size < 1000000) ||
!empty($options['_body_as_string'])
) {
if (($size !== null && $size < 1000000) || !empty($options['_body_as_string'])) {
$conf[\CURLOPT_POSTFIELDS] = (string) $request->getBody();
// Don't duplicate the Content-Length header
$this->removeHeader('Content-Length', $conf);
Expand Down Expand Up @@ -372,9 +360,7 @@ private function applyHandlerOptions(EasyHandle $easy, array &$conf): void
if (\is_string($options['verify'])) {
// Throw an error if the file/folder/link path is not valid or doesn't exist.
if (!\file_exists($options['verify'])) {
throw new \InvalidArgumentException(
"SSL CA bundle not found: {$options['verify']}"
);
throw new \InvalidArgumentException("SSL CA bundle not found: {$options['verify']}");
}
// If it's a directory or a link to a directory use CURLOPT_CAPATH.
// If not, it's probably a file, or a link to a file, so use CURLOPT_CAINFO.
Expand Down Expand Up @@ -414,11 +400,7 @@ private function applyHandlerOptions(EasyHandle $easy, array &$conf): void
$sink = \GuzzleHttp\Psr7\stream_for($sink);
} elseif (!\is_dir(\dirname($sink))) {
// Ensure that the directory exists before failing in curl.
throw new \RuntimeException(\sprintf(
'Directory %s does not exist for sink value of %s',
\dirname($sink),
$sink
));
throw new \RuntimeException(\sprintf('Directory %s does not exist for sink value of %s', \dirname($sink), $sink));
} else {
$sink = new LazyOpenStream($sink, 'w+');
}
Expand Down Expand Up @@ -458,9 +440,7 @@ private function applyHandlerOptions(EasyHandle $easy, array &$conf): void
$scheme = $easy->request->getUri()->getScheme();
if (isset($options['proxy'][$scheme])) {
$host = $easy->request->getUri()->getHost();
if (!isset($options['proxy']['no']) ||
!Utils::isHostInNoProxy($host, $options['proxy']['no'])
) {
if (!isset($options['proxy']['no']) || !Utils::isHostInNoProxy($host, $options['proxy']['no'])) {
$conf[\CURLOPT_PROXY] = $options['proxy'][$scheme];
}
}
Expand All @@ -474,9 +454,7 @@ private function applyHandlerOptions(EasyHandle $easy, array &$conf): void
$cert = $cert[0];
}
if (!\file_exists($cert)) {
throw new \InvalidArgumentException(
"SSL certificate not found: {$cert}"
);
throw new \InvalidArgumentException("SSL certificate not found: {$cert}");
}
$conf[\CURLOPT_SSLCERT] = $cert;
}
Expand All @@ -493,19 +471,15 @@ private function applyHandlerOptions(EasyHandle $easy, array &$conf): void
$sslKey = $sslKey ?? $options['ssl_key'];

if (!\file_exists($sslKey)) {
throw new \InvalidArgumentException(
"SSL private key not found: {$sslKey}"
);
throw new \InvalidArgumentException("SSL private key not found: {$sslKey}");
}
$conf[\CURLOPT_SSLKEY] = $sslKey;
}

if (isset($options['progress'])) {
$progress = $options['progress'];
if (!\is_callable($progress)) {
throw new \InvalidArgumentException(
'progress client option must be callable'
);
throw new \InvalidArgumentException('progress client option must be callable');
}
$conf[\CURLOPT_NOPROGRESS] = false;
$conf[\CURLOPT_PROGRESSFUNCTION] = static function ($resource, int $downloadSize, int $downloaded, int $uploadSize, int $uploaded) use ($progress) {
Expand All @@ -530,11 +504,8 @@ private function applyHandlerOptions(EasyHandle $easy, array &$conf): void
*
* @param callable(RequestInterface, array): PromiseInterface $handler
*/
private static function retryFailedRewind(
callable $handler,
EasyHandle $easy,
array $ctx
): PromiseInterface {
private static function retryFailedRewind(callable $handler, EasyHandle $easy, array $ctx): PromiseInterface
{
try {
// Only rewind if the body has been read from.
$body = $easy->request->getBody();
Expand Down
10 changes: 2 additions & 8 deletions src/Handler/CurlMultiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ public function tick(): void
// Step through the task queue which may add additional requests.
P\Utils::queue()->run();

if ($this->active &&
\curl_multi_select($this->_mh, $this->selectTimeout) === -1
) {
if ($this->active && \curl_multi_select($this->_mh, $this->selectTimeout) === -1) {
// Perform a usleep if a select returns -1.
// See: https://bugs.php.net/bug.php?id=61141
\usleep(250);
Expand Down Expand Up @@ -235,11 +233,7 @@ private function processMessages(): void
unset($this->handles[$id], $this->delays[$id]);
$entry['easy']->errno = $done['result'];
$entry['deferred']->resolve(
CurlFactory::finish(
$this,
$entry['easy'],
$this->factory
)
CurlFactory::finish($this, $entry['easy'], $this->factory)
);
}
}
Expand Down
14 changes: 4 additions & 10 deletions src/Handler/EasyHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,11 @@ public function createResponse(): void
$headers = Utils::headersFromLines($this->headers);
$normalizedKeys = Utils::normalizeHeaderKeys($headers);

if (!empty($this->options['decode_content'])
&& isset($normalizedKeys['content-encoding'])
) {
$headers['x-encoded-content-encoding']
= $headers[$normalizedKeys['content-encoding']];
if (!empty($this->options['decode_content']) && isset($normalizedKeys['content-encoding'])) {
$headers['x-encoded-content-encoding'] = $headers[$normalizedKeys['content-encoding']];
unset($headers[$normalizedKeys['content-encoding']]);
if (isset($normalizedKeys['content-length'])) {
$headers['x-encoded-content-length']
= $headers[$normalizedKeys['content-length']];
$headers['x-encoded-content-length'] = $headers[$normalizedKeys['content-length']];

$bodyLength = (int) $this->sink->getSize();
if ($bodyLength) {
Expand Down Expand Up @@ -116,9 +112,7 @@ public function createResponse(): void
*/
public function __get($name)
{
$msg = $name === 'handle'
? 'The EasyHandle has been released'
: 'Invalid property: ' . $name;
$msg = $name === 'handle' ? 'The EasyHandle has been released' : 'Invalid property: ' . $name;
throw new \BadMethodCallException($msg);
}
}
14 changes: 4 additions & 10 deletions src/Handler/MockHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ class MockHandler implements \Countable
* @param callable|null $onFulfilled Callback to invoke when the return value is fulfilled.
* @param callable|null $onRejected Callback to invoke when the return value is rejected.
*/
public static function createWithMiddleware(
array $queue = null,
callable $onFulfilled = null,
callable $onRejected = null
): HandlerStack {
public static function createWithMiddleware(array $queue = null, callable $onFulfilled = null, callable $onRejected = null): HandlerStack
{
return HandlerStack::create(new self($queue, $onFulfilled, $onRejected));
}

Expand All @@ -69,11 +66,8 @@ public static function createWithMiddleware(
* @param callable|null $onFulfilled Callback to invoke when the return value is fulfilled.
* @param callable|null $onRejected Callback to invoke when the return value is rejected.
*/
public function __construct(
array $queue = null,
callable $onFulfilled = null,
callable $onRejected = null
) {
public function __construct(array $queue = null, callable $onFulfilled = null, callable $onRejected = null)
{
$this->onFulfilled = $onFulfilled;
$this->onRejected = $onRejected;

Expand Down
20 changes: 6 additions & 14 deletions src/Handler/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@ class Proxy
*
* @return callable(\Psr\Http\Message\RequestInterface, array): \GuzzleHttp\Promise\PromiseInterface Returns the composed handler.
*/
public static function wrapSync(
callable $default,
callable $sync
): callable {
public static function wrapSync(callable $default, callable $sync): callable
{
return static function (RequestInterface $request, array $options) use ($default, $sync): PromiseInterface {
return empty($options[RequestOptions::SYNCHRONOUS])
? $default($request, $options)
: $sync($request, $options);
return empty($options[RequestOptions::SYNCHRONOUS]) ? $default($request, $options) : $sync($request, $options);
};
}

Expand All @@ -46,14 +42,10 @@ public static function wrapSync(
*
* @return callable(\Psr\Http\Message\RequestInterface, array): \GuzzleHttp\Promise\PromiseInterface Returns the composed handler.
*/
public static function wrapStreaming(
callable $default,
callable $streaming
): callable {
public static function wrapStreaming(callable $default, callable $streaming): callable
{
return static function (RequestInterface $request, array $options) use ($default, $streaming): PromiseInterface {
return empty($options['stream'])
? $default($request, $options)
: $streaming($request, $options);
return empty($options['stream']) ? $default($request, $options) : $streaming($request, $options);
};
}
}
Loading

0 comments on commit c8c99e3

Please sign in to comment.