Skip to content

Commit

Permalink
Focus on reducing complexity
Browse files Browse the repository at this point in the history
move TYPE_x constants to new Type class
method params - new values : isPassedByReference & isVariadic..    name no longer contains $, &, or ...
  • Loading branch information
bkdotcom committed Jan 15, 2024
1 parent 299975d commit 82cdb65
Show file tree
Hide file tree
Showing 151 changed files with 3,506 additions and 2,735 deletions.
50 changes: 31 additions & 19 deletions src/CurlHttpMessage/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function __destruct()
*
* @return PromiseInterface|ResponseInterface
*/
public function delete($uri, $headers = array(), $body = null)
public function delete($uri, array $headers = array(), $body = null)
{
return $this->request('DELETE', $uri, array(
'body' => $body,
Expand All @@ -96,7 +96,7 @@ public function delete($uri, $headers = array(), $body = null)
* Send a GET request
*
* @param UriInterface|string $uri UriInterface or string
* @param array $headers Request headers
* @param array|null $headers Request headers
*
* @return PromiseInterface|ResponseInterface
*/
Expand All @@ -115,7 +115,7 @@ public function get($uri, $headers = array())
*
* @return PromiseInterface|ResponseInterface
*/
public function head($uri, $headers = array())
public function head($uri, array $headers = array())
{
return $this->request('HEAD', $uri, array(
'headers' => $headers,
Expand All @@ -131,7 +131,7 @@ public function head($uri, $headers = array())
*
* @return PromiseInterface|ResponseInterface
*/
public function options($uri, $headers = array(), $body = null)
public function options($uri, array $headers = array(), $body = null)
{
return $this->request('OPTIONS', $uri, array(
'body' => $body,
Expand All @@ -148,7 +148,7 @@ public function options($uri, $headers = array(), $body = null)
*
* @return PromiseInterface|ResponseInterface
*/
public function patch($uri, $headers = array(), $body = null)
public function patch($uri, array $headers = array(), $body = null)
{
return $this->request('PATCH', $uri, array(
'body' => $body,
Expand All @@ -165,7 +165,7 @@ public function patch($uri, $headers = array(), $body = null)
*
* @return PromiseInterface|ResponseInterface
*/
public function post($uri, $headers = array(), $body = null)
public function post($uri, array $headers = array(), $body = null)
{
return $this->request('POST', $uri, array(
'body' => $body,
Expand All @@ -182,7 +182,7 @@ public function post($uri, $headers = array(), $body = null)
*
* @return PromiseInterface|ResponseInterface
*/
public function put($uri, $headers = array(), $body = null)
public function put($uri, array $headers = array(), $body = null)
{
return $this->request('PUT', $uri, array(
'body' => $body,
Expand All @@ -198,7 +198,7 @@ public function put($uri, $headers = array(), $body = null)
*
* @return PromiseInterface|ResponseInterface
*/
public function trace($uri, $headers = array())
public function trace($uri, array $headers = array())
{
return $this->request(
'TRACE',
Expand Down Expand Up @@ -232,7 +232,7 @@ public function getStack()
* @throws BadResponseException HTTP error (4xx or 5xx response)
* @throws RuntimeException Failure to create stream
*/
public function handle(RequestInterface $request, $options = array())
public function handle(RequestInterface $request, array $options = array())
{
$options = $this->mergeOptions($options);
$request = $this->applyOptions($request, $options);
Expand All @@ -256,12 +256,31 @@ public function handle(RequestInterface $request, $options = array())
*
* @return PromiseInterface|ResponseInterface
*/
public function request($method, $uri, $options = array())
public function request($method, $uri, array $options = array())
{
$request = $this->factory->request($method, $uri);
return $this->handle($request, $options);
}

/**
* Assert valid options
*
* @param array $options Options
*
* @return void
*
* @throws InvalidArgumentException
*/
private function assertOptions(array $options)
{
if (\is_array($options['headers']) !== true) {
throw new InvalidArgumentException('headers must be an array');
}
if (\array_keys($options['headers']) === \range(0, \count($options['headers']) - 1)) {
throw new InvalidArgumentException('headers array must have header name as keys');
}
}

/**
* Merge specified options with default client options
*
Expand Down Expand Up @@ -300,17 +319,10 @@ private function mergeOptions($options)
* @param array $options options
*
* @return RequestInterface
*
* @throws InvalidArgumentException
*/
private function applyOptions(RequestInterface $request, $options)
private function applyOptions(RequestInterface $request, array $options)
{
if (\is_array($options['headers']) !== true) {
throw new InvalidArgumentException('headers must be an array');
}
if (\array_keys($options['headers']) === \range(0, \count($options['headers']) - 1)) {
throw new InvalidArgumentException('headers array must have header name as keys');
}
$this->assertOptions($options);

foreach ($options['headers'] as $name => $val) {
$request = $request->withHeader($name, $val);
Expand Down
7 changes: 2 additions & 5 deletions src/CurlHttpMessage/ClientAsync.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,8 @@ public function each($requests, array $config = array())
yield $key => $request($opts);
continue;
}
throw new InvalidArgumentException(
'Each value yielded by the iterator must be a'
. ' Psr7\Http\Message\RequestInterface or a callable that returns a promise'
. ' that fulfills with a Psr7\Message\Http\ResponseInterface object.'
);
throw new InvalidArgumentException('Each request must be a Psr7\Http\Message\RequestInterface'
. ' or a callable that returns a promise that fulfills with a Psr7\Message\Http\ResponseInterface object.');
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/CurlHttpMessage/CurlReqResOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ private function setOptionsHttpHeader()
private function requestHeadersToHeaders(array $requestHeaders)
{
$headers = [];
foreach ($requestHeaders as $name => $values) {
\array_walk($requestHeaders, function ($values, $name) use (&$headers) {
$nameLower = \strtolower($name);

// cURL does not support 'Expect-Continue', skip all 'EXPECT' headers
if ($nameLower === 'expect') {
continue;
return;
}

if ($nameLower === 'content-length') {
Expand All @@ -207,7 +207,7 @@ private function requestHeadersToHeaders(array $requestHeaders)
foreach ($values as $value) {
$headers[] = $name . ': ' . $value;
}
}
});
return $headers;
}

Expand Down
69 changes: 46 additions & 23 deletions src/CurlHttpMessage/Handler/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,7 @@ public function __invoke(CurlReqRes $curlReqRes)

$this->lastCurlReqRes = $curlReqRes;

$response = \array_shift($this->queue);

if (\is_callable($response)) {
$response = $response($curlReqRes->getRequest(), $curlReqRes->getOptions());
}

$response = $response instanceof Exception
? Promise::rejectionFor($response)
: Promise::promiseFor($response);

return $response->then(
return $this->getResponsePromise()->then(
function (ResponseInterface $value) use ($curlReqRes) {
$curlReqRes->setResponse($value);

Expand Down Expand Up @@ -109,25 +99,14 @@ function ($reason) {
* @param mixed ...$values Value(s) to add to queue
*
* @return void
*
* @throws InvalidArgumentException
*/
public function append($values)
{
$values = \func_num_args() === 1 && \is_array($values) && \is_callable($values) === false
? $values
: \func_get_args();
foreach ($values as $value) {
$isValid = $value instanceof ResponseInterface
|| $value instanceof PromiseInterface
|| $value instanceof Exception
|| \is_callable($value);
if ($isValid === false) {
throw new InvalidArgumentException(\sprintf(
'Expected a Response, Promise, Throwable or callable. %s provided',
\is_object($value) ? \get_class($value) : \gettype($value)
));
}
$this->assertValue($value);
$this->queue[] = $value;
}
}
Expand Down Expand Up @@ -174,4 +153,48 @@ public function reset()
{
$this->queue = array();
}

/**
* Get promise wrapped in promise
*
* @return PromiseInterface
*/
public function getResponsePromise()
{
$response = \array_shift($this->queue);

if (\is_callable($response)) {
$response = $response(
$this->lastCurlReqRes->getRequest(),
$this->lastCurlReqRes->getOptions()
);
}

return $response instanceof Exception
? Promise::rejectionFor($response)
: Promise::promiseFor($response);
}

/**
* Assert valid mock "response" value
*
* @param callable|Exception|PromiseInterface|ResponseInterface $value value to assert
*
* @return void
*
* @throws InvalidArgumentException
*/
private function assertValue($value)
{
$isValid = $value instanceof ResponseInterface
|| $value instanceof PromiseInterface
|| $value instanceof Exception
|| \is_callable($value);
if ($isValid === false) {
throw new InvalidArgumentException(\sprintf(
'Expected a Response, Promise, Throwable or callable. %s provided',
\is_object($value) ? \get_class($value) : \gettype($value)
));
}
}
}
Loading

0 comments on commit 82cdb65

Please sign in to comment.