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 | /** |
||
14 | * Array of registered page slugs to verify uniqueness with |
||
15 | * |
||
16 | * @var array |
||
17 | */ |
||
18 | protected static $registered_pages = array(); |
||
19 | |||
20 | /** |
||
21 | * Array of container settings |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | public $settings = array( |
||
26 | 'parent' => '', |
||
27 | 'file' => '', |
||
28 | 'icon' => '', |
||
29 | 'menu_position' => null, |
||
30 | 'menu_title' => null, |
||
31 | ); |
||
32 | |||
33 | /** |
||
34 | * {@inheritDoc} |
||
35 | */ |
||
36 | View Code Duplication | public function __construct( $id, $title, $type, $condition_collection, $condition_translator ) { |
|
47 | |||
48 | /** |
||
49 | * Sanitize a title to a filename |
||
50 | * |
||
51 | * @param string $title |
||
52 | * @param string $extension |
||
53 | * @return string |
||
54 | */ |
||
55 | protected function title_to_filename( $title, $extension ) { |
||
70 | |||
71 | /** |
||
72 | * Attach container as a theme options page/subpage. |
||
73 | */ |
||
74 | public function init() { |
||
84 | |||
85 | /** |
||
86 | * Checks whether the current save request is valid |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | public function is_valid_save() { |
||
97 | |||
98 | /** |
||
99 | * Perform save operation after successful is_valid_save() check. |
||
100 | * The call is propagated to all fields in the container. |
||
101 | * |
||
102 | * @param mixed $user_data |
||
103 | */ |
||
104 | public function save( $user_data = null ) { |
||
117 | |||
118 | /** |
||
119 | * Get environment array for page request (in admin) |
||
120 | * |
||
121 | * @return array |
||
122 | */ |
||
123 | protected function get_environment_for_request() { |
||
126 | |||
127 | /** |
||
128 | * Perform checks whether the container should be attached during the current request |
||
129 | * |
||
130 | * @return bool True if the container is allowed to be attached |
||
131 | */ |
||
132 | public function is_valid_attach_for_request() { |
||
135 | |||
136 | /** |
||
137 | * Get environment array for object id |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | protected function get_environment_for_object( $object_id ) { |
||
144 | |||
145 | /** |
||
146 | * Check container attachment rules against object id |
||
147 | * |
||
148 | * @param int $object_id |
||
149 | * @return bool |
||
150 | */ |
||
151 | public function is_valid_attach_for_object( $object_id = null ) { |
||
154 | |||
155 | /** |
||
156 | * Add theme options container pages. |
||
157 | * Hook the container saving action. |
||
158 | */ |
||
159 | public function attach() { |
||
188 | |||
189 | /** |
||
190 | * Whether this container is currently viewed. |
||
191 | * |
||
192 | * @return boolean |
||
193 | */ |
||
194 | public function should_activate() { |
||
203 | |||
204 | /** |
||
205 | * Output the container markup |
||
206 | */ |
||
207 | public function render() { |
||
216 | |||
217 | /** |
||
218 | * Register the page while making sure it is unique. |
||
219 | * |
||
220 | * @return boolean |
||
221 | */ |
||
222 | protected function register_page() { |
||
250 | |||
251 | /** |
||
252 | * Change the parent theme options page of this container |
||
253 | * |
||
254 | * @param string|Theme_Options_Container $parent |
||
255 | * @return Container $this |
||
256 | */ |
||
257 | public function set_page_parent( $parent ) { |
||
266 | |||
267 | /** |
||
268 | * Set the theme options file name of this container. |
||
269 | * |
||
270 | * @param string $file |
||
271 | * @return Container $this |
||
272 | */ |
||
273 | public function set_page_file( $file ) { |
||
277 | |||
278 | /** |
||
279 | * Set the title of this container in the administration menu. |
||
280 | * |
||
281 | * @param string $title |
||
282 | * @return Container $this |
||
283 | */ |
||
284 | public function set_page_menu_title( $title ) { |
||
288 | |||
289 | /** |
||
290 | * Alias of the set_page_menu_position() method for backwards compatibility |
||
291 | * |
||
292 | * @param integer $position |
||
293 | * @return Container $this |
||
294 | */ |
||
295 | public function set_page_position( $position ) { |
||
298 | |||
299 | /** |
||
300 | * Set the page position of this container in the administration menu. |
||
301 | * |
||
302 | * @param integer $position |
||
303 | * @return Container $this |
||
304 | */ |
||
305 | public function set_page_menu_position( $position ) { |
||
309 | |||
310 | /** |
||
311 | * Set the icon of this theme options page. |
||
312 | * Applicable only for parent theme option pages. |
||
313 | * |
||
314 | * @param string $icon |
||
315 | * @return Container $this |
||
316 | */ |
||
317 | public function set_icon( $icon ) { |
||
321 | } |
||
322 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: