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:
Complex classes like Theme_Options_Container often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Theme_Options_Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Theme_Options_Container extends Container { |
||
12 | static protected $registered_pages = array(); |
||
13 | |||
14 | public $settings = array( |
||
15 | 'parent' => 'self', |
||
16 | 'file' => '', |
||
17 | 'permissions' => 'manage_options', |
||
18 | ); |
||
19 | |||
20 | public $icon = ''; |
||
21 | |||
22 | /** |
||
23 | * Create a new theme options fields container |
||
24 | * |
||
25 | * @param string $title Unique title of the container |
||
26 | **/ |
||
27 | public function __construct( $title ) { |
||
34 | |||
35 | /** |
||
36 | * Perform save operation after successful is_valid_save() check. |
||
37 | * The call is propagated to all fields in the container. |
||
38 | * |
||
39 | * @param mixed $user_data |
||
40 | **/ |
||
41 | public function save( $user_data = null ) { |
||
54 | |||
55 | |||
56 | /** |
||
57 | * Sanitize a title to a filename |
||
58 | * |
||
59 | * @param string $title |
||
60 | * @return string |
||
61 | */ |
||
62 | protected function title_to_filename( $title, $extension ) { |
||
77 | |||
78 | /** |
||
79 | * Attach container as a theme options page/subpage. |
||
80 | **/ |
||
81 | public function init() { |
||
96 | |||
97 | /** |
||
98 | * Perform checks whether the current save() request is valid. |
||
99 | * |
||
100 | * @return bool |
||
101 | **/ |
||
102 | View Code Duplication | public function is_valid_save() { |
|
109 | |||
110 | /** |
||
111 | * Add theme options container pages. |
||
112 | * Hook the container saving action. |
||
113 | **/ |
||
114 | public function attach() { |
||
141 | |||
142 | /** |
||
143 | * Whether this container is currently viewed. |
||
144 | **/ |
||
145 | public function is_active() { |
||
152 | |||
153 | /** |
||
154 | * Revert the result of attach() |
||
155 | **/ |
||
156 | public function detach() { |
||
164 | |||
165 | /** |
||
166 | * Output the container markup |
||
167 | **/ |
||
168 | public function render() { |
||
175 | |||
176 | /** |
||
177 | * Make sure that there are no duplicate containers with the same name. |
||
178 | **/ |
||
179 | public function verify_unique_page() { |
||
202 | |||
203 | /** |
||
204 | * Unregister the container parent and child pages. |
||
205 | **/ |
||
206 | public function drop_unique_page() { |
||
228 | |||
229 | /** |
||
230 | * Append array of fields to the current fields set. All items of the array |
||
231 | * must be instances of Field and their names should be unique for all |
||
232 | * Carbon containers. |
||
233 | * If a field does not have DataStore already, the container datastore is |
||
234 | * assigned to them instead. |
||
235 | * |
||
236 | * @param array $fields |
||
237 | **/ |
||
238 | public function add_fields( $fields ) { |
||
247 | |||
248 | /** |
||
249 | * Change the parent theme options page of this container |
||
250 | **/ |
||
251 | public function set_page_parent( $parent ) { |
||
259 | |||
260 | /** |
||
261 | * Set the icon of this theme options page. |
||
262 | * Applicable only for parent theme option pages. |
||
263 | **/ |
||
264 | public function set_icon( $icon ) { |
||
268 | |||
269 | /** |
||
270 | * Set the theme options file name of this container. |
||
271 | **/ |
||
272 | public function set_page_file( $file ) { |
||
276 | |||
277 | /** |
||
278 | * Set the permissions necessary to view |
||
279 | * the corresponding theme options page |
||
280 | **/ |
||
281 | public function set_page_permissions( $permissions ) { |
||
285 | |||
286 | } |
||
287 |
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: