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 |
||
15 | class Config { |
||
16 | |||
17 | /** |
||
18 | * Contains all vars from config file. |
||
19 | * |
||
20 | * @var null |
||
21 | */ |
||
22 | protected static $vars = null; |
||
23 | |||
24 | /** |
||
25 | * Returns list of allowed variables that can be used in theme config. |
||
26 | * |
||
27 | * @return array |
||
28 | */ |
||
29 | private static function get_allowed_variables() { |
||
39 | |||
40 | /** |
||
41 | * Requires config file variables |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | public static function get_vars() { |
||
71 | |||
72 | /** |
||
73 | * Retrieves config variables and then init WordPress functionality based on them. |
||
74 | */ |
||
75 | public static function init() { |
||
98 | |||
99 | /** |
||
100 | * Registers Post Types. |
||
101 | * |
||
102 | * @param array $post_types Custom post types to be registered. |
||
103 | */ |
||
104 | View Code Duplication | private static function init_post_types( $post_types ) { |
|
116 | |||
117 | /** |
||
118 | * Wrapper for register_post_type(). |
||
119 | * |
||
120 | * @param string $name Post type key, must not exceed 20 characters. |
||
121 | * @param array $config Better look into register_post_type() function. |
||
122 | * @param string $singular Optional. Default singular name. |
||
123 | * @param string $multiple Optional. Default multiple name. |
||
124 | */ |
||
125 | private static function add_post_type( $name, $config, $singular = 'Entry', $multiple = 'Entries' ) { |
||
145 | |||
146 | /** |
||
147 | * Registers taxonomies. |
||
148 | * |
||
149 | * @param array $taxonomies Taxonomies to be registered. |
||
150 | */ |
||
151 | View Code Duplication | private static function init_taxonomies( $taxonomies ) { |
|
164 | |||
165 | /** |
||
166 | * Wrapper for register_taxonomy(). |
||
167 | * |
||
168 | * @param string $name Taxonomy key, must not exceed 32 characters. |
||
169 | * @param mixed $object_type Name of the object type for the taxonomy object. |
||
170 | * @param array $config Better look into register_taxonomy() function. |
||
171 | * @param string $singular Optional. Default singular name. |
||
172 | * @param string $multiple Optional. Default multiple name. |
||
173 | */ |
||
174 | private static function add_taxonomy( $name, $object_type, $config, $singular = 'Entry', $multiple = 'Entries' ) { |
||
195 | |||
196 | /** |
||
197 | * Registers Post Formats. |
||
198 | * |
||
199 | * @param array $post_formats Array with available post formats. |
||
200 | */ |
||
201 | private static function init_post_formats( $post_formats ) { |
||
206 | |||
207 | /** |
||
208 | * Wrapper for register_sidebar(). |
||
209 | * |
||
210 | * @param array $sidebars Sidebars to be registered. |
||
211 | */ |
||
212 | private static function init_sidebars( $sidebars ) { |
||
231 | } |
||
232 |
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.