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 |
||
16 | class Permission extends Model implements PermissionContract |
||
17 | { |
||
18 | use HasRoles; |
||
19 | use RefreshesPermissionCache; |
||
20 | |||
21 | protected $guarded = ['id']; |
||
22 | |||
23 | public function __construct(array $attributes = []) |
||
24 | { |
||
25 | $attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard'); |
||
26 | |||
27 | parent::__construct($attributes); |
||
28 | } |
||
29 | |||
30 | public function getTable() |
||
31 | { |
||
32 | return config('permission.table_names.permissions', parent::getTable()); |
||
33 | } |
||
34 | |||
35 | public static function create(array $attributes = []) |
||
36 | { |
||
37 | $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class); |
||
38 | |||
39 | $permission = static::getPermissions(['name' => $attributes['name'], 'guard_name' => $attributes['guard_name']])->first(); |
||
40 | |||
41 | if ($permission) { |
||
42 | throw PermissionAlreadyExists::create($attributes['name'], $attributes['guard_name']); |
||
43 | } |
||
44 | |||
45 | return static::query()->create($attributes); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * A permission can be applied to roles. |
||
50 | */ |
||
51 | public function roles(): BelongsToMany |
||
52 | { |
||
53 | return $this->belongsToMany( |
||
54 | config('permission.models.role'), |
||
55 | config('permission.table_names.role_has_permissions'), |
||
56 | 'permission_id', |
||
57 | 'role_id' |
||
58 | ); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * A permission belongs to some users of the model associated with its guard. |
||
63 | */ |
||
64 | View Code Duplication | public function users(): BelongsToMany |
|
74 | |||
75 | /** |
||
76 | * Find a permission by its name (and optionally guardName). |
||
77 | * |
||
78 | * @param string $name |
||
79 | * @param string|null $guardName |
||
80 | * |
||
81 | * @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist |
||
82 | * |
||
83 | * @return \Spatie\Permission\Contracts\Permission |
||
84 | */ |
||
85 | View Code Duplication | public static function findByName(string $name, $guardName = null): PermissionContract |
|
95 | |||
96 | /** |
||
97 | * Find a permission by its id (and optionally guardName). |
||
98 | * |
||
99 | * @param int $id |
||
100 | * @param string|null $guardName |
||
101 | * |
||
102 | * @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist |
||
103 | * |
||
104 | * @return \Spatie\Permission\Contracts\Permission |
||
105 | */ |
||
106 | View Code Duplication | public static function findById(int $id, $guardName = null): PermissionContract |
|
117 | |||
118 | /** |
||
119 | * Find or create permission by its name (and optionally guardName). |
||
120 | * |
||
121 | * @param string $name |
||
122 | * @param string|null $guardName |
||
123 | * |
||
124 | * @return \Spatie\Permission\Contracts\Permission |
||
125 | */ |
||
126 | View Code Duplication | public static function findOrCreate(string $name, $guardName = null): PermissionContract |
|
137 | |||
138 | /** |
||
139 | * Get the current cached permissions. |
||
140 | */ |
||
141 | protected static function getPermissions(array $params = []): Collection |
||
147 | } |
||
148 |
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.