From b1a263a62db90a365f34bbb8123b7b61c4851442 Mon Sep 17 00:00:00 2001 From: ADmad Date: Wed, 28 Dec 2022 13:59:05 +0530 Subject: [PATCH 1/2] Simplify types. --- src/AuthorizationService.php | 13 +++++++------ src/Middleware/AuthorizationMiddleware.php | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/AuthorizationService.php b/src/AuthorizationService.php index ef4c01eb..4b7ceb4a 100644 --- a/src/AuthorizationService.php +++ b/src/AuthorizationService.php @@ -22,6 +22,7 @@ use Authorization\Policy\ResolverInterface; use Authorization\Policy\Result; use Authorization\Policy\ResultInterface; +use Closure; class AuthorizationService implements AuthorizationServiceInterface { @@ -130,10 +131,10 @@ public function applyScope(?IdentityInterface $user, string $action, $resource): * * @param mixed $policy Policy object. * @param string $action Action name. - * @return callable + * @return \Closure * @throws \Authorization\Policy\Exception\MissingMethodException */ - protected function getCanHandler(mixed $policy, string $action): callable + protected function getCanHandler(mixed $policy, string $action): Closure { $method = 'can' . ucfirst($action); @@ -141,7 +142,7 @@ protected function getCanHandler(mixed $policy, string $action): callable throw new MissingMethodException([$method, $action, get_class($policy)]); } - return [$policy, $method]; + return [$policy, $method](...); } /** @@ -149,10 +150,10 @@ protected function getCanHandler(mixed $policy, string $action): callable * * @param mixed $policy Policy object. * @param string $action Action name. - * @return callable + * @return \Closure * @throws \Authorization\Policy\Exception\MissingMethodException */ - protected function getScopeHandler(mixed $policy, string $action): callable + protected function getScopeHandler(mixed $policy, string $action): Closure { $method = 'scope' . ucfirst($action); @@ -160,7 +161,7 @@ protected function getScopeHandler(mixed $policy, string $action): callable throw new MissingMethodException([$method, $action, get_class($policy)]); } - return [$policy, $method]; + return [$policy, $method](...); } /** diff --git a/src/Middleware/AuthorizationMiddleware.php b/src/Middleware/AuthorizationMiddleware.php index d309d01c..337a050b 100644 --- a/src/Middleware/AuthorizationMiddleware.php +++ b/src/Middleware/AuthorizationMiddleware.php @@ -179,7 +179,7 @@ protected function buildIdentity( if (!$identity instanceof IdentityInterface) { throw new RuntimeException(sprintf( 'Invalid identity returned by decorator. `%s` does not implement `%s`.', - is_object($identity) ? get_class($identity) : gettype($identity), + get_debug_type($identity), IdentityInterface::class )); } From 3601a40b3d23e1dafdacbc2753aa31545f6574b2 Mon Sep 17 00:00:00 2001 From: ADmad Date: Wed, 28 Dec 2022 14:09:21 +0530 Subject: [PATCH 2/2] Use assert(). --- src/AuthorizationService.php | 43 ++++++++++++++---------------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/src/AuthorizationService.php b/src/AuthorizationService.php index 4b7ceb4a..b6a11c90 100644 --- a/src/AuthorizationService.php +++ b/src/AuthorizationService.php @@ -85,33 +85,22 @@ protected function performCheck(?IdentityInterface $user, string $action, mixed $result = $policy->before($user, $resource, $action); if ($result !== null) { - return $this->resultTypeCheck($result); + return $result; } } $handler = $this->getCanHandler($policy, $action); $result = $handler($user, $resource); - return $this->resultTypeCheck($result); - } - - /** - * Check result type. - * - * @param mixed $result Result from policy class instance. - * @return \Authorization\Policy\ResultInterface|bool - * @throws \Authorization\Exception\Exception If $result argument is not a boolean or ResultInterface instance. - */ - protected function resultTypeCheck(mixed $result): ResultInterface|bool - { - if (is_bool($result) || $result instanceof ResultInterface) { - return $result; - } + assert( + is_bool($result) || $result instanceof ResultInterface, + new Exception(sprintf( + 'Authorization check method must return `%s` or `bool`.', + ResultInterface::class + )) + ); - throw new Exception(sprintf( - 'Pre-authorization check must return `%s`, `bool` or `null`.', - ResultInterface::class - )); + return $result; } /** @@ -138,9 +127,10 @@ protected function getCanHandler(mixed $policy, string $action): Closure { $method = 'can' . ucfirst($action); - if (!method_exists($policy, $method) && !method_exists($policy, '__call')) { - throw new MissingMethodException([$method, $action, get_class($policy)]); - } + assert( + method_exists($policy, $method) || method_exists($policy, '__call'), + new MissingMethodException([$method, $action, get_class($policy)]) + ); return [$policy, $method](...); } @@ -157,9 +147,10 @@ protected function getScopeHandler(mixed $policy, string $action): Closure { $method = 'scope' . ucfirst($action); - if (!method_exists($policy, $method)) { - throw new MissingMethodException([$method, $action, get_class($policy)]); - } + assert( + method_exists($policy, $method) || method_exists($policy, '__call'), + new MissingMethodException([$method, $action, get_class($policy)]) + ); return [$policy, $method](...); }