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 |
||
| 24 | class Table extends \Bluz\Db\Table |
||
| 25 | { |
||
| 26 | const BASIC_ADMIN = 'admin'; |
||
| 27 | const BASIC_GUEST = 'guest'; |
||
| 28 | const BASIC_MEMBER = 'member'; |
||
| 29 | const BASIC_SYSTEM = 'system'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Table |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $name = 'acl_roles'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Primary key(s) |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $primary = array('id'); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $basicRoles = ['admin', 'guest', 'member', 'system']; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Init table relations |
||
| 51 | * @return void |
||
| 52 | */ |
||
| 53 | 1 | public function init() |
|
| 54 | { |
||
| 55 | 1 | $this->linkTo('id', 'UsersRoles', 'roleId'); |
|
| 56 | 1 | $this->linkToMany('Users', 'UsersRoles'); |
|
| 57 | 1 | } |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Get all roles in system |
||
| 61 | * |
||
| 62 | * @return array |
||
| 63 | */ |
||
| 64 | 2 | public function getRoles() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Get all basic roles |
||
| 71 | * |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | 1 | public function getBasicRoles() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Get all user roles in system |
||
| 81 | * |
||
| 82 | * @param integer $userId |
||
| 83 | * @return array of rows |
||
| 84 | */ |
||
| 85 | public function getUserRoles($userId) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get all user roles in system |
||
| 98 | * |
||
| 99 | * @param integer $userId |
||
| 100 | * @return array of identity |
||
| 101 | */ |
||
| 102 | 4 | View Code Duplication | public function getUserRolesIdentity($userId) |
| 117 | } |
||
| 118 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.