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 |
||
| 26 | class WP_Roles { |
||
| 27 | /** |
||
| 28 | * List of roles and capabilities. |
||
| 29 | * |
||
| 30 | * @since 2.0.0 |
||
| 31 | * @access public |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | public $roles; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * List of the role objects. |
||
| 38 | * |
||
| 39 | * @since 2.0.0 |
||
| 40 | * @access public |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | public $role_objects = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * List of role names. |
||
| 47 | * |
||
| 48 | * @since 2.0.0 |
||
| 49 | * @access public |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | public $role_names = array(); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Option name for storing role list. |
||
| 56 | * |
||
| 57 | * @since 2.0.0 |
||
| 58 | * @access public |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | public $role_key; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Whether to use the database for retrieval and storage. |
||
| 65 | * |
||
| 66 | * @since 2.1.0 |
||
| 67 | * @access public |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | public $use_db = true; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Constructor |
||
| 74 | * |
||
| 75 | * @since 2.0.0 |
||
| 76 | */ |
||
| 77 | public function __construct() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Make private/protected methods readable for backward compatibility. |
||
| 83 | * |
||
| 84 | * @since 4.0.0 |
||
| 85 | * @access public |
||
| 86 | * |
||
| 87 | * @param callable $name Method to call. |
||
| 88 | * @param array $arguments Arguments to pass when calling. |
||
| 89 | * @return mixed|false Return value of the callback, false otherwise. |
||
| 90 | */ |
||
| 91 | public function __call( $name, $arguments ) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Set up the object properties. |
||
| 100 | * |
||
| 101 | * The role key is set to the current prefix for the $wpdb object with |
||
| 102 | * 'user_roles' appended. If the $wp_user_roles global is set, then it will |
||
| 103 | * be used and the role option will not be updated or used. |
||
| 104 | * |
||
| 105 | * @since 2.1.0 |
||
| 106 | * @access protected |
||
| 107 | * |
||
| 108 | * @global wpdb $wpdb WordPress database abstraction object. |
||
| 109 | * @global array $wp_user_roles Used to set the 'roles' property value. |
||
| 110 | */ |
||
| 111 | protected function _init() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Reinitialize the object |
||
| 134 | * |
||
| 135 | * Recreates the role objects. This is typically called only by switch_to_blog() |
||
| 136 | * after switching wpdb to a new site ID. |
||
| 137 | * |
||
| 138 | * @since 3.5.0 |
||
| 139 | * @access public |
||
| 140 | * |
||
| 141 | * @global wpdb $wpdb WordPress database abstraction object. |
||
| 142 | */ |
||
| 143 | public function reinit() { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Add role name with capabilities to list. |
||
| 166 | * |
||
| 167 | * Updates the list of roles, if the role doesn't already exist. |
||
| 168 | * |
||
| 169 | * The capabilities are defined in the following format `array( 'read' => true );` |
||
| 170 | * To explicitly deny a role a capability you set the value for that capability to false. |
||
| 171 | * |
||
| 172 | * @since 2.0.0 |
||
| 173 | * @access public |
||
| 174 | * |
||
| 175 | * @param string $role Role name. |
||
| 176 | * @param string $display_name Role display name. |
||
| 177 | * @param array $capabilities List of role capabilities in the above format. |
||
| 178 | * @return WP_Role|void WP_Role object, if role is added. |
||
| 179 | */ |
||
| 180 | public function add_role( $role, $display_name, $capabilities = array() ) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Remove role by name. |
||
| 198 | * |
||
| 199 | * @since 2.0.0 |
||
| 200 | * @access public |
||
| 201 | * |
||
| 202 | * @param string $role Role name. |
||
| 203 | */ |
||
| 204 | public function remove_role( $role ) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Add capability to role. |
||
| 221 | * |
||
| 222 | * @since 2.0.0 |
||
| 223 | * @access public |
||
| 224 | * |
||
| 225 | * @param string $role Role name. |
||
| 226 | * @param string $cap Capability name. |
||
| 227 | * @param bool $grant Optional, default is true. Whether role is capable of performing capability. |
||
| 228 | */ |
||
| 229 | View Code Duplication | public function add_cap( $role, $cap, $grant = true ) { |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Remove capability from role. |
||
| 240 | * |
||
| 241 | * @since 2.0.0 |
||
| 242 | * @access public |
||
| 243 | * |
||
| 244 | * @param string $role Role name. |
||
| 245 | * @param string $cap Capability name. |
||
| 246 | */ |
||
| 247 | View Code Duplication | public function remove_cap( $role, $cap ) { |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Retrieve role object by name. |
||
| 258 | * |
||
| 259 | * @since 2.0.0 |
||
| 260 | * @access public |
||
| 261 | * |
||
| 262 | * @param string $role Role name. |
||
| 263 | * @return WP_Role|null WP_Role object if found, null if the role does not exist. |
||
| 264 | */ |
||
| 265 | public function get_role( $role ) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Retrieve list of role names. |
||
| 274 | * |
||
| 275 | * @since 2.0.0 |
||
| 276 | * @access public |
||
| 277 | * |
||
| 278 | * @return array List of role names. |
||
| 279 | */ |
||
| 280 | public function get_names() { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Whether role name is currently in the list of available roles. |
||
| 286 | * |
||
| 287 | * @since 2.0.0 |
||
| 288 | * @access public |
||
| 289 | * |
||
| 290 | * @param string $role Role name to look up. |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | public function is_role( $role ) { |
||
| 296 | } |
||
| 297 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..