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 |
||
9 | class User_Meta_Container extends Container { |
||
10 | protected $user_id; |
||
11 | |||
12 | public $settings = array( |
||
13 | 'show_on' => array( |
||
14 | 'role' => array(), |
||
15 | ), |
||
16 | 'show_for' => array( |
||
17 | 'relation' => 'AND', |
||
18 | 'edit_users', |
||
19 | ), |
||
20 | ); |
||
21 | |||
22 | /** |
||
23 | * Create a new container |
||
24 | * |
||
25 | * @param string $unique_id Unique id of the container |
||
26 | * @param string $title title of the container |
||
27 | * @param string $type Type of the container |
||
28 | **/ |
||
29 | public function __construct( $unique_id, $title, $type ) { |
||
36 | |||
37 | /** |
||
38 | * Bind attach() and save() to the appropriate WordPress actions. |
||
39 | **/ |
||
40 | public function init() { |
||
45 | |||
46 | /** |
||
47 | * Checks whether the current request is valid |
||
48 | * |
||
49 | * @return bool |
||
50 | **/ |
||
51 | public function is_valid_save( $user_id = 0 ) { |
||
62 | |||
63 | /** |
||
64 | * Perform save operation after successful is_valid_save() check. |
||
65 | * The call is propagated to all fields in the container. |
||
66 | * |
||
67 | * @param int $user_id ID of the user against which save() is ran |
||
68 | **/ |
||
69 | View Code Duplication | public function save( $user_id ) { |
|
82 | |||
83 | /** |
||
84 | * Perform checks whether the container should be attached during the current request |
||
85 | * |
||
86 | * @return bool True if the container is allowed to be attached |
||
87 | **/ |
||
88 | public function is_valid_attach_for_request() { |
||
95 | |||
96 | /** |
||
97 | * Check container attachment rules against object id |
||
98 | * |
||
99 | * @param int $object_id |
||
100 | * @return bool |
||
101 | **/ |
||
102 | public function is_valid_attach_for_object( $object_id = null ) { |
||
130 | |||
131 | /** |
||
132 | * Add the container to the user |
||
133 | **/ |
||
134 | public function attach() { |
||
139 | |||
140 | /** |
||
141 | * Whether we're on the user profile page |
||
142 | **/ |
||
143 | public function is_profile_page() { |
||
148 | |||
149 | /** |
||
150 | * Perform checks whether the container should be seen for the currently logged in user |
||
151 | * |
||
152 | * @return bool True if the current user is allowed to see the container |
||
153 | **/ |
||
154 | public function is_valid_show_for() { |
||
175 | |||
176 | /** |
||
177 | * Output the container markup |
||
178 | **/ |
||
179 | public function render( $user_profile = null ) { |
||
193 | |||
194 | /** |
||
195 | * Set the user ID the container will operate with. |
||
196 | * |
||
197 | * @param int $user_id |
||
198 | **/ |
||
199 | public function set_user_id( $user_id ) { |
||
203 | |||
204 | /** |
||
205 | * Validate and parse the show_for logic rules. |
||
206 | * |
||
207 | * @param array $show_for |
||
208 | * @return array |
||
209 | */ |
||
210 | protected function parse_show_for( $show_for ) { |
||
237 | |||
238 | /** |
||
239 | * COMMON USAGE METHODS |
||
240 | */ |
||
241 | |||
242 | /** |
||
243 | * Show the container only on users who have the $role role. |
||
244 | * |
||
245 | * @param string $role |
||
246 | * @return object $this |
||
247 | **/ |
||
248 | public function show_on_user_role( $role ) { |
||
253 | |||
254 | /** |
||
255 | * Show the container only for users who have either capabilities or roles setup |
||
256 | * |
||
257 | * @param array $show_for |
||
258 | * @return object $this |
||
259 | **/ |
||
260 | public function show_for( $show_for ) { |
||
265 | } |
||
266 |
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: