From 2875fd771bdcf351c7649318e2fdb15788b40ef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Wed, 5 Jun 2019 17:02:18 +0200 Subject: [PATCH 1/3] Enhancement: Extract method which ensures callable --- src/Client.php | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Client.php b/src/Client.php index e3a5b31..0a84b46 100644 --- a/src/Client.php +++ b/src/Client.php @@ -122,8 +122,23 @@ public function doSendRequest(RequestInterface $request) */ public function on(RequestMatcher $requestMatcher, $result) { - $callable = null; + $callable = self::makeCallable($result); + $this->conditionalResults[] = [ + 'matcher' => $requestMatcher, + 'callable' => $callable, + ]; + } + + /** + * @param ResponseInterface|Exception|ClientExceptionInterface|callable $result + * + * @throws \InvalidArgumentException + * + * @return callable + */ + private static function makeCallable($result) + { switch (true) { case is_callable($result): $callable = $result; @@ -144,10 +159,8 @@ public function on(RequestMatcher $requestMatcher, $result) default: throw new \InvalidArgumentException('Result must be either a response, an exception, or a callable'); } - $this->conditionalResults[] = [ - 'matcher' => $requestMatcher, - 'callable' => $callable, - ]; + + return $callable; } /** From 987c5a0078dde983441f33017c6cf8b33ab78175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Wed, 5 Jun 2019 17:04:12 +0200 Subject: [PATCH 2/3] Enhancement: Return early --- src/Client.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/Client.php b/src/Client.php index 0a84b46..9cdfed4 100644 --- a/src/Client.php +++ b/src/Client.php @@ -141,26 +141,20 @@ private static function makeCallable($result) { switch (true) { case is_callable($result): - $callable = $result; - - break; + return $result; case $result instanceof ResponseInterface: - $callable = function () use ($result) { + return function () use ($result) { return $result; }; break; case $result instanceof \Exception: - $callable = function () use ($result) { + return function () use ($result) { throw $result; }; - - break; default: throw new \InvalidArgumentException('Result must be either a response, an exception, or a callable'); } - - return $callable; } /** From 1ad4c098993f582a57b2d48d62d7220a287a6f0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Wed, 5 Jun 2019 17:47:00 +0200 Subject: [PATCH 3/3] Enhancement: Use if instead of switch --- src/Client.php | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Client.php b/src/Client.php index 9cdfed4..a9ae12f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -139,22 +139,23 @@ public function on(RequestMatcher $requestMatcher, $result) */ private static function makeCallable($result) { - switch (true) { - case is_callable($result): + if (is_callable($result)) { + return $result; + } + + if ($result instanceof ResponseInterface) { + return function () use ($result) { return $result; - case $result instanceof ResponseInterface: - return function () use ($result) { - return $result; - }; - - break; - case $result instanceof \Exception: - return function () use ($result) { - throw $result; - }; - default: - throw new \InvalidArgumentException('Result must be either a response, an exception, or a callable'); + }; } + + if ($result instanceof \Exception) { + return function () use ($result) { + throw $result; + }; + } + + throw new \InvalidArgumentException('Result must be either a response, an exception, or a callable'); } /**