-
-
Notifications
You must be signed in to change notification settings - Fork 31
Description
Summary of problem or feature request
The getPermissions() method in GateRegistrar.php returns an array instead of an Illuminate\Support\Collection as expected, causing a TypeError in Laravel 11.
Code snippet of problem
The error occurs in the vendor/yajra/laravel-acl/src/GateRegistrar.php file:
protected function getPermissions()
{
try {
$key = config('acl.cache.key', 'acl.permissions');
if (config('acl.cache.enabled', true)) {
// @phpstan-ignore-next-line
return $this->cache->rememberForever($key, function () {
return $this->getPermissionClass()->with('roles')->get();
});
} else {
return $this->getPermissionClass()->with('roles')->get();
}
} catch (Exception $exception) {
return [];
}
}This method returns an array in the catch block, which causes a TypeError when a Collection is expected.
The error in my CLI:
TypeError
Yajra\Acl\GateRegistrar::getPermissions(): Return value must be of type Illuminate\Support\Collection, array returned
at vendor/yajra/laravel-acl/src/GateRegistrar.php:61
57▕ if (config('acl.cache.enabled', true)) {
58▕ // @phpstan-ignore-next-line
59▕ return $this->cache->rememberForever($key, function () {
60▕ return $this->getPermissionClass()->with('roles')->get();
➜ 61▕ });
62▕ } else {
63▕ return $this->getPermissionClass()->with('roles')->get();
64▕ }
65▕ } catch (Exception $exception) {
�[2m+9 vendor frames �[22m
10 [internal]:0
Illuminate\Foundation\Application::Illuminate\Foundation\{closure}(Object(Yajra\Acl\AclServiceProvider))
�[2m+5 vendor frames �[22m
16 artisan:35
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1
System details
- Operating System: Debian GNU/Linux 12 (bookworm)
- PHP Version: 8.2
- Laravel Version: 11.0
- Laravel-ACL Version: 11.1
Possible Solution
To resolve this issue, I downgraded to the more stable version of Laravel 10 and modified the getPermissions method to ensure it always returns a collection of Permission instances. The function has been simplified to avoid using else and uses a ternary operator for cleaner and more concise code.
If you're following the package's code standards, I can open a PR to implement the solution. Waiting for feedback.
protected function getPermissions(): Collection
{
/** @var string $key */
$key = config('acl.cache.key', 'permissions.policies');
try {
$permissions = config('acl.cache.enabled', true)
? $this->cache->rememberForever($key, function () {
return $this->getPermissionClass()->with('roles')->get()->toArray();
})
: $this->getPermissionClass()->with('roles')->get()->toArray();
return collect($permissions)->map(function ($permission) {
return new \Yajra\Acl\Models\Permission($permission);
});
} catch (Exception $exception) {
$this->cache->forget($key);
return new Collection;
}
}