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 |
||
| 11 | class Theme_Options_Container extends Container { |
||
| 12 | |||
| 13 | protected static $registered_pages = array(); |
||
| 14 | |||
| 15 | public $settings = array( |
||
| 16 | 'parent' => '', |
||
| 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() { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Perform checks whether the current save() request is valid. |
||
| 59 | * |
||
| 60 | * @return bool |
||
| 61 | **/ |
||
| 62 | public function is_valid_save() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Perform save operation after successful is_valid_save() check. |
||
| 68 | * The call is propagated to all fields in the container. |
||
| 69 | * |
||
| 70 | * @param mixed $user_data |
||
| 71 | **/ |
||
| 72 | public function save( $user_data = null ) { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Perform checks whether the container should be attached during the current request |
||
| 88 | * |
||
| 89 | * @return bool True if the container is allowed to be attached |
||
| 90 | **/ |
||
| 91 | public function is_valid_attach_for_request() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Check container attachment rules against object id |
||
| 97 | * |
||
| 98 | * @param int $object_id |
||
| 99 | * @return bool |
||
| 100 | **/ |
||
| 101 | public function is_valid_attach_for_object( $object_id = null ) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Add theme options container pages. |
||
| 107 | * Hook the container saving action. |
||
| 108 | **/ |
||
| 109 | public function attach() { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Whether this container is currently viewed. |
||
| 139 | **/ |
||
| 140 | View Code Duplication | public function should_activate() { |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Output the container markup |
||
| 151 | **/ |
||
| 152 | public function render() { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Make sure that there are no duplicate containers with the same name. |
||
| 163 | **/ |
||
| 164 | public function verify_unique_page() { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Sanitize the container title for use in |
||
| 192 | * the theme options file name. |
||
| 193 | **/ |
||
| 194 | protected function clear_string( $string ) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * COMMON USAGE METHODS |
||
| 200 | */ |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Change the parent theme options page of this container |
||
| 204 | **/ |
||
| 205 | public function set_page_parent( $parent ) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Set the icon of this theme options page. |
||
| 216 | * Applicable only for parent theme option pages. |
||
| 217 | **/ |
||
| 218 | public function set_icon( $icon ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Set the theme options file name of this container. |
||
| 225 | **/ |
||
| 226 | public function set_page_file( $file ) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Set the permissions necessary to view |
||
| 233 | * the corresponding theme options page |
||
| 234 | **/ |
||
| 235 | public function set_page_permissions( $permissions ) { |
||
| 239 | } |
||
| 240 | |||
| 241 |