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  | 
            ||
| 12 | trait HasRoleAndPermission  | 
            ||
| 13 | { | 
            ||
| 14 |     use HasRole { | 
            ||
| 15 | HasRole::getPermissions as getRolePermissions;  | 
            ||
| 16 | }  | 
            ||
| 17 | |||
| 18 | private $permissionClass;  | 
            ||
| 19 | |||
| 20 | /**  | 
            ||
| 21 | * Grant user permission by slug.  | 
            ||
| 22 | *  | 
            ||
| 23 | * @param string|array $slug  | 
            ||
| 24 | */  | 
            ||
| 25 | public function grantPermissionBySlug($slug)  | 
            ||
| 36 | |||
| 37 | /**  | 
            ||
| 38 | * Get Permission class.  | 
            ||
| 39 | *  | 
            ||
| 40 | * @return \Yajra\Acl\Models\Permission  | 
            ||
| 41 | */  | 
            ||
| 42 | View Code Duplication | public function getPermissionClass(): Permission  | 
            |
| 43 |     { | 
            ||
| 44 |         if (!isset($this->permissionClass)) { | 
            ||
| 45 |             $this->permissionClass = resolve(config('acl.permission')); | 
            ||
| 46 | }  | 
            ||
| 47 | |||
| 48 | return $this->permissionClass;  | 
            ||
| 49 | }  | 
            ||
| 50 | |||
| 51 | /**  | 
            ||
| 52 | * Grants the given permission to the user.  | 
            ||
| 53 | *  | 
            ||
| 54 | * @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $id  | 
            ||
| 55 | * @param array $attributes  | 
            ||
| 56 | * @param bool $touch  | 
            ||
| 57 | * @return void  | 
            ||
| 58 | */  | 
            ||
| 59 | public function grantPermission($id, array $attributes = [], $touch = true)  | 
            ||
| 64 | |||
| 65 | /**  | 
            ||
| 66 | * Get related permissions.  | 
            ||
| 67 | *  | 
            ||
| 68 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany  | 
            ||
| 69 | */  | 
            ||
| 70 | public function permissions(): BelongsToMany  | 
            ||
| 74 | |||
| 75 | /**  | 
            ||
| 76 | * Grant user permissions by resource.  | 
            ||
| 77 | *  | 
            ||
| 78 | * @param string|array $resource  | 
            ||
| 79 | */  | 
            ||
| 80 | public function grantPermissionByResource($resource)  | 
            ||
| 91 | |||
| 92 | /**  | 
            ||
| 93 | * Revokes the given permission from the user.  | 
            ||
| 94 | *  | 
            ||
| 95 | * @param mixed $ids  | 
            ||
| 96 | * @param bool $touch  | 
            ||
| 97 | * @return int  | 
            ||
| 98 | */  | 
            ||
| 99 | public function revokePermission($ids = null, $touch = true): int  | 
            ||
| 103 | |||
| 104 | /**  | 
            ||
| 105 | * Syncs the given permission(s) with the user.  | 
            ||
| 106 | *  | 
            ||
| 107 | * @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids  | 
            ||
| 108 | * @param bool $detaching  | 
            ||
| 109 | * @return array  | 
            ||
| 110 | */  | 
            ||
| 111 | public function syncPermissions($ids, $detaching = true): array  | 
            ||
| 115 | |||
| 116 | /**  | 
            ||
| 117 | * Revokes all permissions from the user.  | 
            ||
| 118 | *  | 
            ||
| 119 | * @return int  | 
            ||
| 120 | */  | 
            ||
| 121 | public function revokeAllPermissions(): int  | 
            ||
| 125 | |||
| 126 | /**  | 
            ||
| 127 | * Get all user permissions slug.  | 
            ||
| 128 | *  | 
            ||
| 129 | * @return array|null  | 
            ||
| 130 | */  | 
            ||
| 131 | public function getPermissions(): array  | 
            ||
| 132 |     { | 
            ||
| 133 | $rolePermissions = $this->getRolePermissions();  | 
            ||
| 134 |         $userPermissions = $this->permissions->pluck('slug')->toArray(); | 
            ||
| 135 | |||
| 136 | return collect($userPermissions)->merge($rolePermissions)->unique()->toArray();  | 
            ||
| 137 | }  | 
            ||
| 138 | }  | 
            ||
| 139 | 
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: