Skip to content

Commit

Permalink
fix code smells for codeclimate
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafamaklad committed Mar 17, 2018
1 parent e22ffa3 commit a0a643d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 42 deletions.
12 changes: 3 additions & 9 deletions src/Guard.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: mostafamaklad
* Date: 12/03/2018
* Time: 2:23 PM
*/

namespace Maklad\Permission;

Expand All @@ -25,7 +19,7 @@ class Guard
* @return Collection
* @throws \ReflectionException
*/
public static function getNames($model) : Collection
public function getNames($model) : Collection
{
if (\is_object($model)) {
$guardName = $model->guard_name ?? null;
Expand Down Expand Up @@ -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;
}
}
8 changes: 4 additions & 4 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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',
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();

Expand Down
10 changes: 6 additions & 4 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Maklad\Permission\Helpers;
use Maklad\Permission\Traits\HasPermissions;
use Maklad\Permission\Traits\RefreshesPermissionCache;
use ReflectionException;

/**
* Class Role
Expand All @@ -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);

Expand All @@ -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()) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -129,6 +130,7 @@ public static function findByName(string $name, $guardName = null): RoleInterfac
* @return bool
*
* @throws GuardDoesNotMatch
* @throws ReflectionException
*/
public function hasPermissionTo($permission): bool
{
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected function ensureModelSharesGuard(Model $roleOrPermission)
*/
protected function getGuardNames(): Collection
{
return Guard::getNames($this);
return (new Guard())->getNames($this);
}

/**
Expand All @@ -151,7 +151,7 @@ protected function getGuardNames(): Collection
*/
protected function getDefaultGuardName(): string
{
return Guard::getDefaultName($this);
return (new Guard())->getDefaultName($this);
}

/**
Expand Down
46 changes: 23 additions & 23 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -228,6 +225,7 @@ public function hasAllRoles($roles): bool
* @param string|null $guardName
*
* @return bool
* @throws ReflectionException
*/
public function hasPermissionTo($permission, $guardName = null): bool
{
Expand Down Expand Up @@ -281,6 +279,7 @@ protected function hasPermissionViaRole(Permission $permission): bool
* @param string|Permission $permission
*
* @return bool
* @throws ReflectionException
*/
public function hasDirectPermission($permission): bool
{
Expand Down Expand Up @@ -327,6 +326,7 @@ public function getAllPermissions(): Collection
* @param String|Role $role role name
*
* @return Role
* @throws ReflectionException
*/
protected function getStoredRole($role): Role
{
Expand Down

0 comments on commit a0a643d

Please sign in to comment.