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 ) { |
||
28 | parent::__construct( $title ); |
||
29 | |||
30 | if ( ! $this->get_datastore() ) { |
||
31 | $this->set_datastore( new Theme_Options_Datastore() ); |
||
32 | } |
||
33 | } |
||
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 | * Attach container as a theme options page/subpage. |
||
57 | **/ |
||
58 | public function init() { |
||
75 | |||
76 | /** |
||
77 | * Perform checks whether the current save() request is valid. |
||
78 | * |
||
79 | * @return bool |
||
80 | **/ |
||
81 | View Code Duplication | public function is_valid_save() { |
|
88 | |||
89 | /** |
||
90 | * Add theme options container pages. |
||
91 | * Hook the container saving action. |
||
92 | **/ |
||
93 | public function attach() { |
||
120 | |||
121 | /** |
||
122 | * Whether this container is currently viewed. |
||
123 | **/ |
||
124 | public function is_active() { |
||
131 | |||
132 | /** |
||
133 | * Revert the result of attach() |
||
134 | **/ |
||
135 | public function detach() { |
||
143 | |||
144 | /** |
||
145 | * Output the container markup |
||
146 | **/ |
||
147 | public function render() { |
||
154 | |||
155 | /** |
||
156 | * Make sure that there are no duplicate containers with the same name. |
||
157 | **/ |
||
158 | public function verify_unique_page() { |
||
181 | |||
182 | /** |
||
183 | * Unregister the container parent and child pages. |
||
184 | **/ |
||
185 | public function drop_unique_page() { |
||
207 | |||
208 | /** |
||
209 | * Make sure a field certain name can't be registered multiple times. |
||
210 | **/ |
||
211 | public function verify_unique_field_name( $name ) { |
||
224 | |||
225 | /** |
||
226 | * Remove field name $name from the list of unique field names |
||
227 | * |
||
228 | * @param string $name |
||
229 | **/ |
||
230 | public function drop_unique_field_name( $name ) { |
||
238 | |||
239 | /** |
||
240 | * Append array of fields to the current fields set. All items of the array |
||
241 | * must be instances of Field and their names should be unique for all |
||
242 | * Carbon containers. |
||
243 | * If a field does not have DataStore already, the container data store is |
||
244 | * assigned to them instead. |
||
245 | * |
||
246 | * @param array $fields |
||
247 | **/ |
||
248 | public function add_fields( $fields ) { |
||
257 | |||
258 | /** |
||
259 | * Change the parent theme options page of this container |
||
260 | **/ |
||
261 | public function set_page_parent( $parent ) { |
||
269 | |||
270 | /** |
||
271 | * Set the icon of this theme options page. |
||
272 | * Applicable only for parent theme option pages. |
||
273 | **/ |
||
274 | public function set_icon( $icon ) { |
||
278 | |||
279 | /** |
||
280 | * Set the theme options file name of this container. |
||
281 | **/ |
||
282 | public function set_page_file( $file ) { |
||
286 | |||
287 | /** |
||
288 | * Set the permissions necessary to view |
||
289 | * the corresponding theme options page |
||
290 | **/ |
||
291 | public function set_page_permissions( $permissions ) { |
||
295 | |||
296 | /** |
||
297 | * Sanitize the container title for use in |
||
298 | * the theme options file name. |
||
299 | **/ |
||
300 | protected function clear_string( $string ) { |
||
303 | |||
304 | } |
||
305 | |||
306 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.