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 |
||
| 8 | class User_Meta_Container extends Container { |
||
| 9 | protected $user_id; |
||
| 10 | |||
| 11 | public $settings = array( |
||
| 12 | 'show_on' => array( |
||
| 13 | 'role' => array(), |
||
| 14 | ), |
||
| 15 | 'show_for' => array( |
||
| 16 | 'relation' => 'AND', |
||
| 17 | 'edit_users', |
||
| 18 | ), |
||
| 19 | ); |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Create a new container |
||
| 23 | * |
||
| 24 | * @param string $unique_id Unique id of the container |
||
| 25 | * @param string $title title of the container |
||
| 26 | * @param string $type Type of the container |
||
| 27 | **/ |
||
| 28 | View Code Duplication | public function __construct( $unique_id, $title, $type ) { |
|
| 35 | |||
| 36 | /** |
||
| 37 | * Bind attach() and save() to the appropriate WordPress actions. |
||
| 38 | **/ |
||
| 39 | public function init() { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Checks whether the current request is valid |
||
| 47 | * |
||
| 48 | * @return bool |
||
| 49 | **/ |
||
| 50 | public function is_valid_save( $user_id = 0 ) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Perform save operation after successful is_valid_save() check. |
||
| 64 | * The call is propagated to all fields in the container. |
||
| 65 | * |
||
| 66 | * @param int $user_id ID of the user against which save() is ran |
||
| 67 | **/ |
||
| 68 | View Code Duplication | public function save( $user_id ) { |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Perform checks whether the container should be attached during the current request |
||
| 84 | * |
||
| 85 | * @return bool True if the container is allowed to be attached |
||
| 86 | **/ |
||
| 87 | public function is_valid_attach_for_request() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Check container attachment rules against object id |
||
| 97 | * |
||
| 98 | * @return bool |
||
| 99 | **/ |
||
| 100 | public function is_valid_attach_for_object( $object_id = null ) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Add the container to the user |
||
| 131 | **/ |
||
| 132 | public function attach() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Whether we're on the user profile page |
||
| 140 | **/ |
||
| 141 | public function is_profile_page() { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Perform checks whether the container should be seen for the currently logged in user |
||
| 149 | * |
||
| 150 | * @return bool True if the current user is allowed to see the container |
||
| 151 | **/ |
||
| 152 | public function is_valid_show_for() { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Output the container markup |
||
| 176 | **/ |
||
| 177 | public function render( $user_profile = null ) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Set the user ID the container will operate with. |
||
| 194 | * |
||
| 195 | * @param int $user_id |
||
| 196 | **/ |
||
| 197 | public function set_user_id( $user_id ) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Validate and parse the show_for logic rules. |
||
| 204 | * |
||
| 205 | * @param array $show_for |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | protected function parse_show_for( $show_for ) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * COMMON USAGE METHODS |
||
| 247 | */ |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Show the container only on users who have the $role role. |
||
| 251 | * |
||
| 252 | * @param string $role |
||
| 253 | * @return object $this |
||
| 254 | **/ |
||
| 255 | public function show_on_user_role( $role ) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Show the container only for users who have either capabilities or roles setup |
||
| 263 | * |
||
| 264 | * @param array $show_for |
||
| 265 | * @return object $this |
||
| 266 | **/ |
||
| 267 | public function show_for( $show_for ) { |
||
| 272 | } |
||
| 273 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: