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 |
||
21 | class RoleManager |
||
22 | { |
||
23 | /** |
||
24 | * Define all permission and make usable from laravel application |
||
25 | * |
||
26 | * @return bool |
||
27 | */ |
||
28 | public function defineAllPermissions() |
||
62 | |||
63 | /** |
||
64 | * Assign Role to user |
||
65 | * |
||
66 | * @param int|User $user User Instance or user id |
||
67 | * @param int|string|Roles $role Role Instance, role name, or role id |
||
68 | * |
||
69 | * @return bool |
||
70 | */ |
||
71 | View Code Duplication | public function assignRole($user, $role) |
|
87 | |||
88 | /** |
||
89 | * Remove Role from user |
||
90 | * |
||
91 | * @param int|User $user User Instance or user id |
||
92 | * @param int|string|Roles $role Role Instance, role name, or role id |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | View Code Duplication | public function removeRole($user, $role) |
|
113 | |||
114 | /** |
||
115 | * Get All permissions |
||
116 | * |
||
117 | * @return array|\Illuminate\Database\Eloquent\Collection|static[] |
||
118 | */ |
||
119 | public function getPermissions() |
||
126 | |||
127 | } |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.