Completed
Push — master ( bc6754...ed64c2 )
by
unknown
07:19
created

Config   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 232
Duplicated Lines 11.64 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 27
loc 232
rs 10
wmc 26
lcom 1
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A get_allowed_variables() 0 5 1
B get_vars() 0 36 5
B init() 0 17 5
A init_post_types() 12 12 3
B add_post_type() 0 24 2
A init_taxonomies() 12 12 3
B add_taxonomy() 0 25 2
A init_post_formats() 0 9 2
B init_sidebars() 0 24 3

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Classy;
4
5
/**
6
 * Theme Config
7
 *
8
 * Loads theme config and registers models based on it
9
 */
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() {
20
21
		return array( 'environment', 'textdomain', 'post_types', 'taxonomies', 'post_formats', 'sidebars' );
22
23
	}
24
25
	/**
26
	 * Requires config file variables
27
	 *
28
	 * @return array
29
	 */
30
	public static function get_vars() {
31
32
		if ( null === self::$vars ) {
33
34
			// Check for a theme config
35
			$config_file = CLASSY_THEME_FRAMEWORK_PATH . 'config.php';
36
37
			if ( file_exists( $config_file ) ) {
38
39
				require_once CLASSY_THEME_FRAMEWORK_PATH . 'config.php';
40
41
				$vars = self::get_allowed_variables();
42
43
				foreach ( $vars as $var ) {
44
45
					if ( isset( $$var ) ) {
46
47
						self::$vars[ $var ] = $$var;
48
49
						unset( $$var ); // We don't require it anymore
50
51
					}
52
				}
53
			} else {
54
55
				wp_die( sprintf(
56
					'There is no config file in %s custom/config.php',
57
					esc_html( CLASSY_THEME )
58
				) );
59
60
			}
61
		}
62
63
		return self::$vars;
64
65
	}
66
67
68
69
	/**
70
	 * Retrieves config variables and then init WordPress functionality based on them.
71
	 */
72
	public static function init() {
73
74
		$vars = self::get_vars();
75
76
		// Init Post Types
77
		if ( isset( $vars['post_types'] ) ) { self::init_post_types( $vars['post_types'] ); }
78
79
		// Init Taxonomies
80
		if ( isset( $vars['taxonomies'] ) ) { self::init_taxonomies( $vars['taxonomies'] ); }
81
82
		// Init Post Formats
83
		if ( isset( $vars['post_formats'] ) ) { self::init_post_formats( $vars['post_formats'] ); }
84
85
		// Init Sidebars
86
		if ( isset( $vars['sidebars'] ) ) { self::init_sidebars( $vars['sidebars'] ); }
87
88
	}
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 ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
98
99
		if ( is_array( $post_types ) ) {
100
101
			foreach ( $post_types as $type => $options ) {
102
103
				self::add_post_type( $type, $options['config'], $options['singular'], $options['multiple'] );
104
105
			}
106
		}
107
108
	}
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' ) {
120
121
		$domain = Classy::textdomain();
122
123
		if ( ! isset( $config['labels'] ) ) {
124
125
			$config['labels'] = array(
126
				'name' => __( $multiple, $domain ),
127
				'singular_name' => __( $singular, $domain ),
128
				'not_found' => __( 'No ' . $multiple . ' Found', $domain ),
129
				'not_found_in_trash' => __( 'No ' . $multiple . ' found in Trash', $domain ),
130
				'edit_item' => __( 'Edit ', $singular, $domain ),
131
				'search_items' => __( 'Search ' . $multiple, $domain ),
132
				'view_item' => __( 'View ', $singular, $domain ),
133
				'new_item' => __( 'New ' . $singular, $domain ),
134
				'add_new' => __( 'Add New', $domain ),
135
				'add_new_item' => __( 'Add New ' . $singular, $domain ),
136
			);
137
138
		}
139
140
		register_post_type( $name, $config );
141
142
	}
143
144
	/**
145
	 * Registers taxonomies
146
	 *
147
	 * @param  array $taxonomies
148
	 */
149 View Code Duplication
	private static function init_taxonomies( $taxonomies ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
150
151
		if ( is_array( $taxonomies ) ) {
152
153
			foreach ( $taxonomies as $type => $options ) {
154
155
				self::add_taxonomy( $type, $options['for'], $options['config'], $options['singular'], $options['multiple'] );
156
157
			}
158
		}
159
160
	}
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' ) {
172
173
		$domain = Classy::textdomain();
174
175
		if ( ! isset( $config['labels'] ) ) {
176
177
			$config['labels'] = array(
178
				'name' => __( $multiple, $domain ),
179
				'singular_name' => __( $singular, $domain ),
180
				'search_items' => __( 'Search ' . $multiple, $domain ),
181
				'all_items' => __( 'All ' . $multiple, $domain ),
182
				'parent_item' => __( 'Parent ' . $singular, $domain ),
183
				'parent_item_colon' => __( 'Parent ' . $singular . ':', $domain ),
184
				'edit_item' => __( 'Edit ' . $singular, $domain ),
185
				'update_item' => __( 'Update ' . $singular, $domain ),
186
				'add_new_item' => __( 'Add New ' . $singular, $domain ),
187
				'new_item_name' => __( 'New ' . $singular . ' Name', $domain ),
188
				'menu_name' => __( $singular, $domain ),
189
			);
190
191
		}
192
193
		register_taxonomy( $name, $object_type, $config );
194
195
	}
196
197
	/**
198
	 * Registers Post Formats
199
	 *
200
	 * @param  array $post_formats
201
	 */
202
	private static function init_post_formats( $post_formats ) {
203
204
		if ( is_array( $post_formats ) ) {
205
206
			add_theme_support( 'post-formats', $post_formats );
207
208
		}
209
210
	}
211
212
	/**
213
	 * Registers Sidebars
214
	 *
215
	 * @param  array $sidebars
216
	 */
217
	private static function init_sidebars( $sidebars ) {
218
219
		$domain = Classy::textdomain();
220
221
		if ( is_array( $sidebars ) ) {
222
223
			foreach ( $sidebars as $id => $title ) {
224
225
				register_sidebar(
226
					array(
227
						'id' => $id,
228
						'name' => __( $title, $domain ),
229
						'description' => __( $title, $domain ),
230
						'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="widget-inner">',
231
						'after_widget' => '</div></div>',
232
						'before_title' => '<h3>',
233
						'after_title' => '</h3>',
234
					)
235
				);
236
237
			}
238
		}
239
240
	}
241
}
242