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 |
||
10 | class Config { |
||
11 | |||
12 | protected static $vars = null; |
||
13 | |||
14 | /** |
||
15 | * Returns list of allowed variables that can be used in theme config |
||
16 | * |
||
17 | * @return array |
||
18 | */ |
||
19 | private static function get_allowed_variables() { |
||
24 | |||
25 | /** |
||
26 | * Requires config file variables |
||
27 | * |
||
28 | * @return array |
||
29 | */ |
||
30 | public static function get_vars() { |
||
66 | |||
67 | |||
68 | |||
69 | /** |
||
70 | * Retrieves config variables and then init WordPress functionality based on them. |
||
71 | */ |
||
72 | public static function init() { |
||
89 | |||
90 | |||
91 | |||
92 | /** |
||
93 | * Registers Post Types |
||
94 | * |
||
95 | * @param array $post_types |
||
96 | */ |
||
97 | View Code Duplication | private static function init_post_types( $post_types ) { |
|
109 | |||
110 | |||
111 | /** |
||
112 | * Register Post Type Wrapper |
||
113 | * |
||
114 | * @param string $name |
||
115 | * @param array $config |
||
116 | * @param string $singular |
||
117 | * @param string $multiple |
||
118 | */ |
||
119 | private static function add_post_type( $name, $config, $singular = 'Entry', $multiple = 'Entries' ) { |
||
143 | |||
144 | /** |
||
145 | * Registers taxonomies |
||
146 | * |
||
147 | * @param array $taxonomies |
||
148 | */ |
||
149 | View Code Duplication | private static function init_taxonomies( $taxonomies ) { |
|
161 | |||
162 | /** |
||
163 | * Register taxonomy wrapper |
||
164 | * |
||
165 | * @param string $name |
||
166 | * @param mixed $object_type |
||
167 | * @param array $config |
||
168 | * @param string $singular |
||
169 | * @param string $multiple |
||
170 | */ |
||
171 | private static function add_taxonomy( $name, $object_type, $config, $singular = 'Entry', $multiple = 'Entries' ) { |
||
196 | |||
197 | /** |
||
198 | * Registers Post Formats |
||
199 | * |
||
200 | * @param array $post_formats |
||
201 | */ |
||
202 | private static function init_post_formats( $post_formats ) { |
||
211 | |||
212 | /** |
||
213 | * Registers Sidebars |
||
214 | * |
||
215 | * @param array $sidebars |
||
216 | */ |
||
217 | private static function init_sidebars( $sidebars ) { |
||
241 | } |
||
242 |
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.