Config   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 219
Duplicated Lines 12.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 6
Bugs 1 Features 3
Metric Value
dl 27
loc 219
rs 10
c 6
b 1
f 3
wmc 26
lcom 1
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A get_allowed_variables() 0 12 1
B get_vars() 0 26 5
B init() 0 23 5
A init_post_types() 12 12 3
A add_post_type() 0 20 2
A init_taxonomies() 13 13 3
A add_taxonomy() 0 21 2
A init_post_formats() 0 5 2
A init_sidebars() 0 19 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
 * Theme Config.
4
 *
5
 * Loads theme config and registers models based on it.
6
 *
7
 * @package Classy
8
 */
9
10
namespace Classy;
11
12
/**
13
 * Class Config.
14
 */
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() {
30
		return array(
31
			'environment',
32
			'textdomain',
33
			'minify_html',
34
			'post_types',
35
			'taxonomies',
36
			'post_formats',
37
			'sidebars',
38
			'include',
39
		);
40
	}
41
42
	/**
43
	 * Requires config file variables
44
	 *
45
	 * @return array
46
	 */
47
	public static function get_vars() {
48
		if ( is_null( self::$vars ) ) {
49
			// Check for a theme config.
50
			$config_file = CLASSY_THEME_FRAMEWORK_PATH . '/custom/config.php';
51
52
			if ( ! file_exists( $config_file ) ) {
53
				wp_die( sprintf(
54
					'There is no config file in %s custom/config.php',
55
					esc_html( CLASSY_THEME )
56
				) );
57
			}
58
59
			require_once( $config_file );
60
			$vars = self::get_allowed_variables();
61
62
			foreach ( $vars as $var ) {
63
				if ( isset( $$var ) ) {
64
					self::$vars[ $var ] = $$var;
65
66
					unset( $$var ); // We don't require it anymore.
67
				}
68
			}
69
		}
70
71
		return self::$vars;
72
	}
73
74
	/**
75
	 * Retrieves config variables and then init WordPress functionality based on them.
76
	 */
77
	public static function init() {
78
		$vars = self::get_vars();
79
80
		// Init Post Types.
81
		if ( isset( $vars['post_types'] ) ) {
82
			self::init_post_types( $vars['post_types'] );
83
		}
84
85
		// Init Taxonomies.
86
		if ( isset( $vars['taxonomies'] ) ) {
87
			self::init_taxonomies( $vars['taxonomies'] );
88
		}
89
90
		// Init Post Formats.
91
		if ( isset( $vars['post_formats'] ) ) {
92
			self::init_post_formats( $vars['post_formats'] );
93
		}
94
95
		// Init Sidebars.
96
		if ( isset( $vars['sidebars'] ) ) {
97
			self::init_sidebars( $vars['sidebars'] );
98
		}
99
	}
100
101
	/**
102
	 * Registers Post Types.
103
	 *
104
	 * @param array $post_types Custom post types to be registered.
105
	 */
106 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...
107
		if ( is_array( $post_types ) ) {
108
			foreach ( $post_types as $type => $options ) {
109
				self::add_post_type(
110
					$type,
111
					$options['config'],
112
					$options['singular'],
113
					$options['multiple']
114
				);
115
			}
116
		}
117
	}
118
119
	/**
120
	 * Wrapper for register_post_type().
121
	 *
122
	 * @param string $name     Post type key, must not exceed 20 characters.
123
	 * @param array  $config   Better look into register_post_type() function.
124
	 * @param string $singular Optional. Default singular name.
125
	 * @param string $multiple Optional. Default multiple name.
126
	 */
127
	private static function add_post_type( $name, $config, $singular = 'Entry', $multiple = 'Entries' ) {
128
		$domain = Classy::textdomain();
129
130
		if ( ! isset( $config['labels'] ) ) {
131
			$config['labels'] = array(
132
				'name' => __( $multiple, $domain ),
133
				'singular_name' => __( $singular, $domain ),
134
				'not_found' => __( 'No ' . $multiple . ' Found', $domain ),
135
				'not_found_in_trash' => __( 'No ' . $multiple . ' found in Trash', $domain ),
136
				'edit_item' => __( 'Edit ', $singular, $domain ),
137
				'search_items' => __( 'Search ' . $multiple, $domain ),
138
				'view_item' => __( 'View ', $singular, $domain ),
139
				'new_item' => __( 'New ' . $singular, $domain ),
140
				'add_new' => __( 'Add New', $domain ),
141
				'add_new_item' => __( 'Add New ' . $singular, $domain ),
142
			);
143
		}
144
145
		register_post_type( $name, $config );
146
	}
147
148
	/**
149
	 * Registers taxonomies.
150
	 *
151
	 * @param array $taxonomies Taxonomies to be registered.
152
	 */
153 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...
154
		if ( is_array( $taxonomies ) ) {
155
			foreach ( $taxonomies as $type => $options ) {
156
				self::add_taxonomy(
157
					$type,
158
					$options['for'],
159
					$options['config'],
160
					$options['singular'],
161
					$options['multiple']
162
				);
163
			}
164
		}
165
	}
166
167
	/**
168
	 * Wrapper for register_taxonomy().
169
	 *
170
	 * @param string $name 		  Taxonomy key, must not exceed 32 characters.
171
	 * @param mixed  $object_type Name of the object type for the taxonomy object.
172
	 * @param array  $config	  Better look into register_taxonomy() function.
173
	 * @param string $singular    Optional. Default singular name.
174
	 * @param string $multiple 	  Optional. Default multiple name.
175
	 */
176
	private static function add_taxonomy( $name, $object_type, $config, $singular = 'Entry', $multiple = 'Entries' ) {
177
		$domain = Classy::textdomain();
178
179
		if ( ! isset( $config['labels'] ) ) {
180
			$config['labels'] = array(
181
				'name' => __( $multiple, $domain ),
182
				'singular_name' => __( $singular, $domain ),
183
				'search_items' => __( 'Search ' . $multiple, $domain ),
184
				'all_items' => __( 'All ' . $multiple, $domain ),
185
				'parent_item' => __( 'Parent ' . $singular, $domain ),
186
				'parent_item_colon' => __( 'Parent ' . $singular . ':', $domain ),
187
				'edit_item' => __( 'Edit ' . $singular, $domain ),
188
				'update_item' => __( 'Update ' . $singular, $domain ),
189
				'add_new_item' => __( 'Add New ' . $singular, $domain ),
190
				'new_item_name' => __( 'New ' . $singular . ' Name', $domain ),
191
				'menu_name' => __( $singular, $domain ),
192
			);
193
		}
194
195
		register_taxonomy( $name, $object_type, $config );
196
	}
197
198
	/**
199
	 * Registers Post Formats.
200
	 *
201
	 * @param array $post_formats Array with available post formats.
202
	 */
203
	private static function init_post_formats( $post_formats ) {
204
		if ( is_array( $post_formats ) ) {
205
			add_theme_support( 'post-formats', $post_formats );
206
		}
207
	}
208
209
	/**
210
	 * Wrapper for register_sidebar().
211
	 *
212
	 * @param array $sidebars Sidebars to be registered.
213
	 */
214
	private static function init_sidebars( $sidebars ) {
215
		$domain = Classy::textdomain();
216
217
		if ( is_array( $sidebars ) ) {
218
			foreach ( $sidebars as $id => $title ) {
219
				register_sidebar(
220
					array(
221
						'id' => $id,
222
						'name' => __( $title, $domain ),
223
						'description' => __( $title, $domain ),
224
						'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="widget-inner">',
225
						'after_widget' => '</div></div>',
226
						'before_title' => '<h3>',
227
						'after_title' => '</h3>',
228
					)
229
				);
230
			}
231
		}
232
	}
233
}
234