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 user meta container |
||
23 | * |
||
24 | * @param string $title Unique title of the container |
||
25 | **/ |
||
26 | public function __construct( $title ) { |
||
33 | |||
34 | /** |
||
35 | * Bind attach() and save() to the appropriate WordPress actions. |
||
36 | **/ |
||
37 | public function init() { |
||
42 | |||
43 | /** |
||
44 | * Perform save operation after successful is_valid_save() check. |
||
45 | * The call is propagated to all fields in the container. |
||
46 | * |
||
47 | * @param int $user_id ID of the user against which save() is ran |
||
48 | **/ |
||
49 | public function save( $user_id ) { |
||
62 | |||
63 | /** |
||
64 | * Checks whether the current request is valid |
||
65 | * |
||
66 | * @return bool |
||
67 | **/ |
||
68 | public function is_valid_save( $user_id = 0 ) { |
||
77 | |||
78 | /** |
||
79 | * Perform checks whether the current save() request is valid |
||
80 | * |
||
81 | * @param int $user_id ID of the user against which save() is ran |
||
82 | * @return bool |
||
83 | **/ |
||
84 | public function is_valid_save_conditions( $user_id ) { |
||
107 | |||
108 | /** |
||
109 | * Show the container only on users who have the $role role. |
||
110 | * |
||
111 | * @param string $role |
||
112 | * @return object $this |
||
113 | **/ |
||
114 | public function show_on_user_role( $role ) { |
||
119 | |||
120 | /** |
||
121 | * Show the container only for users who have either capabilities or roles setup |
||
122 | * |
||
123 | * @param array $show_for |
||
124 | * @return object $this |
||
125 | **/ |
||
126 | public function show_for( $show_for ) { |
||
131 | |||
132 | /** |
||
133 | * Validate and parse the show_for logic rules. |
||
134 | * |
||
135 | * @param array $rules |
||
136 | * @return array |
||
137 | */ |
||
138 | protected function parse_show_for( $show_for ) { |
||
174 | |||
175 | /** |
||
176 | * Add the container to the user |
||
177 | **/ |
||
178 | public function attach() { |
||
183 | |||
184 | /** |
||
185 | * Whether we're on the user profile page |
||
186 | **/ |
||
187 | public function is_profile_page() { |
||
192 | |||
193 | /** |
||
194 | * Perform checks whether the container should be attached during the current request |
||
195 | * |
||
196 | * @return bool True if the container is allowed to be attached |
||
197 | **/ |
||
198 | public function is_valid_attach() { |
||
205 | |||
206 | /** |
||
207 | * Perform checks whether the container should be seen for the currently logged in user |
||
208 | * |
||
209 | * @return bool True if the current user is allowed to see the container |
||
210 | **/ |
||
211 | public function is_valid_show_for() { |
||
232 | |||
233 | /** |
||
234 | * Output the container markup |
||
235 | **/ |
||
236 | public function render( $user_profile = null ) { |
||
250 | |||
251 | /** |
||
252 | * Set the user ID the container will operate with. |
||
253 | * |
||
254 | * @param int $user_id |
||
255 | **/ |
||
256 | public function set_user_id( $user_id ) { |
||
260 | } |
||
261 |
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: