Skip to content

Commit d8b8e2c

Browse files
committed
Performed code cleanup.
* Ran the PHP CS Fixer. * Reduced dependencies on facades, replaced with proper DI. * Replaced Collection return types with Enumerables.
1 parent 8f1c0bd commit d8b8e2c

38 files changed

+505
-311
lines changed

src/Console/Commands/InstallCommand.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,15 @@ public function handle(): int
6666
if (!is_dir($this->laravel->basePath('app/Authorization'))) {
6767
mkdir($this->laravel->basePath('app/Authorization'));
6868
}
69+
6970
if (!is_dir($this->laravel->basePath('app/Authorization/Handlers'))) {
7071
mkdir($this->laravel->basePath('app/Authorization/Handlers'));
7172
}
73+
7274
if (!is_dir($this->laravel->basePath('app/Authorization/Policies'))) {
7375
mkdir($this->laravel->basePath('app/Authorization/Policies'));
7476
}
77+
7578
if (!is_dir($this->laravel->basePath('app/Authorization/Requirements'))) {
7679
mkdir($this->laravel->basePath('app/Authorization/Requirements'));
7780
}
@@ -83,6 +86,7 @@ public function handle(): int
8386

8487
// Check whether the user has default laravel policies and warn them if they do
8588
$defaultPoliciesPath = $this->laravel->basePath('app/Policies');
89+
8690
if (is_dir($defaultPoliciesPath)) {
8791
if ($this->checkDirectoryEmpty($defaultPoliciesPath)) {
8892
unlink($defaultPoliciesPath);
@@ -135,7 +139,7 @@ private function createPermissionEnum(): bool
135139
]);
136140

137141
// Extract path before last slash (supposedly the Enums directory)
138-
$directory = $this->laravel->basePath(substr(self::PermissionEnumPath, 0, strrpos(self::PermissionEnumPath, '/')));
142+
$directory = $this->laravel->basePath(mb_substr(self::PermissionEnumPath, 0, mb_strrpos(self::PermissionEnumPath, '/')));
139143

140144
// If the directory does not exist, create it
141145
if (!is_dir($directory)) {
@@ -165,8 +169,8 @@ private function completeStub(string $stubName, array $replacements = []): strin
165169

166170
// Replace interpolations from the replacements array
167171
foreach ($replacements as $key => $value) {
168-
$stub = (string)str_ireplace('{{ ' . $key . ' }}', $value, $stub);
169-
$stub = (string)str_ireplace('{{' . $key . '}}', $value, $stub);
172+
$stub = (string) str_ireplace('{{ ' . $key . ' }}', $value, $stub);
173+
$stub = (string) str_ireplace('{{' . $key . '}}', $value, $stub);
170174
}
171175

172176
// Return the resulting string
@@ -206,19 +210,20 @@ public function addMiddlewareToKernel(): bool
206210

207211
$groups = $reflection->getProperty('middlewareGroups')->getDefaultValue();
208212

209-
if (!is_array($groups)) {
213+
if (!\is_array($groups)) {
210214
return false;
211215
}
212216

213217
// Build the middleware class' FQN
214218
$middlewareQualifiedName = AuthorizationMiddleware::class;
219+
215220
if (!str_starts_with($middlewareQualifiedName, '\\')) {
216221
$middlewareQualifiedName = '\\' . $middlewareQualifiedName;
217222
}
218223

219224
// Add the middleware to each group
220225
foreach ($groups as $group => $groupMiddleware) {
221-
if (!in_array(AuthorizationMiddleware::class, $groupMiddleware)) {
226+
if (!\in_array(AuthorizationMiddleware::class, $groupMiddleware)) {
222227
$newFileContents = preg_replace(
223228
'/([\'"]' . $group . '[\'"] => \\[[a-zA-Z\\s:,\\/\\\\\'\"]+?)([ \\t]+)((?:(?:\\w|\\\\)+)?SubstituteBindings::class)/m',
224229
'$1$2' . $middlewareQualifiedName . "::class,\n$2$3",
@@ -240,13 +245,16 @@ public function addMiddlewareToKernel(): bool
240245
private function checkDirectoryEmpty(string $directory): bool
241246
{
242247
$handle = opendir($directory);
248+
243249
while (($entry = readdir($handle)) !== false) {
244-
if ($entry !== "." && $entry !== "..") {
250+
if ($entry !== '.' && $entry !== '..') {
245251
closedir($handle);
252+
246253
return false;
247254
}
248255
}
249256
closedir($handle);
257+
250258
return true;
251259
}
252260
}

src/Console/Commands/MakeRequirementCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class MakeRequirementCommand extends GeneratorCommand
1919
/**
2020
* Execute the console command.
2121
*
22+
* @throws FileNotFoundException
2223
* @return bool|null
2324
*
24-
* @throws FileNotFoundException
2525
*/
2626
public function handle(): ?bool
2727
{
@@ -56,9 +56,9 @@ protected function getStub(): string
5656
* Build the class with the given name.
5757
*
5858
* @param string $name
59+
* @throws FileNotFoundException
5960
* @return string
6061
*
61-
* @throws FileNotFoundException
6262
*/
6363
protected function buildClass($name): string
6464
{

src/Console/Commands/MakeRequirementHandlerCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ protected function getStub(): string
3737
* Build the class with the given name.
3838
*
3939
* @param string $name
40+
* @throws FileNotFoundException
4041
* @return string
4142
*
42-
* @throws FileNotFoundException
4343
*/
4444
protected function buildClass($name): string
4545
{

src/Contracts/IAuthorizable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
interface IAuthorizable
66
{
7-
}
7+
}

src/Contracts/Services/IAuthorizationService.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace Codestage\Authorization\Contracts\Services;
44

5-
use Codestage\Authorization\Contracts\IPolicy;
6-
use Codestage\Authorization\Contracts\IRequirement;
5+
use Codestage\Authorization\Contracts\{IPolicy, IRequirement};
76
use Illuminate\Contracts\Container\BindingResolutionException;
87

98
/**
@@ -16,8 +15,8 @@ interface IAuthorizationService
1615
*
1716
* @param TResource|null $resource
1817
* @param IPolicy|class-string $policy
19-
* @return bool
2018
* @throws BindingResolutionException
19+
* @return bool
2120
*/
2221
public function authorizePolicy(mixed $resource, IPolicy|string $policy): bool;
2322

@@ -29,4 +28,4 @@ public function authorizePolicy(mixed $resource, IPolicy|string $policy): bool;
2928
* @return bool
3029
*/
3130
public function authorizeRequirements(mixed $resource, iterable $requirements): bool;
32-
}
31+
}

src/Models/Role.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
namespace Codestage\Authorization\Models;
44

55
use Carbon\Carbon;
6+
use Codestage\Authorization\Contracts\IPermissionEnum;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Database\Eloquent\Relations\HasMany;
8-
use Illuminate\Support\{Collection, Str};
9+
use Illuminate\Support\{Enumerable, Str};
910

1011
/**
11-
* @template TPermission of \Codestage\Authorization\Contracts\IPermissionEnum
12+
* @template TPermission of IPermissionEnum
1213
*
1314
* @property string $id
1415
* @property string $key
1516
* @property string $name
1617
* @property Carbon $created_at
1718
* @property Carbon $updated_at
18-
* @property Collection<TPermission> $permissions
19+
* @property Enumerable<TPermission> $permissions
1920
*/
2021
class Role extends Model
2122
{

src/Models/RolePermission.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Illuminate\Support\Facades\Config;
1111

1212
/**
13-
* @template TPermission of \Codestage\Authorization\Contracts\IPermissionEnum
13+
* @template TPermission of IPermissionEnum
1414
*
1515
* @property string $role_id
1616
* @property TPermission $permission

src/Providers/AuthorizationServiceProvider.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
namespace Codestage\Authorization\Providers;
44

5-
use Codestage\Authorization\Console\Commands\InstallCommand;
6-
use Codestage\Authorization\Console\Commands\MakePolicyCommand;
7-
use Codestage\Authorization\Console\Commands\MakeRequirementCommand;
8-
use Codestage\Authorization\Console\Commands\MakeRequirementHandlerCommand;
5+
use Codestage\Authorization\Console\Commands\{InstallCommand, MakePolicyCommand, MakeRequirementCommand, MakeRequirementHandlerCommand};
96
use Codestage\Authorization\Contracts\Services\{IAuthorizationCheckService, IAuthorizationService};
107
use Codestage\Authorization\Services\{AuthorizationCheckService, AuthorizationService};
118
use Illuminate\Foundation\Console\PolicyMakeCommand as BaseMakePolicyCommand;

src/Services/AuthorizationCheckService.php

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,24 @@
44

55
use Closure;
66
use Codestage\Authorization\Attributes\{AllowAnonymous, Authorize};
7-
use Illuminate\Contracts\Container\Container;
87
use 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};
1412
use Codestage\Authorization\Traits\HasPermissions;
1513
use Illuminate\Auth\AuthenticationException;
1614
use Illuminate\Contracts\Auth\Guard as AuthManager;
15+
use Illuminate\Contracts\Container\Container;
16+
use Illuminate\Contracts\Support\Arrayable;
1717
use Illuminate\Database\Eloquent\Model;
18-
use Illuminate\Support\Collection;
18+
use Illuminate\Support\{Collection, Enumerable};
1919
use ReflectionAttribute;
2020
use ReflectionClass;
2121
use ReflectionException;
2222
use ReflectionFunction;
2323
use function get_class;
2424
use function in_array;
25-
use function is_array;
2625
use 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

Comments
 (0)