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) |
|
| 60 | { |
||
| 61 | 3 | $item = $this->getItem($name) ?: $this->createItem($type, $name); |
|
| 62 | 3 | if ($description) { |
|
|
|
|||
| 63 | $item->description = $description; |
||
| 64 | } |
||
| 65 | 3 | $this->add($item); |
|
| 66 | |||
| 67 | 3 | return $item; |
|
| 68 | } |
||
| 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) |
|
| 95 | { |
||
| 96 | 3 | View Code Duplication | if (is_string($parent)) { |
| 97 | 3 | $name = $parent; |
|
| 98 | 3 | $parent = $this->getItem($parent); |
|
| 99 | 3 | if (is_null($parent)) { |
|
| 100 | throw new InvalidParamException("Unknown parent:$name at setChild"); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | 3 | View Code Duplication | if (is_string($child)) { |
| 104 | 3 | $name = $child; |
|
| 105 | 3 | $child = $this->getItem($child); |
|
| 106 | 3 | if (is_null($child)) { |
|
| 107 | throw new InvalidParamException("Unknown child:$name at setChild"); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | 3 | if (isset($this->children[$parent->name][$child->name])) { |
|
| 111 | return false; |
||
| 112 | } |
||
| 113 | |||
| 114 | 3 | return $this->addChild($parent, $child); |
|
| 115 | } |
||
| 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) |
|
| 125 | { |
||
| 126 | 6 | View Code Duplication | if (is_string($role)) { |
| 127 | 6 | $name = $role; |
|
| 128 | 6 | $role = $this->getRole($role); |
|
| 129 | 6 | if (is_null($role)) { |
|
| 130 | throw new InvalidParamException("Unknown role:$name at setAssignment"); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | 6 | if (isset($this->assignments[$userId][$role->name])) { |
|
| 134 | return false; |
||
| 135 | } |
||
| 136 | |||
| 137 | 6 | return $this->assign($role, $userId); |
|
| 138 | } |
||
| 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 = []) |
|
| 172 | { |
||
| 173 | 6 | $this->setCurrentUserRole(); |
|
| 174 | 6 | if ($userId === $this->getIdentity()->id) { |
|
| 175 | $userId = $this->getIdentity()->username; |
||
| 176 | } |
||
| 177 | |||
| 178 | 6 | return parent::checkAccess($userId, $permission, $params); |
|
| 179 | } |
||
| 180 | |||
| 181 | protected $_currentUserRole; |
||
| 182 | |||
| 183 | 6 | public function setCurrentUserRole() |
|
| 184 | { |
||
| 185 | 6 | if ($this->_currentUserRole === null) { |
|
| 186 | 6 | $this->_currentUserRole = $this->getIdentity()->type ?: ''; |
|
| 187 | 6 | if ($this->_currentUserRole) { |
|
| 188 | $this->setAssignment($this->_currentUserRole, $this->getIdentity()->username); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | 6 | } |
|
| 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: