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 |
||
15 | class Roles { |
||
16 | /** |
||
17 | * Map of roles we care about, and their corresponding minimum capabilities. |
||
18 | * |
||
19 | * @access protected |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $capability_translations = array( |
||
24 | 'administrator' => 'manage_options', |
||
25 | 'editor' => 'edit_others_posts', |
||
26 | 'author' => 'publish_posts', |
||
27 | 'contributor' => 'edit_posts', |
||
28 | 'subscriber' => 'read', |
||
29 | ); |
||
30 | |||
31 | /** |
||
32 | * Get the role of the current user. |
||
33 | * |
||
34 | * @access public |
||
35 | * |
||
36 | * @return string|boolean Current user's role, false if not enough capabilities for any of the roles. |
||
37 | */ |
||
38 | View Code Duplication | public function translate_current_user_to_role() { |
|
47 | |||
48 | /** |
||
49 | * Get the role of a particular user. |
||
50 | * |
||
51 | * @access public |
||
52 | * |
||
53 | * @param \WP_User $user User object. |
||
54 | * @return string|boolean User's role, false if not enough capabilities for any of the roles. |
||
55 | */ |
||
56 | View Code Duplication | public function translate_user_to_role( $user ) { |
|
65 | |||
66 | /** |
||
67 | * Get the minimum capability for a role. |
||
68 | * |
||
69 | * @access public |
||
70 | * |
||
71 | * @param string $role Role name. |
||
72 | * @return string|boolean Capability, false if role isn't mapped to any capabilities. |
||
73 | */ |
||
74 | public function translate_role_to_cap( $role ) { |
||
81 | } |
||
82 |