Role   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 74
Duplicated Lines 56.76 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 42
loc 74
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A cachedPermissions() 7 7 1
D hasPermission() 27 27 9
A users() 8 8 2
A permissions() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * This file is part of the Laravel Platfourm package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\Platfourm\User\Models\Eloquent;
12
13
use Cache;
14
use Config;
15
use Illuminate\Database\Eloquent\SoftDeletes;
16
use Longman\Platfourm\Database\Eloquent\Model;
17
use Longman\Platfourm\User\Models\Eloquent\EntrustRoleTrait;
18
use Zizaco\Entrust\EntrustRole;
19
20
class Role extends Model
21
{
22
    use SoftDeletes;
23
24
    protected $fillable = ['name', 'display_name', 'description'];
25
26
    protected $searchableFields = ['name', 'display_name', 'description'];
27
28
    protected $sortableFields = ['name', 'display_name', 'description', 'created_at', 'updated_at'];
29
30
    protected $dates = ['deleted_at'];
31
32 View Code Duplication
    public function cachedPermissions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34
        $cacheKey = 'entrust_permissions_for_role_' . $this->getKey();
35
        return Cache::remember($cacheKey, Config::get('cache.ttl'), function () {
36
            return $this->permissions()->get();
37
        });
38
    }
39
40
    /**
41
     * Checks if the role has a permission by its name.
42
     *
43
     * @param  string|array $name       Permission name or array of permission names.
44
     * @param  bool         $requireAll All permissions in the array are required.
45
     * @return bool
46
     */
47 View Code Duplication
    public function hasPermission($name, $requireAll = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49
        if (is_array($name)) {
50
            foreach ($name as $permissionName) {
51
                $hasPermission = $this->hasPermission($permissionName);
52
53
                if ($hasPermission && !$requireAll) {
54
                    return true;
55
                } elseif (!$hasPermission && $requireAll) {
56
                    return false;
57
                }
58
            }
59
60
            // If we've made it this far and $requireAll is FALSE, then NONE of the permissions were found
61
            // If we've made it this far and $requireAll is TRUE, then ALL of the permissions were found.
62
            // Return the value of $requireAll;
63
            return $requireAll;
64
        } else {
65
            foreach ($this->cachedPermissions() as $permission) {
66
                if ($permission->name == $name) {
67
                    return true;
68
                }
69
            }
70
        }
71
72
        return false;
73
    }
74
75 View Code Duplication
    public function users()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        $model = User::class;
78
        if (class_exists(\App\Models\User::class)) {
79
            $model = \App\Models\User::class;
80
        }
81
        return $this->hasMany($model);
82
    }
83
84
    public function permissions()
85
    {
86
        $model = Permission::class;
87
        if (class_exists(\App\Models\Permission::class)) {
88
            $model = \App\Models\Permission::class;
89
        }
90
        return $this->belongsToMany($model);
91
    }
92
93
}
94