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 | View Code Duplication | public function is_valid_attach_for_request() { |
|
112 | |||
113 | /** |
||
114 | * Get environment array for object id |
||
115 | * |
||
116 | * @return array |
||
117 | */ |
||
118 | protected function get_environment_for_object( $object_id ) { |
||
121 | |||
122 | /** |
||
123 | * Check container attachment rules against object id |
||
124 | * |
||
125 | * @param int $object_id |
||
126 | * @return bool |
||
127 | **/ |
||
128 | View Code Duplication | public function is_valid_attach_for_object( $object_id = null ) { |
|
135 | |||
136 | /** |
||
137 | * Add theme options container pages. |
||
138 | * Hook the container saving action. |
||
139 | **/ |
||
140 | public function attach() { |
||
169 | |||
170 | /** |
||
171 | * Whether this container is currently viewed. |
||
172 | **/ |
||
173 | public function should_activate() { |
||
181 | |||
182 | /** |
||
183 | * Output the container markup |
||
184 | **/ |
||
185 | public function render() { |
||
193 | |||
194 | /** |
||
195 | * Make sure that there are no duplicate containers with the same name. |
||
196 | **/ |
||
197 | public function verify_unique_page() { |
||
222 | |||
223 | /** |
||
224 | * Sanitize the container title for use in |
||
225 | * the theme options file name. |
||
226 | **/ |
||
227 | protected function clear_string( $string ) { |
||
230 | |||
231 | /** |
||
232 | * COMMON USAGE METHODS |
||
233 | */ |
||
234 | |||
235 | /** |
||
236 | * Change the parent theme options page of this container |
||
237 | **/ |
||
238 | public function set_page_parent( $parent ) { |
||
246 | |||
247 | /** |
||
248 | * Set the icon of this theme options page. |
||
249 | * Applicable only for parent theme option pages. |
||
250 | **/ |
||
251 | public function set_icon( $icon ) { |
||
255 | |||
256 | /** |
||
257 | * Set the theme options file name of this container. |
||
258 | **/ |
||
259 | public function set_page_file( $file ) { |
||
263 | } |
||
264 | |||
265 |
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.