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 |
||
22 | class AuthManager extends \yii\rbac\PhpManager |
||
23 | { |
||
24 | public $itemFile = '@hipanel/rbac/files/items.php'; |
||
25 | public $ruleFile = '@hipanel/rbac/files/rules.php'; |
||
26 | public $assignmentFile = '@hipanel/rbac/files/assignments.php'; |
||
27 | |||
28 | /** |
||
29 | * Set permission. |
||
30 | * @param string $name |
||
31 | * @param string $description |
||
32 | * @return Item |
||
33 | */ |
||
34 | 3 | public function setPermission($name, $description = null) |
|
38 | |||
39 | /** |
||
40 | * Set role. |
||
41 | * @param string $name |
||
42 | * @param string $description |
||
43 | * @return Item |
||
44 | */ |
||
45 | 3 | public function setRole($name, $description = null) |
|
49 | |||
50 | /** |
||
51 | * Set item by type and name. |
||
52 | * Created if not exists else updates. |
||
53 | * @param string $type |
||
54 | * @param string $name |
||
55 | * @param string $description |
||
56 | * @return Item |
||
57 | */ |
||
58 | 3 | public function setItem($type, $name, $description = null) |
|
68 | |||
69 | /** |
||
70 | * Create item by type and name. |
||
71 | * @param string $type |
||
72 | * @param string $name |
||
73 | * @throws InvalidParamException |
||
74 | * @return Item |
||
75 | */ |
||
76 | 3 | public function createItem($type, $name) |
|
86 | |||
87 | /** |
||
88 | * Set child. |
||
89 | * @param string|Item $parent |
||
90 | * @param string|Item $child |
||
91 | * @return bool |
||
92 | */ |
||
93 | 3 | public function setChild($parent, $child) |
|
115 | |||
116 | 6 | public function setAssignment($role, $userId) |
|
131 | |||
132 | 6 | public function getRoles() |
|
136 | |||
137 | public function getPermissions() |
||
141 | |||
142 | public function getAllAssignments() |
||
146 | |||
147 | /** |
||
148 | * We don't keep all the assignments, only basic. |
||
149 | * @see forceSaveAssignments |
||
150 | */ |
||
151 | 6 | protected function saveAssignments() |
|
154 | |||
155 | /** |
||
156 | * Create only basic assignments before saving. |
||
157 | */ |
||
158 | 3 | public function saveBasicAssignments() |
|
162 | } |
||
163 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: