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 | ); |
||
| 19 | |||
| 20 | public $icon = ''; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Create a new container |
||
| 24 | * |
||
| 25 | * @param string $unique_id Unique id of the container |
||
| 26 | * @param string $title title of the container |
||
| 27 | * @param string $type Type of the container |
||
| 28 | **/ |
||
| 29 | View Code Duplication | public function __construct( $unique_id, $title, $type ) { |
|
| 36 | |||
| 37 | /** |
||
| 38 | * Attach container as a theme options page/subpage. |
||
| 39 | **/ |
||
| 40 | public function init() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Perform checks whether the current save() request is valid. |
||
| 58 | * |
||
| 59 | * @return bool |
||
| 60 | **/ |
||
| 61 | public function is_valid_save() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Perform save operation after successful is_valid_save() check. |
||
| 71 | * The call is propagated to all fields in the container. |
||
| 72 | * |
||
| 73 | * @param mixed $user_data |
||
| 74 | **/ |
||
| 75 | public function save( $user_data = null ) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get environment array for page request (in admin) |
||
| 91 | * |
||
| 92 | * @return array |
||
| 93 | **/ |
||
| 94 | protected function get_environment_for_request() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Perform checks whether the container should be attached during the current request |
||
| 100 | * |
||
| 101 | * @return bool True if the container is allowed to be attached |
||
| 102 | **/ |
||
| 103 | public function is_valid_attach_for_request() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get environment array for object id |
||
| 109 | * |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | protected function get_environment_for_object( $object_id ) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Check container attachment rules against object id |
||
| 118 | * |
||
| 119 | * @param int $object_id |
||
| 120 | * @return bool |
||
| 121 | **/ |
||
| 122 | public function is_valid_attach_for_object( $object_id = null ) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Add theme options container pages. |
||
| 128 | * Hook the container saving action. |
||
| 129 | **/ |
||
| 130 | public function attach() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Whether this container is currently viewed. |
||
| 162 | **/ |
||
| 163 | public function should_activate() { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Output the container markup |
||
| 174 | **/ |
||
| 175 | public function render() { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Make sure that there are no duplicate containers with the same name. |
||
| 186 | **/ |
||
| 187 | public function verify_unique_page() { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Sanitize the container title for use in |
||
| 215 | * the theme options file name. |
||
| 216 | **/ |
||
| 217 | protected function clear_string( $string ) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * COMMON USAGE METHODS |
||
| 223 | */ |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Change the parent theme options page of this container |
||
| 227 | **/ |
||
| 228 | public function set_page_parent( $parent ) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Set the icon of this theme options page. |
||
| 239 | * Applicable only for parent theme option pages. |
||
| 240 | **/ |
||
| 241 | public function set_icon( $icon ) { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Set the theme options file name of this container. |
||
| 248 | **/ |
||
| 249 | public function set_page_file( $file ) { |
||
| 253 | } |
||
| 254 | |||
| 255 |