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 |
||
| 17 | class Permission extends Model implements PermissionContract |
||
| 18 | { |
||
| 19 | use HasRoles; |
||
| 20 | use RefreshesPermissionCache; |
||
| 21 | |||
| 22 | protected $guarded = ['id']; |
||
| 23 | |||
| 24 | public function __construct(array $attributes = []) |
||
| 32 | |||
| 33 | public static function create(array $attributes = []) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Create permissions for resource controllers. |
||
| 48 | * @param string|array $permissions |
||
| 49 | * @return \Illuminate\Support\Collection |
||
| 50 | */ |
||
| 51 | public static function createResource(...$permissions): Collection |
||
| 63 | |||
| 64 | /** |
||
| 65 | * A permission can be applied to roles. |
||
| 66 | */ |
||
| 67 | public function roles(): BelongsToMany |
||
| 76 | |||
| 77 | /** |
||
| 78 | * A permission belongs to some users of the model associated with its guard. |
||
| 79 | */ |
||
| 80 | View Code Duplication | public function users(): MorphToMany |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Find a permission by its name (and optionally guardName). |
||
| 93 | * |
||
| 94 | * @param string $name |
||
| 95 | * @param string|null $guardName |
||
| 96 | * |
||
| 97 | * @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist |
||
| 98 | * |
||
| 99 | * @return \Spatie\Permission\Contracts\Permission |
||
| 100 | */ |
||
| 101 | View Code Duplication | public static function findByName(string $name, $guardName = null): PermissionContract |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Find a permission by its id (and optionally guardName). |
||
| 114 | * |
||
| 115 | * @param int $id |
||
| 116 | * @param string|null $guardName |
||
| 117 | * |
||
| 118 | * @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist |
||
| 119 | * |
||
| 120 | * @return \Spatie\Permission\Contracts\Permission |
||
| 121 | */ |
||
| 122 | View Code Duplication | public static function findById(int $id, $guardName = null): PermissionContract |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Find or create permission by its name (and optionally guardName). |
||
| 136 | * |
||
| 137 | * @param string $name |
||
| 138 | * @param string|null $guardName |
||
| 139 | * |
||
| 140 | * @return \Spatie\Permission\Contracts\Permission |
||
| 141 | */ |
||
| 142 | View Code Duplication | public static function findOrCreate(string $name, $guardName = null): PermissionContract |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Get the current cached permissions. |
||
| 156 | */ |
||
| 157 | protected static function getPermissions(array $params = []): Collection |
||
| 163 | } |
||
| 164 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.