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 ) { |
||
51 | if ( ! $this->verified_nonce_in_request() ) { |
||
52 | return false; |
||
53 | } |
||
54 | |||
55 | if ( ! $this->is_valid_attach() ) { |
||
56 | return false; |
||
57 | } |
||
58 | |||
59 | return $this->is_valid_save_conditions( $user_id ); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Perform checks whether the current save() request is valid |
||
64 | * |
||
65 | * @param int $user_id ID of the user against which save() is ran |
||
66 | * @return bool |
||
67 | **/ |
||
68 | public function is_valid_save_conditions( $user_id ) { |
||
91 | |||
92 | /** |
||
93 | * Perform save operation after successful is_valid_save() check. |
||
94 | * The call is propagated to all fields in the container. |
||
95 | * |
||
96 | * @param int $user_id ID of the user against which save() is ran |
||
97 | **/ |
||
98 | View Code Duplication | public function save( $user_id ) { |
|
111 | |||
112 | /** |
||
113 | * Perform checks whether the container should be attached during the current request |
||
114 | * |
||
115 | * @return bool True if the container is allowed to be attached |
||
116 | **/ |
||
117 | public function _is_valid_attach() { |
||
124 | |||
125 | /** |
||
126 | * Add the container to the user |
||
127 | **/ |
||
128 | public function attach() { |
||
133 | |||
134 | /** |
||
135 | * Whether we're on the user profile page |
||
136 | **/ |
||
137 | public function is_profile_page() { |
||
142 | |||
143 | /** |
||
144 | * Perform checks whether the container should be seen for the currently logged in user |
||
145 | * |
||
146 | * @return bool True if the current user is allowed to see the container |
||
147 | **/ |
||
148 | public function is_valid_show_for() { |
||
169 | |||
170 | /** |
||
171 | * Output the container markup |
||
172 | **/ |
||
173 | public function render( $user_profile = null ) { |
||
187 | |||
188 | /** |
||
189 | * Set the user ID the container will operate with. |
||
190 | * |
||
191 | * @param int $user_id |
||
192 | **/ |
||
193 | public function set_user_id( $user_id ) { |
||
197 | |||
198 | /** |
||
199 | * Validate and parse the show_for logic rules. |
||
200 | * |
||
201 | * @param array $show_for |
||
202 | * @return array |
||
203 | */ |
||
204 | protected function parse_show_for( $show_for ) { |
||
240 | |||
241 | /** |
||
242 | * COMMON USAGE METHODS |
||
243 | */ |
||
244 | |||
245 | /** |
||
246 | * Show the container only on users who have the $role role. |
||
247 | * |
||
248 | * @param string $role |
||
249 | * @return object $this |
||
250 | **/ |
||
251 | public function show_on_user_role( $role ) { |
||
256 | |||
257 | /** |
||
258 | * Show the container only for users who have either capabilities or roles setup |
||
259 | * |
||
260 | * @param array $show_for |
||
261 | * @return object $this |
||
262 | **/ |
||
263 | public function show_for( $show_for ) { |
||
268 | } |
||
269 |
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: