44
55use Closure ;
66use Codestage \Authorization \Attributes \{AllowAnonymous , Authorize };
7- use Illuminate \Contracts \Container \Container ;
87use Codestage \Authorization \Contracts \{IPermissionEnum ,
98 IPolicy ,
109 Services \IAuthorizationCheckService ,
1110 Services \IAuthorizationService };
12- use Codestage \Authorization \Requirements \HasPermissionRequirement ;
13- use Codestage \Authorization \Requirements \HasRoleRequirement ;
11+ use Codestage \Authorization \Requirements \{HasPermissionRequirement , HasRoleRequirement };
1412use Codestage \Authorization \Traits \HasPermissions ;
1513use Illuminate \Auth \AuthenticationException ;
1614use Illuminate \Contracts \Auth \Guard as AuthManager ;
15+ use Illuminate \Contracts \Container \Container ;
16+ use Illuminate \Contracts \Support \Arrayable ;
1717use Illuminate \Database \Eloquent \Model ;
18- use Illuminate \Support \Collection ;
18+ use Illuminate \Support \{ Collection , Enumerable } ;
1919use ReflectionAttribute ;
2020use ReflectionClass ;
2121use ReflectionException ;
2222use ReflectionFunction ;
2323use function get_class ;
2424use function in_array ;
25- use function is_array ;
2625use function is_string ;
2726
2827/**
@@ -75,9 +74,9 @@ private function isAuthorizationAttribute(object|string $class): bool
7574 * @param class-string $className
7675 * @param string $methodName
7776 * @throws ReflectionException
78- * @return Collection <ReflectionAttribute>
77+ * @return Enumerable <ReflectionAttribute>
7978 */
80- private function extractAttributesFromClassMethod (string $ className , string $ methodName ): Collection
79+ private function extractAttributesFromClassMethod (string $ className , string $ methodName ): Enumerable
8180 {
8281 $ reflectionClass = new ReflectionClass ($ className );
8382 $ method = $ reflectionClass ->getMethod ($ methodName );
@@ -89,9 +88,9 @@ private function extractAttributesFromClassMethod(string $className, string $met
8988 /**
9089 * @param class-string $className
9190 * @throws ReflectionException
92- * @return Collection
91+ * @return Enumerable<ReflectionAttribute>
9392 */
94- private function extractAttributesFromClass (string $ className ): Collection
93+ private function extractAttributesFromClass (string $ className ): Enumerable
9594 {
9695 $ reflectionClass = new ReflectionClass ($ className );
9796
@@ -105,9 +104,9 @@ private function extractAttributesFromClass(string $className): Collection
105104 * @param class-string $className
106105 * @param string $methodName
107106 * @throws ReflectionException
108- * @return Collection <ReflectionAttribute>
107+ * @return Enumerable <ReflectionAttribute>
109108 */
110- private function computeAttributesForClassMethod (string $ className , string $ methodName ): Collection
109+ private function computeAttributesForClassMethod (string $ className , string $ methodName ): Enumerable
111110 {
112111 $ classAttributes = $ this ->extractAttributesFromClass ($ className );
113112 $ methodAttributes = $ this ->extractAttributesFromClassMethod ($ className , $ methodName );
@@ -120,9 +119,9 @@ private function computeAttributesForClassMethod(string $className, string $meth
120119 *
121120 * @param Closure $closure
122121 * @throws ReflectionException
123- * @return Collection
122+ * @return Enumerable<ReflectionAttribute>
124123 */
125- private function computeAttributesForClosure (Closure $ closure ): Collection
124+ private function computeAttributesForClosure (Closure $ closure ): Enumerable
126125 {
127126 $ reflectionFunction = new ReflectionFunction ($ closure );
128127
@@ -133,14 +132,14 @@ private function computeAttributesForClosure(Closure $closure): Collection
133132 /**
134133 * Check whether an action can be accessed when guarded by the given attributes.
135134 *
136- * @param Collection<ReflectionAttribute>|ReflectionAttribute[] $attributes
137- * @return bool
135+ * @param Enumerable<ReflectionAttribute>|Arrayable<ReflectionAttribute>|iterable<ReflectionAttribute> $attributes
138136 * @throws AuthenticationException
137+ * @return bool
139138 */
140- private function canAccessThroughAttributes (Collection | array $ attributes ): bool
139+ private function canAccessThroughAttributes (Enumerable | iterable | Arrayable $ attributes ): bool
141140 {
142- // Make sure the attributes are a Collection
143- if (is_array ($ attributes )) {
141+ // Make sure the attributes are enumerable
142+ if (! ($ attributes instanceof Enumerable )) {
144143 $ attributes = new Collection ($ attributes );
145144 }
146145
@@ -187,8 +186,9 @@ private function checkAttributePasses(Authorize $attribute): bool
187186 // Add role policies
188187 if (!!$ attribute ->roles ) {
189188 $ roles = new Collection ($ attribute ->roles );
189+
190190 foreach ($ roles as $ role ) {
191- $ policies ->push (new class ($ role ) implements IPolicy {
191+ $ policies ->push (new class ($ role ) implements IPolicy {
192192 /** Constructor method. */
193193 public function __construct (public readonly string $ role )
194194 {
@@ -206,8 +206,9 @@ public function requirements(): array
206206 // Add permission policies
207207 if (!!$ attribute ->permissions ) {
208208 $ permissions = new Collection ($ attribute ->permissions );
209+
209210 foreach ($ permissions as $ permission ) {
210- $ policies ->push (new class ($ permission ) implements IPolicy {
211+ $ policies ->push (new class ($ permission ) implements IPolicy {
211212 /** Constructor method. */
212213 public function __construct (public readonly IPermissionEnum $ permission )
213214 {
@@ -229,19 +230,17 @@ public function requirements(): array
229230
230231 // Instantiate all policies and run them
231232 return $ policies ->map (fn (string |IPolicy $ policy ) => is_string ($ policy ) ? $ this ->_container ->make ($ policy ) : $ policy )
232- ->some (function (IPolicy $ policy ) {
233- return $ this ->_authorizationService ->authorizePolicy (null , $ policy );
234- });
233+ ->some (fn (IPolicy $ policy ) => $ this ->_authorizationService ->authorizePolicy (null , $ policy ));
235234 }
236235
237236 /**
238237 * Check whether the given controller method can be accessed in the current request context.
239238 *
240239 * @param class-string $className
241240 * @param class-string $methodName
242- * @return bool
243241 * @throws AuthenticationException
244242 * @throws ReflectionException
243+ * @return bool
245244 */
246245 public function canAccessControllerMethod (string $ className , string $ methodName ): bool
247246 {
@@ -254,9 +253,9 @@ public function canAccessControllerMethod(string $className, string $methodName)
254253 * Check whether the given controller method can be accessed in the current request context.
255254 *
256255 * @param Closure $closure
257- * @return bool
258256 * @throws AuthenticationException
259257 * @throws ReflectionException
258+ * @return bool
260259 */
261260 public function canAccessClosure (Closure $ closure ): bool
262261 {
0 commit comments