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 user meta container |
||
24 | * |
||
25 | * @param string $title Unique title of the container |
||
26 | **/ |
||
27 | public function __construct( $title ) { |
||
34 | |||
35 | /** |
||
36 | * Bind attach() and save() to the appropriate WordPress actions. |
||
37 | **/ |
||
38 | public function init() { |
||
43 | |||
44 | /** |
||
45 | * Add the container to the user |
||
46 | **/ |
||
47 | public function attach() { |
||
52 | |||
53 | /** |
||
54 | * Whether we're on the user profile page |
||
55 | **/ |
||
56 | public function is_profile_page() { |
||
61 | |||
62 | /** |
||
63 | * Perform checks whether the container should be seen for the currently logged in user |
||
64 | * |
||
65 | * @return bool True if the current user is allowed to see the container |
||
66 | **/ |
||
67 | public function is_valid_show_for() { |
||
88 | |||
89 | /** |
||
90 | * Perform checks whether the container should be attached during the current request |
||
91 | * |
||
92 | * @return bool True if the container is allowed to be attached |
||
93 | **/ |
||
94 | public function is_valid_attach() { |
||
101 | |||
102 | /** |
||
103 | * Perform checks whether the current save() request is valid |
||
104 | * |
||
105 | * @param int $user_id ID of the user against which save() is ran |
||
106 | * @return bool |
||
107 | **/ |
||
108 | public function is_valid_save_conditions( $user_id ) { |
||
131 | |||
132 | /** |
||
133 | * Checks whether the current request is valid |
||
134 | * |
||
135 | * @return bool |
||
136 | **/ |
||
137 | public function is_valid_save( $user_id = 0 ) { |
||
148 | |||
149 | /** |
||
150 | * Perform save operation after successful is_valid_save() check. |
||
151 | * The call is propagated to all fields in the container. |
||
152 | * |
||
153 | * @param int $user_id ID of the user against which save() is ran |
||
154 | **/ |
||
155 | View Code Duplication | public function save( $user_id ) { |
|
168 | |||
169 | /** |
||
170 | * Output the container markup |
||
171 | **/ |
||
172 | public function render( $user_profile = null ) { |
||
186 | |||
187 | /** |
||
188 | * Set the user ID the container will operate with. |
||
189 | * |
||
190 | * @param int $user_id |
||
191 | **/ |
||
192 | public function set_user_id( $user_id ) { |
||
196 | |||
197 | /** |
||
198 | * Show the container only on users who have the $role role. |
||
199 | * |
||
200 | * @param string $role |
||
201 | * @return object $this |
||
202 | **/ |
||
203 | public function show_on_user_role( $role ) { |
||
208 | |||
209 | /** |
||
210 | * Show the container only for users who have either capabilities or roles setup |
||
211 | * |
||
212 | * @param array $show_for |
||
213 | * @return object $this |
||
214 | **/ |
||
215 | public function show_for( $show_for ) { |
||
220 | |||
221 | /** |
||
222 | * Validate and parse the show_for logic rules. |
||
223 | * |
||
224 | * @param array $rules |
||
225 | * @return array |
||
226 | */ |
||
227 | protected function parse_show_for( $show_for ) { |
||
263 | } |
||
264 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: