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 /** MicroDbRBAC */ |
||
21 | class DbRbac extends Rbac |
||
22 | { |
||
23 | /** |
||
24 | * Constructor file RBAC |
||
25 | * |
||
26 | * @inheritdoc |
||
27 | */ |
||
28 | public function __construct(array $params = []) |
||
42 | |||
43 | /** |
||
44 | * Assign RBAC element into user |
||
45 | * |
||
46 | * @access public |
||
47 | * |
||
48 | * @param integer $userId user ID |
||
49 | * @param string $name assign element name |
||
50 | * |
||
51 | * @return bool |
||
52 | */ |
||
53 | public function assign($userId, $name) |
||
63 | |||
64 | /** |
||
65 | * Check privileges to operation |
||
66 | * |
||
67 | * @access public |
||
68 | * |
||
69 | * @param integer $userId user id |
||
70 | * @param string $action checked action |
||
71 | * @param array $data action params |
||
72 | * |
||
73 | * @return boolean |
||
74 | * @throws \Micro\Base\Exception |
||
75 | */ |
||
76 | public function check($userId, $action, array $data = []) |
||
84 | |||
85 | /** |
||
86 | * Add new element into RBAC rules |
||
87 | * |
||
88 | * @access public |
||
89 | * |
||
90 | * @param string $name element name |
||
91 | * @param int $type element type |
||
92 | * @param string $based based element name |
||
93 | * @param string $data element params |
||
94 | * |
||
95 | * @return bool |
||
96 | */ |
||
97 | public function create($name, $type = self::TYPE_ROLE, $based = null, $data = null) |
||
120 | |||
121 | /** |
||
122 | * Delete element from RBAC rules |
||
123 | * |
||
124 | * @access public |
||
125 | * |
||
126 | * @param string $name element name |
||
127 | * |
||
128 | * @result void |
||
129 | * @throws \Micro\Base\Exception |
||
130 | */ |
||
131 | public function delete($name) |
||
138 | |||
139 | /** |
||
140 | * Get raw roles |
||
141 | * |
||
142 | * @access public |
||
143 | * |
||
144 | * @param int $pdo PHPDataObject fetch key |
||
145 | * |
||
146 | * @return mixed |
||
147 | * @throws \Micro\Base\Exception |
||
148 | */ |
||
149 | public function rawRoles($pdo = \PDO::FETCH_ASSOC) |
||
158 | |||
159 | /** |
||
160 | * Recursive delete roles from array |
||
161 | * |
||
162 | * @access public |
||
163 | * |
||
164 | * @param array $tree elements tree |
||
165 | * |
||
166 | * @return void |
||
167 | */ |
||
168 | public function recursiveDelete(&$tree) |
||
180 | } |
||
181 |
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.