From a0a643dc9e9308642baf5206891d95ef4b250861 Mon Sep 17 00:00:00 2001 From: Mostafa Maklad Date: Sat, 17 Mar 2018 23:44:15 +0300 Subject: [PATCH] fix code smells for codeclimate --- src/Guard.php | 12 +++------ src/Models/Permission.php | 8 +++--- src/Models/Role.php | 10 +++++--- src/Traits/HasPermissions.php | 4 +-- src/Traits/HasRoles.php | 46 +++++++++++++++++------------------ 5 files changed, 38 insertions(+), 42 deletions(-) diff --git a/src/Guard.php b/src/Guard.php index c388e72..6c15c89 100644 --- a/src/Guard.php +++ b/src/Guard.php @@ -1,10 +1,4 @@ guard_name ?? null; @@ -57,9 +51,9 @@ public static function getNames($model) : Collection * @return string * @throws \ReflectionException */ - public static function getDefaultName($class): string + public function getDefaultName($class): string { $default = config('auth.defaults.guard'); - return static::getNames($class)->first() ?: $default; + return $this->getNames($class)->first() ?: $default; } } diff --git a/src/Models/Permission.php b/src/Models/Permission.php index 2d1dfa2..b360710 100644 --- a/src/Models/Permission.php +++ b/src/Models/Permission.php @@ -34,7 +34,7 @@ class Permission extends Model implements PermissionInterface */ public function __construct(array $attributes = []) { - $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class); + $attributes['guard_name'] = $attributes['guard_name'] ?? (new Guard())->getDefaultName(static::class); parent::__construct($attributes); @@ -55,7 +55,7 @@ public function __construct(array $attributes = []) public static function create(array $attributes = []) { $helpers = new Helpers(); - $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class); + $attributes['guard_name'] = $attributes['guard_name'] ?? (new Guard())->getDefaultName(static::class); if (static::getPermissions()->where('name', $attributes['name'])->where( 'guard_name', @@ -85,7 +85,7 @@ public static function create(array $attributes = []) */ public static function findOrCreate(string $name, $guardName = null): PermissionInterface { - $guardName = $guardName ?? Guard::getDefaultName(static::class); + $guardName = $guardName ?? (new Guard())->getDefaultName(static::class); $permission = static::getPermissions() ->where('name', $name) @@ -132,7 +132,7 @@ public function users(): BelongsToMany */ public static function findByName(string $name, $guardName = null): PermissionInterface { - $guardName = $guardName ?? Guard::getDefaultName(static::class); + $guardName = $guardName ?? (new Guard())->getDefaultName(static::class); $permission = static::getPermissions()->where('name', $name)->where('guard_name', $guardName)->first(); diff --git a/src/Models/Role.php b/src/Models/Role.php index 1724d7a..bcb86ef 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -12,6 +12,7 @@ use Maklad\Permission\Helpers; use Maklad\Permission\Traits\HasPermissions; use Maklad\Permission\Traits\RefreshesPermissionCache; +use ReflectionException; /** * Class Role @@ -34,7 +35,7 @@ class Role extends Model implements RoleInterface */ public function __construct(array $attributes = []) { - $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class); + $attributes['guard_name'] = $attributes['guard_name'] ?? (new Guard())->getDefaultName(static::class); parent::__construct($attributes); @@ -54,7 +55,7 @@ public function __construct(array $attributes = []) */ public static function create(array $attributes = []) { - $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class); + $attributes['guard_name'] = $attributes['guard_name'] ?? (new Guard())->getDefaultName(static::class); $helpers = new Helpers(); if (static::where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) { @@ -82,7 +83,7 @@ public static function create(array $attributes = []) */ public static function findOrCreate(string $name, $guardName = null): RoleInterface { - $guardName = $guardName ?? Guard::getDefaultName(static::class); + $guardName = $guardName ?? (new Guard())->getDefaultName(static::class); $role = static::where('name', $name) ->where('guard_name', $guardName) @@ -107,7 +108,7 @@ public static function findOrCreate(string $name, $guardName = null): RoleInterf */ public static function findByName(string $name, $guardName = null): RoleInterface { - $guardName = $guardName ?? Guard::getDefaultName(static::class); + $guardName = $guardName ?? (new Guard())->getDefaultName(static::class); $role = static::where('name', $name) ->where('guard_name', $guardName) @@ -129,6 +130,7 @@ public static function findByName(string $name, $guardName = null): RoleInterfac * @return bool * * @throws GuardDoesNotMatch + * @throws ReflectionException */ public function hasPermissionTo($permission): bool { diff --git a/src/Traits/HasPermissions.php b/src/Traits/HasPermissions.php index 8895e97..502d982 100644 --- a/src/Traits/HasPermissions.php +++ b/src/Traits/HasPermissions.php @@ -142,7 +142,7 @@ protected function ensureModelSharesGuard(Model $roleOrPermission) */ protected function getGuardNames(): Collection { - return Guard::getNames($this); + return (new Guard())->getNames($this); } /** @@ -151,7 +151,7 @@ protected function getGuardNames(): Collection */ protected function getDefaultGuardName(): string { - return Guard::getDefaultName($this); + return (new Guard())->getDefaultName($this); } /** diff --git a/src/Traits/HasRoles.php b/src/Traits/HasRoles.php index 1da905a..e303415 100644 --- a/src/Traits/HasRoles.php +++ b/src/Traits/HasRoles.php @@ -9,6 +9,7 @@ use Jenssegers\Mongodb\Relations\BelongsToMany; use Maklad\Permission\Contracts\PermissionInterface as Permission; use Maklad\Permission\Contracts\RoleInterface as Role; +use ReflectionException; /** * Trait HasRoles @@ -159,26 +160,23 @@ public function hasRole($roles): bool if (\is_string($roles) && false !== \strpos($roles, '|')) { $roles = \explode('|', $roles); } - + $has_role = false; if (\is_string($roles)) { - return $this->roles->contains('name', $roles); - } - - if ($roles instanceof Role) { - return $this->roles->contains('id', $roles->id); - } - - if (\is_array($roles)) { + $has_role = $this->roles->contains('name', $roles); + }elseif ($roles instanceof Role) { + $has_role = $this->roles->contains('id', $roles->id); + }elseif (\is_array($roles)) { foreach ($roles as $role) { if ($this->hasRole($role)) { - return true; + $has_role = true; + break; } } - - return false; + }else { + $has_role = ! $roles->intersect($this->roles)->isEmpty(); } - return ! $roles->intersect($this->roles)->isEmpty(); + return $has_role; } /** @@ -207,18 +205,17 @@ public function hasAllRoles($roles): bool } if (\is_string($roles)) { - return $this->roles->contains('name', $roles); - } - - if ($roles instanceof Role) { - return $this->roles->contains('id', $roles->id); + $has_roles = $this->roles->contains('name', $roles); + }elseif ($roles instanceof Role) { + $has_roles = $this->roles->contains('id', $roles->id); + }else { + $roles = \collect()->make($roles)->map(function ($role) { + return $role instanceof Role ? $role->name : $role; + }); + $has_roles = $roles->intersect($this->roles->pluck('name')) == $roles; } - $roles = \collect()->make($roles)->map(function ($role) { - return $role instanceof Role ? $role->name : $role; - }); - - return $roles->intersect($this->roles->pluck('name')) == $roles; + return $has_roles; } /** @@ -228,6 +225,7 @@ public function hasAllRoles($roles): bool * @param string|null $guardName * * @return bool + * @throws ReflectionException */ public function hasPermissionTo($permission, $guardName = null): bool { @@ -281,6 +279,7 @@ protected function hasPermissionViaRole(Permission $permission): bool * @param string|Permission $permission * * @return bool + * @throws ReflectionException */ public function hasDirectPermission($permission): bool { @@ -327,6 +326,7 @@ public function getAllPermissions(): Collection * @param String|Role $role role name * * @return Role + * @throws ReflectionException */ protected function getStoredRole($role): Role {