From 91e7ab6db27b3aabbd621bbc044c18d6e508b3cf Mon Sep 17 00:00:00 2001 From: Yan Brasiliano Silva Penalva Date: Fri, 2 Aug 2024 09:35:32 -0300 Subject: [PATCH 1/6] fix: Ensure getPermissions returns Collection and simplify logic --- src/GateRegistrar.php | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/GateRegistrar.php b/src/GateRegistrar.php index 394b92b..bcbbf39 100644 --- a/src/GateRegistrar.php +++ b/src/GateRegistrar.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Cache\Repository; use Illuminate\Foundation\Auth\User; use Illuminate\Support\Str; +use Illuminate\Support\Collection; use Yajra\Acl\Models\Permission; class GateRegistrar @@ -16,16 +17,11 @@ public function __construct(public GateContract $gate, public Repository $cache) public function register(): void { - collect($this->getPermissions())->each(function ($data) { - $permission = new Permission($data); - + $this->getPermissions()->each(function (Permission $permission) { $ability = $permission->slug; $policy = function (User $user) use ($permission) { if (method_exists($user, 'getPermissions')) { - // @phpstan-ignore-next-line - $permissions = collect($user->getPermissions()); - - return $permissions->contains($permission->slug); + return collect($user->getPermissions())->contains($permission->slug); } return false; @@ -43,32 +39,31 @@ public function register(): void /** * Get all permissions. * - * @return array> + * @return \Illuminate\Support\Collection */ - protected function getPermissions(): array + protected function getPermissions(): Collection { /** @var string $key */ $key = config('acl.cache.key', 'permissions.policies'); try { - if (config('acl.cache.enabled', true)) { - return $this->cache->rememberForever($key, fn () => $this->getPermissionsFromQuery()); - } else { - return $this->getPermissionsFromQuery(); - } + $permissions = config('acl.cache.enabled', true) + ? $this->cache->rememberForever($key, fn () => $this->getPermissionsFromQuery()) + : $this->getPermissionsFromQuery(); + + return collect($permissions)->map(fn ($data) => new Permission($data)); } catch (\Throwable) { $this->cache->forget($key); - return []; + return collect([]); } } /** - * @return array> + * @return array */ public function getPermissionsFromQuery(): array { - // @phpstan-ignore-next-line return $this->getPermissionClass() ->with('roles') ->get() From 0df6083c15d56c8567182b4517597a6f7460aba7 Mon Sep 17 00:00:00 2001 From: Yan Brasiliano Silva Penalva Date: Fri, 2 Aug 2024 09:49:45 -0300 Subject: [PATCH 2/6] chore: add phpstan ignore again --- src/GateRegistrar.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/GateRegistrar.php b/src/GateRegistrar.php index bcbbf39..c17b663 100644 --- a/src/GateRegistrar.php +++ b/src/GateRegistrar.php @@ -21,6 +21,7 @@ public function register(): void $ability = $permission->slug; $policy = function (User $user) use ($permission) { if (method_exists($user, 'getPermissions')) { + // @phpstan-ignore-next-line return collect($user->getPermissions())->contains($permission->slug); } From e547fff6eb8d9954025c39a915ae67a45e6831b1 Mon Sep 17 00:00:00 2001 From: Yan Brasiliano Silva Penalva Date: Fri, 2 Aug 2024 10:12:21 -0300 Subject: [PATCH 3/6] chore: add phpstan ignore again --- src/GateRegistrar.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/GateRegistrar.php b/src/GateRegistrar.php index c17b663..32703b5 100644 --- a/src/GateRegistrar.php +++ b/src/GateRegistrar.php @@ -65,6 +65,7 @@ protected function getPermissions(): Collection */ public function getPermissionsFromQuery(): array { + // @phpstan-ignore-next-line return $this->getPermissionClass() ->with('roles') ->get() From c56248f784f85705d8f43fe1bdf8cf87f1f8617c Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Mon, 5 Aug 2024 10:14:34 +0800 Subject: [PATCH 4/6] deps: upgrade --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 392114c..1a514de 100644 --- a/composer.json +++ b/composer.json @@ -24,10 +24,10 @@ "illuminate/database": "^11.0" }, "require-dev": { - "larastan/larastan": "^2.9.1", - "laravel/pint": "^1.14", - "rector/rector": "^1.0", - "orchestra/testbench": "^9.0" + "larastan/larastan": "^2.9.8", + "laravel/pint": "^1.17.1", + "rector/rector": "^1.2.2", + "orchestra/testbench": "^9.2" }, "autoload": { "psr-4": { From f21cca1b08c5085de7539d924c7f94d6c66bbc63 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Mon, 5 Aug 2024 10:14:40 +0800 Subject: [PATCH 5/6] fix: pint --- src/Directives/DirectiveAbstract.php | 4 +--- src/GateRegistrar.php | 11 ++--------- tests/Feature/CanAtLeastMiddlewareTest.php | 2 +- tests/Feature/HasRoleTest.php | 4 ++-- tests/Feature/PermissionMiddlewareTest.php | 2 +- tests/TestCase.php | 2 +- 6 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/Directives/DirectiveAbstract.php b/src/Directives/DirectiveAbstract.php index 1070356..726d881 100644 --- a/src/Directives/DirectiveAbstract.php +++ b/src/Directives/DirectiveAbstract.php @@ -9,7 +9,5 @@ abstract class DirectiveAbstract /** * IsRoleDirective constructor. */ - public function __construct(protected Guard $auth) - { - } + public function __construct(protected Guard $auth) {} } diff --git a/src/GateRegistrar.php b/src/GateRegistrar.php index 32703b5..20a7cb5 100644 --- a/src/GateRegistrar.php +++ b/src/GateRegistrar.php @@ -5,15 +5,13 @@ use Illuminate\Contracts\Auth\Access\Gate as GateContract; use Illuminate\Contracts\Cache\Repository; use Illuminate\Foundation\Auth\User; -use Illuminate\Support\Str; use Illuminate\Support\Collection; +use Illuminate\Support\Str; use Yajra\Acl\Models\Permission; class GateRegistrar { - public function __construct(public GateContract $gate, public Repository $cache) - { - } + public function __construct(public GateContract $gate, public Repository $cache) {} public function register(): void { @@ -39,8 +37,6 @@ public function register(): void /** * Get all permissions. - * - * @return \Illuminate\Support\Collection */ protected function getPermissions(): Collection { @@ -60,9 +56,6 @@ protected function getPermissions(): Collection } } - /** - * @return array - */ public function getPermissionsFromQuery(): array { // @phpstan-ignore-next-line diff --git a/tests/Feature/CanAtLeastMiddlewareTest.php b/tests/Feature/CanAtLeastMiddlewareTest.php index dffca69..c5d3953 100644 --- a/tests/Feature/CanAtLeastMiddlewareTest.php +++ b/tests/Feature/CanAtLeastMiddlewareTest.php @@ -20,7 +20,7 @@ public function it_can_access_permission_protected_routes() $middleware = new CanAtLeastMiddleware; - $response = $middleware->handle(new Request(), fn () => 'Pass', 'create-article,non-existing-permission'); + $response = $middleware->handle(new Request, fn () => 'Pass', 'create-article,non-existing-permission'); $this->assertEquals('Pass', $response); } diff --git a/tests/Feature/HasRoleTest.php b/tests/Feature/HasRoleTest.php index d95198a..911fbfa 100644 --- a/tests/Feature/HasRoleTest.php +++ b/tests/Feature/HasRoleTest.php @@ -181,10 +181,10 @@ public function it_can_query_with_having_roles_scopes() $supportUser->attachRole($support); - $roles = (new User())->havingRoles([$manager->getKey(), $support->getKey()])->get(); + $roles = (new User)->havingRoles([$manager->getKey(), $support->getKey()])->get(); $this->assertCount(2, $roles); - $roles = (new User())->havingRolesBySlugs([$manager->slug, $user->slug])->get(); + $roles = (new User)->havingRolesBySlugs([$manager->slug, $user->slug])->get(); $this->assertCount(1, $roles); } } diff --git a/tests/Feature/PermissionMiddlewareTest.php b/tests/Feature/PermissionMiddlewareTest.php index 7cfc556..f98c0ae 100644 --- a/tests/Feature/PermissionMiddlewareTest.php +++ b/tests/Feature/PermissionMiddlewareTest.php @@ -20,7 +20,7 @@ public function it_can_access_permission_protected_routes() $middleware = new PermissionMiddleware; - $response = $middleware->handle(new Request(), fn () => 'Pass', 'create-article'); + $response = $middleware->handle(new Request, fn () => 'Pass', 'create-article'); $this->assertEquals('Pass', $response); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 607b8a4..bb536bd 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -135,7 +135,7 @@ protected function getEnvironmentSetUp($app): void $app['config']->set('view.paths', [__DIR__.'/resources/views']); $app['config']->set('auth.providers.users.model', User::class); - $app['log']->getLogger()->pushHandler(new TestHandler()); + $app['log']->getLogger()->pushHandler(new TestHandler); } protected function getPackageProviders($app): array From 26090c3f8712a1c84a1768b3e7b0face6530265c Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Tue, 6 Aug 2024 15:36:09 +0800 Subject: [PATCH 6/6] fix: phpstan --- phpstan.neon.dist | 2 -- src/GateRegistrar.php | 17 +++++++++-------- src/Models/Permission.php | 1 + src/Models/Role.php | 1 + src/Traits/InteractsWithPermission.php | 3 ++- src/Traits/InteractsWithRole.php | 3 ++- 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 31521e9..a560fa9 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -11,5 +11,3 @@ parameters: ignoreErrors: excludePaths: - - checkMissingIterableValueType: true diff --git a/src/GateRegistrar.php b/src/GateRegistrar.php index 20a7cb5..c98d1ff 100644 --- a/src/GateRegistrar.php +++ b/src/GateRegistrar.php @@ -37,6 +37,8 @@ public function register(): void /** * Get all permissions. + * + * @return \Illuminate\Support\Collection */ protected function getPermissions(): Collection { @@ -44,25 +46,24 @@ protected function getPermissions(): Collection $key = config('acl.cache.key', 'permissions.policies'); try { - $permissions = config('acl.cache.enabled', true) + return config('acl.cache.enabled', true) ? $this->cache->rememberForever($key, fn () => $this->getPermissionsFromQuery()) : $this->getPermissionsFromQuery(); - - return collect($permissions)->map(fn ($data) => new Permission($data)); } catch (\Throwable) { $this->cache->forget($key); - return collect([]); + return collect(); } } - public function getPermissionsFromQuery(): array + /** + * @return \Illuminate\Support\Collection + */ + public function getPermissionsFromQuery(): Collection { - // @phpstan-ignore-next-line return $this->getPermissionClass() ->with('roles') - ->get() - ->toArray(); + ->get(); } protected function getPermissionClass(): Permission diff --git a/src/Models/Permission.php b/src/Models/Permission.php index 5c28641..94f379f 100644 --- a/src/Models/Permission.php +++ b/src/Models/Permission.php @@ -105,6 +105,7 @@ public function users(): BelongsToMany /** @var class-string<\Illuminate\Foundation\Auth\User> $model */ $model = config('acl.user', config('auth.providers.users.model')); + // @phpstan-ignore-next-line return $this->belongsToMany($model)->withTimestamps(); } } diff --git a/src/Models/Role.php b/src/Models/Role.php index 9d291d4..8bb4c0e 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -48,6 +48,7 @@ public function users(): BelongsToMany /** @var class-string<\Illuminate\Foundation\Auth\User> $model */ $model = config('acl.user', config('auth.providers.users.model')); + // @phpstan-ignore-next-line return $this->belongsToMany($model)->withTimestamps(); } } diff --git a/src/Traits/InteractsWithPermission.php b/src/Traits/InteractsWithPermission.php index 91448d7..c3856ba 100644 --- a/src/Traits/InteractsWithPermission.php +++ b/src/Traits/InteractsWithPermission.php @@ -69,9 +69,10 @@ public function grantPermission(mixed $ids, array $attributes = [], bool $touch */ public function permissions(): BelongsToMany { - /** @var class-string $model */ + /** @var class-string $model */ $model = config('acl.permission', Permission::class); + // @phpstan-ignore-next-line return $this->belongsToMany($model)->withTimestamps(); } diff --git a/src/Traits/InteractsWithRole.php b/src/Traits/InteractsWithRole.php index 232bd79..83e14ef 100644 --- a/src/Traits/InteractsWithRole.php +++ b/src/Traits/InteractsWithRole.php @@ -79,9 +79,10 @@ public function attachRole(mixed $role, array $attributes = [], bool $touch = tr */ public function roles(): BelongsToMany { - /** @var class-string $model */ + /** @var class-string $model */ $model = config('acl.role', Role::class); + // @phpstan-ignore-next-line return $this->belongsToMany($model)->withTimestamps(); }