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 | /** |
||
| 25 | * Set permission. |
||
| 26 | * @param string $name |
||
| 27 | * @param string $description |
||
| 28 | * @return Item |
||
| 29 | */ |
||
| 30 | 1 | public function setPermission($name, $description = null) |
|
| 34 | |||
| 35 | /** |
||
| 36 | * Set role. |
||
| 37 | * @param string $name |
||
| 38 | * @param string $description |
||
| 39 | * @return Item |
||
| 40 | */ |
||
| 41 | 1 | public function setRole($name, $description = null) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * Set item by type and name. |
||
| 48 | * Created if not exists else updates. |
||
| 49 | * @param string $type |
||
| 50 | * @param string $name |
||
| 51 | * @param string $description |
||
| 52 | * @return Item |
||
| 53 | */ |
||
| 54 | 1 | public function setItem($type, $name, $description = null) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Create item by type and name. |
||
| 67 | * @param string $type |
||
| 68 | * @param string $name |
||
| 69 | * @throws InvalidParamException |
||
| 70 | * @return Item |
||
| 71 | */ |
||
| 72 | 1 | public function createItem($type, $name) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Set child. |
||
| 85 | * @param string|Item $parent |
||
| 86 | * @param string|Item $child |
||
| 87 | * @return bool |
||
| 88 | */ |
||
| 89 | 1 | public function setChild($parent, $child) |
|
| 110 | |||
| 111 | 1 | public function setAssignment($role, $userId) |
|
| 125 | } |
||
| 126 |
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: