Skip to content

Commit a7c10c2

Browse files
committed
Use assert().
1 parent b1a263a commit a7c10c2

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

src/AuthorizationService.php

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,33 +85,30 @@ protected function performCheck(?IdentityInterface $user, string $action, mixed
8585
$result = $policy->before($user, $resource, $action);
8686

8787
if ($result !== null) {
88-
return $this->resultTypeCheck($result);
88+
assert(
89+
is_bool($result) || $result instanceof ResultInterface,
90+
new Exception(sprintf(
91+
'Pre-authorization check must return `%s`, `bool` or `null`.',
92+
ResultInterface::class
93+
))
94+
);
95+
96+
return $result;
8997
}
9098
}
9199

92100
$handler = $this->getCanHandler($policy, $action);
93101
$result = $handler($user, $resource);
94102

95-
return $this->resultTypeCheck($result);
96-
}
97-
98-
/**
99-
* Check result type.
100-
*
101-
* @param mixed $result Result from policy class instance.
102-
* @return \Authorization\Policy\ResultInterface|bool
103-
* @throws \Authorization\Exception\Exception If $result argument is not a boolean or ResultInterface instance.
104-
*/
105-
protected function resultTypeCheck(mixed $result): ResultInterface|bool
106-
{
107-
if (is_bool($result) || $result instanceof ResultInterface) {
108-
return $result;
109-
}
103+
assert(
104+
is_bool($result) || $result instanceof ResultInterface,
105+
new Exception(sprintf(
106+
'Authorization check method must return `%s` or `bool`.',
107+
ResultInterface::class
108+
))
109+
);
110110

111-
throw new Exception(sprintf(
112-
'Pre-authorization check must return `%s`, `bool` or `null`.',
113-
ResultInterface::class
114-
));
111+
return $result;
115112
}
116113

117114
/**
@@ -138,9 +135,10 @@ protected function getCanHandler(mixed $policy, string $action): Closure
138135
{
139136
$method = 'can' . ucfirst($action);
140137

141-
if (!method_exists($policy, $method) && !method_exists($policy, '__call')) {
142-
throw new MissingMethodException([$method, $action, get_class($policy)]);
143-
}
138+
assert(
139+
method_exists($policy, $method) || method_exists($policy, '__call'),
140+
new MissingMethodException([$method, $action, get_class($policy)])
141+
);
144142

145143
return [$policy, $method](...);
146144
}
@@ -157,9 +155,10 @@ protected function getScopeHandler(mixed $policy, string $action): Closure
157155
{
158156
$method = 'scope' . ucfirst($action);
159157

160-
if (!method_exists($policy, $method)) {
161-
throw new MissingMethodException([$method, $action, get_class($policy)]);
162-
}
158+
assert(
159+
method_exists($policy, $method) || method_exists($policy, '__call'),
160+
new MissingMethodException([$method, $action, get_class($policy)])
161+
);
163162

164163
return [$policy, $method](...);
165164
}

0 commit comments

Comments
 (0)