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 |
||
| 23 | class AuthManager extends \yii\rbac\PhpManager |
||
| 24 | { |
||
| 25 | public $itemFile = '@hipanel/rbac/files/items.php'; |
||
| 26 | public $ruleFile = '@hipanel/rbac/files/rules.php'; |
||
| 27 | public $assignmentFile = '@hipanel/rbac/files/assignments.php'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Set permission. |
||
| 31 | * @param string $name |
||
| 32 | * @param string $description |
||
| 33 | * @return Item |
||
| 34 | */ |
||
| 35 | 3 | public function setPermission($name, $description = null) |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Set role. |
||
| 42 | * @param string $name |
||
| 43 | * @param string $description |
||
| 44 | * @return Item |
||
| 45 | */ |
||
| 46 | 3 | public function setRole($name, $description = null) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Set item by type and name. |
||
| 53 | * Created if not exists else updates. |
||
| 54 | * @param string $type |
||
| 55 | * @param string $name |
||
| 56 | * @param string $description |
||
| 57 | * @return Item |
||
| 58 | */ |
||
| 59 | 3 | public function setItem($type, $name, $description = null) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Create item by type and name. |
||
| 72 | * @param string $type |
||
| 73 | * @param string $name |
||
| 74 | * @throws InvalidParamException |
||
| 75 | * @return Item |
||
| 76 | */ |
||
| 77 | 3 | public function createItem($type, $name) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Set child. |
||
| 90 | * @param string|Item $parent |
||
| 91 | * @param string|Item $child |
||
| 92 | * @return bool |
||
| 93 | */ |
||
| 94 | 3 | public function setChild($parent, $child) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Assigns a role to a user. |
||
| 119 | * @param Role $role |
||
| 120 | * @param string|integer $userId the user ID (see [[\yii\web\User::id]]) |
||
| 121 | * @throws \Exception when given wrong role name or the role has already been assigned to the user |
||
| 122 | * @return Assignment the role assignment information |
||
| 123 | */ |
||
| 124 | 6 | public function setAssignment($role, $userId) |
|
| 139 | |||
| 140 | 6 | public function getRoles() |
|
| 144 | |||
| 145 | public function getPermissions() |
||
| 149 | |||
| 150 | public function getAllAssignments() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * We don't keep all the assignments, only basic. |
||
| 157 | * @see forceSaveAssignments |
||
| 158 | */ |
||
| 159 | 6 | protected function saveAssignments() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Create only basic assignments before saving. |
||
| 165 | */ |
||
| 166 | 3 | public function saveBasicAssignments() |
|
| 170 | |||
| 171 | 6 | public function checkAccess($userId, $permission, $params = []) |
|
| 180 | |||
| 181 | protected $_currentUserRole; |
||
| 182 | |||
| 183 | 6 | public function setCurrentUserRole() |
|
| 192 | |||
| 193 | 6 | public function getIdentity() |
|
| 197 | } |
||
| 198 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: