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 | |||
| 13 | protected static $registered_pages = array(); |
||
| 14 | |||
| 15 | public $settings = array( |
||
| 16 | 'parent' => 'self', |
||
| 17 | 'file' => '', |
||
| 18 | 'permissions' => 'manage_options', |
||
| 19 | ); |
||
| 20 | |||
| 21 | public $icon = ''; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Create a new container |
||
| 25 | * |
||
| 26 | * @param string $unique_id Unique id of the container |
||
| 27 | * @param string $title title of the container |
||
| 28 | * @param string $type Type of the container |
||
| 29 | **/ |
||
| 30 | View Code Duplication | public function __construct( $unique_id, $title, $type ) { |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Attach container as a theme options page/subpage. |
||
| 40 | **/ |
||
| 41 | public function init() { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Perform checks whether the current save() request is valid. |
||
| 61 | * |
||
| 62 | * @return bool |
||
| 63 | **/ |
||
| 64 | public function is_valid_save() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Perform save operation after successful is_valid_save() check. |
||
| 70 | * The call is propagated to all fields in the container. |
||
| 71 | * |
||
| 72 | * @param mixed $user_data |
||
| 73 | **/ |
||
| 74 | public function save( $user_data = null ) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Perform checks whether the container should be attached during the current request |
||
| 90 | * |
||
| 91 | * @return bool True if the container is allowed to be attached |
||
| 92 | **/ |
||
| 93 | public function is_valid_attach_for_request() { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Check container attachment rules against object id |
||
| 99 | * |
||
| 100 | * @param int $object_id |
||
| 101 | * @return bool |
||
| 102 | **/ |
||
| 103 | public function is_valid_attach_for_object( $object_id = null ) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Add theme options container pages. |
||
| 109 | * Hook the container saving action. |
||
| 110 | **/ |
||
| 111 | public function attach() { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Whether this container is currently viewed. |
||
| 141 | **/ |
||
| 142 | View Code Duplication | public function should_activate() { |
|
| 143 | $request_page = isset( $_GET['page'] ) ? $_GET['page'] : ''; |
||
|
1 ignored issue
–
show
|
|||
| 144 | if ( ! empty( $request_page ) && $request_page === $this->settings['file'] ) { |
||
| 145 | return true; |
||
| 146 | } |
||
| 147 | |||
| 148 | return false; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Output the container markup |
||
| 153 | **/ |
||
| 154 | public function render() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Make sure that there are no duplicate containers with the same name. |
||
| 165 | **/ |
||
| 166 | public function verify_unique_page() { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Unregister the container parent and child pages. |
||
| 192 | **/ |
||
| 193 | public function drop_unique_page() { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Sanitize the container title for use in |
||
| 218 | * the theme options file name. |
||
| 219 | **/ |
||
| 220 | protected function clear_string( $string ) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * COMMON USAGE METHODS |
||
| 226 | */ |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Change the parent theme options page of this container |
||
| 230 | **/ |
||
| 231 | public function set_page_parent( $parent ) { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Set the icon of this theme options page. |
||
| 242 | * Applicable only for parent theme option pages. |
||
| 243 | **/ |
||
| 244 | public function set_icon( $icon ) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Set the theme options file name of this container. |
||
| 251 | **/ |
||
| 252 | public function set_page_file( $file ) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Set the permissions necessary to view |
||
| 259 | * the corresponding theme options page |
||
| 260 | **/ |
||
| 261 | public function set_page_permissions( $permissions ) { |
||
| 265 | } |
||
| 266 | |||
| 267 |