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 |
||
67 | |||
68 | /** |
||
69 | * A permission can be applied to roles. |
||
70 | */ |
||
71 | public function roles(): BelongsToMany |
||
80 | |||
81 | /** |
||
82 | * A permission belongs to some users of the model associated with its guard. |
||
83 | */ |
||
84 | View Code Duplication | public function users(): MorphToMany |
|
94 | |||
95 | /** |
||
96 | * Find a permission by its name (and optionally guardName). |
||
97 | * |
||
98 | * @param string $name |
||
99 | * @param string|null $guardName |
||
100 | * |
||
101 | * @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist |
||
102 | * |
||
103 | * @return \Spatie\Permission\Contracts\Permission |
||
104 | */ |
||
105 | View Code Duplication | public static function findByName(string $name, $guardName = null): PermissionContract |
|
115 | |||
116 | /** |
||
117 | * Find a permission by its id (and optionally guardName). |
||
118 | * |
||
119 | * @param int $id |
||
120 | * @param string|null $guardName |
||
121 | * |
||
122 | * @throws \Spatie\Permission\Exceptions\PermissionDoesNotExist |
||
123 | * |
||
124 | * @return \Spatie\Permission\Contracts\Permission |
||
125 | */ |
||
126 | View Code Duplication | public static function findById(int $id, $guardName = null): PermissionContract |
|
137 | |||
138 | /** |
||
139 | * Find or create permission by its name (and optionally guardName). |
||
140 | * |
||
141 | * @param string $name |
||
142 | * @param string|null $guardName |
||
143 | * |
||
144 | * @return \Spatie\Permission\Contracts\Permission |
||
145 | */ |
||
146 | View Code Duplication | public static function findOrCreate(string $name, $guardName = null): PermissionContract |
|
157 | |||
158 | /** |
||
159 | * Get the current cached permissions. |
||
160 | */ |
||
161 | protected static function getPermissions(array $params = []): Collection |
||
167 | } |
||
168 |
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.