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 | * @static |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected static $capability_translations = array( |
||
| 25 | 'administrator' => 'manage_options', |
||
| 26 | 'editor' => 'edit_others_posts', |
||
| 27 | 'author' => 'publish_posts', |
||
| 28 | 'contributor' => 'edit_posts', |
||
| 29 | 'subscriber' => 'read', |
||
| 30 | ); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Get the role of the current user. |
||
| 34 | * |
||
| 35 | * @return string|boolean Current user's role, false if not enough capabilities for any of the roles. |
||
| 36 | */ |
||
| 37 | View Code Duplication | public static function translate_current_user_to_role() { |
|
| 46 | |||
| 47 | /** |
||
| 48 | * Get the role of a particular user. |
||
| 49 | * |
||
| 50 | * @param \WP_User $user User object. |
||
| 51 | * @return string|boolean User's role, false if not enough capabilities for any of the roles. |
||
| 52 | */ |
||
| 53 | View Code Duplication | public static function translate_user_to_role( $user ) { |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Get the minimum capability for a role. |
||
| 65 | * |
||
| 66 | * @param string $role Role name. |
||
| 67 | * @return string|boolean Capability, false if role isn't mapped to any capabilities. |
||
| 68 | */ |
||
| 69 | public static function translate_role_to_cap( $role ) { |
||
| 76 | } |
||
| 77 |