Skip to content

Commit

Permalink
Leverage PHP8's get_debug_type()
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Mar 16, 2020
1 parent 05431fd commit b19681a
Show file tree
Hide file tree
Showing 16 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion AmpHttpClient.php
Expand Up @@ -138,7 +138,7 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa
if ($responses instanceof AmpResponse) {
$responses = [$responses];
} elseif (!is_iterable($responses)) {
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of AmpResponse objects, %s given.', __METHOD__, \is_object($responses) ? \get_class($responses) : \gettype($responses)));
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of AmpResponse objects, %s given.', __METHOD__, get_debug_type($responses)));
}

return new ResponseStream(AmpResponse::stream($responses, $timeout));
Expand Down
2 changes: 1 addition & 1 deletion CachingHttpClient.php
Expand Up @@ -114,7 +114,7 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa
if ($responses instanceof ResponseInterface) {
$responses = [$responses];
} elseif (!is_iterable($responses)) {
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of ResponseInterface objects, "%s" given.', __METHOD__, \is_object($responses) ? \get_class($responses) : \gettype($responses)));
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of ResponseInterface objects, "%s" given.', __METHOD__, get_debug_type($responses)));
}

$mockResponses = [];
Expand Down
6 changes: 3 additions & 3 deletions CurlHttpClient.php
Expand Up @@ -163,7 +163,7 @@ public function request(string $method, string $url, array $options = []): Respo
}

if (!\is_string($options['auth_ntlm'])) {
throw new InvalidArgumentException(sprintf('Option "auth_ntlm" must be a string or an array, "%s" given.', \gettype($options['auth_ntlm'])));
throw new InvalidArgumentException(sprintf('Option "auth_ntlm" must be a string or an array, "%s" given.', get_debug_type($options['auth_ntlm'])));
}

$curlopts[CURLOPT_USERPWD] = $options['auth_ntlm'];
Expand Down Expand Up @@ -319,7 +319,7 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa
if ($responses instanceof CurlResponse) {
$responses = [$responses];
} elseif (!is_iterable($responses)) {
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of CurlResponse objects, "%s" given.', __METHOD__, \is_object($responses) ? \get_class($responses) : \gettype($responses)));
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of CurlResponse objects, "%s" given.', __METHOD__, get_debug_type($responses)));
}

$active = 0;
Expand Down Expand Up @@ -439,7 +439,7 @@ private static function readRequestBody(int $length, \Closure $body, string &$bu
{
if (!$eof && \strlen($buffer) < $length) {
if (!\is_string($data = $body($length))) {
throw new TransportException(sprintf('The return value of the "body" option callback must be a string, "%s" returned.', \gettype($data)));
throw new TransportException(sprintf('The return value of the "body" option callback must be a string, "%s" returned.', get_debug_type($data)));
}

$buffer .= $data;
Expand Down
18 changes: 9 additions & 9 deletions HttpClientTrait.php
Expand Up @@ -49,7 +49,7 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
$options['buffer'] = static function (array $headers) use ($buffer) {
if (!\is_bool($buffer = $buffer($headers))) {
if (!\is_array($bufferInfo = @stream_get_meta_data($buffer))) {
throw new \LogicException(sprintf('The closure passed as option "buffer" must return bool or stream resource, got "%s".', \is_resource($buffer) ? get_resource_type($buffer).' resource' : \gettype($buffer)));
throw new \LogicException(sprintf('The closure passed as option "buffer" must return bool or stream resource, got "%s".', get_debug_type($buffer)));
}

if (false === strpbrk($bufferInfo['mode'], 'acew+')) {
Expand All @@ -61,7 +61,7 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
};
} elseif (!\is_bool($buffer)) {
if (!\is_array($bufferInfo = @stream_get_meta_data($buffer))) {
throw new InvalidArgumentException(sprintf('Option "buffer" must be bool, stream resource or Closure, "%s" given.', \is_resource($buffer) ? get_resource_type($buffer).' resource' : \gettype($buffer)));
throw new InvalidArgumentException(sprintf('Option "buffer" must be bool, stream resource or Closure, "%s" given.', get_debug_type($buffer)));
}

if (false === strpbrk($bufferInfo['mode'], 'acew+')) {
Expand Down Expand Up @@ -95,7 +95,7 @@ private static function prepareRequest(?string $method, ?string $url, array $opt

// Validate on_progress
if (!\is_callable($onProgress = $options['on_progress'] ?? 'var_dump')) {
throw new InvalidArgumentException(sprintf('Option "on_progress" must be callable, "%s" given.', \is_object($onProgress) ? \get_class($onProgress) : \gettype($onProgress)));
throw new InvalidArgumentException(sprintf('Option "on_progress" must be callable, "%s" given.', get_debug_type($onProgress)));
}

if (\is_array($options['auth_basic'] ?? null)) {
Expand All @@ -108,11 +108,11 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
}

if (!\is_string($options['auth_basic'] ?? '')) {
throw new InvalidArgumentException(sprintf('Option "auth_basic" must be string or an array, "%s" given.', \gettype($options['auth_basic'])));
throw new InvalidArgumentException(sprintf('Option "auth_basic" must be string or an array, "%s" given.', get_debug_type($options['auth_basic'])));
}

if (isset($options['auth_bearer']) && (!\is_string($options['auth_bearer']) || !preg_match('{^[-._=~+/0-9a-zA-Z]++$}', $options['auth_bearer']))) {
throw new InvalidArgumentException(sprintf('Option "auth_bearer" must be a string containing only characters from the base 64 alphabet, %s given.', \is_string($options['auth_bearer']) ? 'invalid string' : '"'.\gettype($options['auth_bearer']).'"'));
throw new InvalidArgumentException(sprintf('Option "auth_bearer" must be a string containing only characters from the base 64 alphabet, %s given.', \is_string($options['auth_bearer']) ? 'invalid string' : '"'.get_debug_type($options['auth_bearer']).'"'));
}

if (isset($options['auth_basic'], $options['auth_bearer'])) {
Expand Down Expand Up @@ -232,13 +232,13 @@ private static function normalizeHeaders(array $headers): array

if (\is_int($name)) {
if (!\is_string($values)) {
throw new InvalidArgumentException(sprintf('Invalid value for header "%s": expected string, "%s" given.', $name, \gettype($values)));
throw new InvalidArgumentException(sprintf('Invalid value for header "%s": expected string, "%s" given.', $name, get_debug_type($values)));
}
[$name, $values] = explode(':', $values, 2);
$values = [ltrim($values)];
} elseif (!is_iterable($values)) {
if (\is_object($values)) {
throw new InvalidArgumentException(sprintf('Invalid value for header "%s": expected string, "%s" given.', $name, \get_class($values)));
throw new InvalidArgumentException(sprintf('Invalid value for header "%s": expected string, "%s" given.', $name, get_debug_type($values)));
}

$values = (array) $values;
Expand Down Expand Up @@ -313,7 +313,7 @@ private static function normalizeBody($body)
}

if (!\is_array(@stream_get_meta_data($body))) {
throw new InvalidArgumentException(sprintf('Option "body" must be string, stream resource, iterable or callable, "%s" given.', \is_resource($body) ? get_resource_type($body) : \gettype($body)));
throw new InvalidArgumentException(sprintf('Option "body" must be string, stream resource, iterable or callable, "%s" given.', get_debug_type($body)));
}

return $body;
Expand All @@ -339,7 +339,7 @@ private static function normalizePeerFingerprint($fingerprint): array
$fingerprint[$algo] = 'pin-sha256' === $algo ? (array) $hash : str_replace(':', '', $hash);
}
} else {
throw new InvalidArgumentException(sprintf('Option "peer_fingerprint" must be string or array, "%s" given.', \gettype($fingerprint)));
throw new InvalidArgumentException(sprintf('Option "peer_fingerprint" must be string or array, "%s" given.', get_debug_type($fingerprint)));
}

return $fingerprint;
Expand Down
2 changes: 1 addition & 1 deletion HttplugClient.php
Expand Up @@ -184,7 +184,7 @@ public function createStream($body = null): StreamInterface
} elseif (\is_resource($body)) {
$stream = $this->streamFactory->createStreamFromResource($body);
} else {
throw new \InvalidArgumentException(sprintf('"%s()" expects string, resource or StreamInterface, "%s" given.', __METHOD__, \gettype($body)));
throw new \InvalidArgumentException(sprintf('"%s()" expects string, resource or StreamInterface, "%s" given.', __METHOD__, get_debug_type($body)));
}

if ($stream->isSeekable()) {
Expand Down
2 changes: 1 addition & 1 deletion Internal/AmpBody.php
Expand Up @@ -133,7 +133,7 @@ private function doRead(): Promise
}

if (!\is_string($data)) {
throw new TransportException(sprintf('Return value of the "body" option callback must be string, "%s" returned.', \gettype($data)));
throw new TransportException(sprintf('Return value of the "body" option callback must be string, "%s" returned.', get_debug_type($data)));
}

return new Success($data);
Expand Down
2 changes: 1 addition & 1 deletion MockHttpClient.php
Expand Up @@ -82,7 +82,7 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa
if ($responses instanceof ResponseInterface) {
$responses = [$responses];
} elseif (!is_iterable($responses)) {
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of MockResponse objects, "%s" given.', __METHOD__, \is_object($responses) ? \get_class($responses) : \gettype($responses)));
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of MockResponse objects, "%s" given.', __METHOD__, get_debug_type($responses)));
}

return new ResponseStream(MockResponse::stream($responses, $timeout));
Expand Down
4 changes: 2 additions & 2 deletions NativeHttpClient.php
Expand Up @@ -235,7 +235,7 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa
if ($responses instanceof NativeResponse) {
$responses = [$responses];
} elseif (!is_iterable($responses)) {
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of NativeResponse objects, "%s" given.', __METHOD__, \is_object($responses) ? \get_class($responses) : \gettype($responses)));
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of NativeResponse objects, "%s" given.', __METHOD__, get_debug_type($responses)));
}

return new ResponseStream(NativeResponse::stream($responses, $timeout));
Expand All @@ -255,7 +255,7 @@ private static function getBodyAsString($body): string

while ('' !== $data = $body(self::$CHUNK_SIZE)) {
if (!\is_string($data)) {
throw new TransportException(sprintf('Return value of the "body" option callback must be string, "%s" returned.', \gettype($data)));
throw new TransportException(sprintf('Return value of the "body" option callback must be string, "%s" returned.', get_debug_type($data)));
}

$result .= $data;
Expand Down
4 changes: 2 additions & 2 deletions NoPrivateNetworkHttpClient.php
Expand Up @@ -54,7 +54,7 @@ final class NoPrivateNetworkHttpClient implements HttpClientInterface, LoggerAwa
public function __construct(HttpClientInterface $client, $subnets = null)
{
if (!(\is_array($subnets) || \is_string($subnets) || null === $subnets)) {
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be of the type array, string or null. "%s" given.', __METHOD__, \is_object($subnets) ? \get_class($subnets) : \gettype($subnets)));
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be of the type array, string or null. "%s" given.', __METHOD__, get_debug_type($subnets)));
}

if (!class_exists(IpUtils::class)) {
Expand All @@ -72,7 +72,7 @@ public function request(string $method, string $url, array $options = []): Respo
{
$onProgress = $options['on_progress'] ?? null;
if (null !== $onProgress && !\is_callable($onProgress)) {
throw new InvalidArgumentException(sprintf('Option "on_progress" must be callable, "%s" given.', \is_object($onProgress) ? \get_class($onProgress) : \gettype($onProgress)));
throw new InvalidArgumentException(sprintf('Option "on_progress" must be callable, "%s" given.', get_debug_type($onProgress)));
}

$subnets = $this->subnets;
Expand Down
2 changes: 1 addition & 1 deletion Response/MockResponse.php
Expand Up @@ -228,7 +228,7 @@ private static function writeRequest(self $response, array $options, ResponseInt
} elseif ($body instanceof \Closure) {
while ('' !== $data = $body(16372)) {
if (!\is_string($data)) {
throw new TransportException(sprintf('Return value of the "body" option callback must be string, "%s" returned.', \gettype($data)));
throw new TransportException(sprintf('Return value of the "body" option callback must be string, "%s" returned.', get_debug_type($data)));
}

// "notify" upload progress
Expand Down
2 changes: 1 addition & 1 deletion Response/ResponseTrait.php
Expand Up @@ -161,7 +161,7 @@ public function toArray(bool $throw = true): array
}

if (!\is_array($content)) {
throw new JsonException(sprintf('JSON content was expected to decode to an array, "%s" returned for "%s".', \gettype($content), $this->getInfo('url')));
throw new JsonException(sprintf('JSON content was expected to decode to an array, "%s" returned for "%s".', get_debug_type($content), $this->getInfo('url')));
}

if (null !== $this->content) {
Expand Down
4 changes: 2 additions & 2 deletions Tests/HttpClientTraitTest.php
Expand Up @@ -179,7 +179,7 @@ public function testAuthBearerOption()
public function testInvalidAuthBearerOption()
{
$this->expectException('Symfony\Component\HttpClient\Exception\InvalidArgumentException');
$this->expectExceptionMessage('Option "auth_bearer" must be a string containing only characters from the base 64 alphabet, "object" given.');
$this->expectExceptionMessage('Option "auth_bearer" must be a string containing only characters from the base 64 alphabet, "stdClass" given.');
self::prepareRequest('POST', 'http://example.com', ['auth_bearer' => new \stdClass()], HttpClientInterface::OPTIONS_DEFAULTS);
}

Expand Down Expand Up @@ -249,7 +249,7 @@ public function testNormalizePeerFingerprintException()
public function testNormalizePeerFingerprintTypeException()
{
$this->expectException('Symfony\Component\HttpClient\Exception\InvalidArgumentException');
$this->expectExceptionMessage('Option "peer_fingerprint" must be string or array, "object" given.');
$this->expectExceptionMessage('Option "peer_fingerprint" must be string or array, "stdClass" given.');
$fingerprint = new \stdClass();

$this->normalizePeerFingerprint($fingerprint);
Expand Down
2 changes: 1 addition & 1 deletion Tests/NoPrivateNetworkHttpClientTest.php
Expand Up @@ -122,7 +122,7 @@ public function testNonCallableOnProgressCallback()
public function testConstructor()
{
$this->expectException(\TypeError::class);
$this->expectExceptionMessage('Argument 2 passed to "Symfony\Component\HttpClient\NoPrivateNetworkHttpClient::__construct()" must be of the type array, string or null. "integer" given.');
$this->expectExceptionMessage('Argument 2 passed to "Symfony\Component\HttpClient\NoPrivateNetworkHttpClient::__construct()" must be of the type array, string or null. "int" given.');

new NoPrivateNetworkHttpClient(new MockHttpClient(), 3);
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Response/MockResponseTest.php
Expand Up @@ -62,7 +62,7 @@ public function toArrayErrors()
yield [
'content' => '8',
'responseHeaders' => [],
'message' => 'JSON content was expected to decode to an array, "integer" returned for "https://example.com/file.json".',
'message' => 'JSON content was expected to decode to an array, "int" returned for "https://example.com/file.json".',
];
}
}
4 changes: 2 additions & 2 deletions TraceableHttpClient.php
Expand Up @@ -67,13 +67,13 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa
if ($responses instanceof TraceableResponse) {
$responses = [$responses];
} elseif (!is_iterable($responses)) {
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, \is_object($responses) ? \get_class($responses) : \gettype($responses)));
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($responses)));
}

return $this->client->stream(\Closure::bind(static function () use ($responses) {
foreach ($responses as $k => $r) {
if (!$r instanceof TraceableResponse) {
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, \is_object($r) ? \get_class($r) : \gettype($r)));
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', __METHOD__, get_debug_type($r)));
}

yield $k => $r->response;
Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -25,6 +25,7 @@
"psr/log": "^1.0",
"symfony/http-client-contracts": "^1.1.8|^2",
"symfony/polyfill-php73": "^1.11",
"symfony/polyfill-php80": "^1.15",
"symfony/service-contracts": "^1.0|^2"
},
"require-dev": {
Expand Down

0 comments on commit b19681a

Please sign in to comment.