Skip to content

Commit 347667d

Browse files
authored
Merge pull request #224 from cakephp/3.x-types
3.x types
2 parents 26ef7f5 + 3601a40 commit 347667d

File tree

2 files changed

+25
-33
lines changed

2 files changed

+25
-33
lines changed

src/AuthorizationService.php

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Authorization\Policy\ResolverInterface;
2323
use Authorization\Policy\Result;
2424
use Authorization\Policy\ResultInterface;
25+
use Closure;
2526

2627
class AuthorizationService implements AuthorizationServiceInterface
2728
{
@@ -84,33 +85,22 @@ protected function performCheck(?IdentityInterface $user, string $action, mixed
8485
$result = $policy->before($user, $resource, $action);
8586

8687
if ($result !== null) {
87-
return $this->resultTypeCheck($result);
88+
return $result;
8889
}
8990
}
9091

9192
$handler = $this->getCanHandler($policy, $action);
9293
$result = $handler($user, $resource);
9394

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

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

116106
/**
@@ -130,37 +120,39 @@ public function applyScope(?IdentityInterface $user, string $action, $resource):
130120
*
131121
* @param mixed $policy Policy object.
132122
* @param string $action Action name.
133-
* @return callable
123+
* @return \Closure
134124
* @throws \Authorization\Policy\Exception\MissingMethodException
135125
*/
136-
protected function getCanHandler(mixed $policy, string $action): callable
126+
protected function getCanHandler(mixed $policy, string $action): Closure
137127
{
138128
$method = 'can' . ucfirst($action);
139129

140-
if (!method_exists($policy, $method) && !method_exists($policy, '__call')) {
141-
throw new MissingMethodException([$method, $action, get_class($policy)]);
142-
}
130+
assert(
131+
method_exists($policy, $method) || method_exists($policy, '__call'),
132+
new MissingMethodException([$method, $action, get_class($policy)])
133+
);
143134

144-
return [$policy, $method];
135+
return [$policy, $method](...);
145136
}
146137

147138
/**
148139
* Returns a policy scope action handler.
149140
*
150141
* @param mixed $policy Policy object.
151142
* @param string $action Action name.
152-
* @return callable
143+
* @return \Closure
153144
* @throws \Authorization\Policy\Exception\MissingMethodException
154145
*/
155-
protected function getScopeHandler(mixed $policy, string $action): callable
146+
protected function getScopeHandler(mixed $policy, string $action): Closure
156147
{
157148
$method = 'scope' . ucfirst($action);
158149

159-
if (!method_exists($policy, $method)) {
160-
throw new MissingMethodException([$method, $action, get_class($policy)]);
161-
}
150+
assert(
151+
method_exists($policy, $method) || method_exists($policy, '__call'),
152+
new MissingMethodException([$method, $action, get_class($policy)])
153+
);
162154

163-
return [$policy, $method];
155+
return [$policy, $method](...);
164156
}
165157

166158
/**

src/Middleware/AuthorizationMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ protected function buildIdentity(
179179
if (!$identity instanceof IdentityInterface) {
180180
throw new RuntimeException(sprintf(
181181
'Invalid identity returned by decorator. `%s` does not implement `%s`.',
182-
is_object($identity) ? get_class($identity) : gettype($identity),
182+
get_debug_type($identity),
183183
IdentityInterface::class
184184
));
185185
}

0 commit comments

Comments
 (0)