Objects   B
last analyzed

Complexity

Total Complexity 52

Size/Duplication

Total Lines 308
Duplicated Lines 10.06 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 31
loc 308
rs 7.9487
c 0
b 0
f 0
wmc 52
lcom 1
cbo 1

11 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 41 2
A get_feed_types() 0 5 1
A get_calendar_types() 0 5 1
A get_admin_pages() 0 3 1
B get_calendar_view() 0 15 6
A get_field() 0 8 4
A get_admin_page() 0 3 2
A get_object() 0 21 4
B make_class_name() 0 20 6
C get_calendar() 13 29 12
C get_feed() 18 39 13

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Objects often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Objects, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Objects Factory
4
 *
5
 * @package SimpleCalendar
6
 */
7
namespace SimpleCalendar;
8
9
use SimpleCalendar\Abstracts as Object;
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * Objects factory.
17
 *
18
 * Helper class to get the right type of object used across the plugin.
19
 *
20
 * @since 3.0.0
21
 */
22
class Objects {
23
24
	/**
25
	 * Constructor.
26
	 *
27
	 * Add default objects.
28
	 *
29
	 * @since 3.0.0
30
	 */
31
	public function __construct() {
32
33
		// Add default feed type.
34
		add_filter( 'simcal_get_feed_types', function( $feed_types ) {
35
			return array_merge( $feed_types, array(
36
				'google',
37
				'grouped-calendars',
38
			) );
39
		}, 10, 1 );
40
41
		// Add default calendar type.
42
		add_filter( 'simcal_get_calendar_types', function( $calendar_types ) {
43
			return array_merge( $calendar_types, array(
44
				'default-calendar' => array(
45
					'grid',
46
					'list',
47
				),
48
			) );
49
		}, 10, 1 );
50
51
		// Add default admin objects.
52
		if ( $is_admin = is_admin() ) {
53
			add_filter( 'simcal_get_admin_pages', function( $admin_pages ) {
54
				return array_merge( $admin_pages, array(
55
					'add-ons' => array(
56
						'add-ons',
57
					),
58
					'settings' => array(
59
						'feeds',
60
						'calendars',
61
						'advanced',
62
					),
63
					'tools' => array(
64
						'system-status',
65
					),
66
				) );
67
			}, 10, 1 );
68
		}
69
70
		do_action( 'simcal_load_objects', $is_admin );
71
	}
72
73
	/**
74
	 * Get feed types.
75
	 *
76
	 * @since  3.0.0
77
	 *
78
	 * @return array
79
	 */
80
	public function get_feed_types() {
81
		$array = apply_filters( 'simcal_get_feed_types', array() );
82
		ksort( $array );
83
		return $array;
84
	}
85
86
	/**
87
	 * Get calendar types.
88
	 *
89
	 * @since  3.0.0
90
	 *
91
	 * @return array
92
	 */
93
	public function get_calendar_types() {
94
		$array = apply_filters( 'simcal_get_calendar_types', array() );
95
		ksort( $array );
96
		return $array;
97
	}
98
99
	/**
100
	 * Get admin pages.
101
	 *
102
	 * @since  3.0.0
103
	 *
104
	 * @return array
105
	 */
106
	public function get_admin_pages() {
107
		return apply_filters( 'simcal_get_admin_pages', array() );
108
	}
109
110
	/**
111
	 * Get a calendar.
112
	 *
113
	 * Returns the right type of calendar.
114
	 *
115
	 * @since  3.0.0
116
	 *
117
	 * @param  int|string|object|\WP_Post|Object\Calendar $object
118
	 *
119
	 * @return null|Object\Calendar
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
120
	 */
121
	public function get_calendar( $object ) {
122
123 View Code Duplication
		if ( is_string( $object ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
124
			return ! empty( $object ) ? $this->get_object( $object, 'calendar', '' ) : null;
125
		}
126
127
		if ( is_object( $object ) ) {
128
			if ( $object instanceof Object\Calendar ) {
129
				return $this->get_object( $object->type, 'feed', $object );
130 View Code Duplication
			} elseif ( $object instanceof \WP_Post ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
131
				if ( $type = wp_get_object_terms( $object->ID, 'calendar_type' ) ) {
132
					$name = sanitize_title( current( $type )->name );
133
					return $this->get_object( $name, 'calendar', $object );
134
				}
135
			} elseif ( isset( $object->type ) && isset( $object->id ) ) {
136
				return $this->get_object( $object->type, 'calendar', $object->id );
137
			}
138
		}
139
140
		if ( is_int( $object ) ) {
141
			$post = get_post( $object );
142 View Code Duplication
			if ( $post && ( $type = wp_get_object_terms( $post->ID, 'calendar_type' ) ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
143
				$name = sanitize_title( current( $type )->name );
144
				return $this->get_object( $name, 'calendar', $post );
145
			}
146
		}
147
148
		return null;
149
	}
150
151
	/**
152
	 * Get a calendar view.
153
	 *
154
	 * @since  3.0.0
155
	 *
156
	 * @param  int    $id   Feed post id.
157
	 * @param  string $name (optional) Name of calendar view.
158
	 *
159
	 * @return null|Object\Calendar_View
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
160
	 */
161
	public function get_calendar_view( $id = 0, $name = '' ) {
162
163
		if ( ! $name && $id > 0 ) {
164
165
			$calendar_view = get_post_meta( $id, '_calendar_view', true );
166
167
			if ( $terms = wp_get_object_terms( $id, 'calendar_type' ) ) {
168
				$calendar_type = sanitize_title( current( $terms )->name );
169
				$name = isset( $calendar_view[ $calendar_type ] ) ? $calendar_type . '-' . $calendar_view[ $calendar_type ] : '';
170
			}
171
172
		}
173
174
		return $name ? $this->get_object( $name, 'calendar-view', '' ) : null;
175
	}
176
177
	/**
178
	 * Get a feed.
179
	 *
180
	 * Returns the right type of feed.
181
	 *
182
	 * @since  3.0.0
183
	 *
184
	 * @param  int|string|object|\WP_Post|Object\Calendar $object
185
	 *
186
	 * @return null|Object\Feed
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
187
	 */
188
	public function get_feed( $object ) {
189
190 View Code Duplication
		if ( is_string( $object ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
191
			return ! empty( $object ) ? $this->get_object( $object, 'feed', '' ) : null;
192
		}
193
194
		if ( is_object( $object ) ) {
195
			if ( $object instanceof Object\Calendar ) {
196
				$feed_name = '';
197
				if ( empty( $object->feed ) ) {
198
					if ( $feed_type = wp_get_object_terms( $object->id, 'feed_type' ) ) {
199
						$feed_name = sanitize_title( current( $feed_type )->name );
200
					}
201
				} else {
202
					$feed_name = $object->feed;
203
				}
204
				return $this->get_object( $feed_name, 'feed', $object );
205 View Code Duplication
			} elseif ( $object instanceof \WP_Post ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
206
				$calendar = $this->get_calendar( $object );
207
208
				if ( isset( $calendar->feed ) ) {
209
					return $this->get_object( $calendar->feed, 'feed', $calendar );
210
				} else {
211
					return null;
212
				}
213
214
215
			} elseif ( isset( $object->feed ) && isset( $object->id ) ) {
216
				return $this->get_object( $object->feed, 'feed', $object );
217
			}
218
		}
219
220 View Code Duplication
		if ( is_int( $object ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
221
			$calendar = $this->get_calendar( $object );
222
			return isset( $calendar->feed ) ? $this->get_object( $calendar->feed, 'feed', $calendar ) : null;
223
		}
224
225
		return null;
226
	}
227
228
	/**
229
	 * Get a field.
230
	 *
231
	 * @since  3.0.0
232
	 *
233
	 * @param  array  $args Field args.
234
	 * @param  string $name Field type.
235
	 *
236
	 * @return null|Object\Field
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
237
	 */
238
	public function get_field( $args, $name = '' ) {
239
240
		if ( empty( $name ) ) {
241
			$name = isset( $args['type'] ) ? $args['type'] : false;
242
		}
243
244
		return $name ? $this->get_object( $name, 'field', $args ) : null;
245
	}
246
247
	/**
248
	 * Get a settings page.
249
	 *
250
	 * @since  3.0.0
251
	 *
252
	 * @param  string $name
253
	 *
254
	 * @return null|Object\Admin_Page
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
255
	 */
256
	public function get_admin_page( $name ) {
257
		return $name ? $this->get_object( $name, 'admin-page' ) : null;
258
	}
259
260
	/**
261
	 * Get a plugin object.
262
	 *
263
	 * @since  3.0.0
264
	 * @access private
265
	 *
266
	 * @param  string $name Object name.
267
	 * @param  string $type Object type.
268
	 * @param  mixed  $args (optional) arguments for the class constructor.
269
	 *
270
	 * @return null|Object
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
271
	 */
272
	private function get_object( $name, $type, $args = '' ) {
273
274
		$types = array(
275
			'admin-page',
276
			'calendar',
277
			'calendar-view',
278
			'feed',
279
			'field',
280
		);
281
282
		if ( in_array( $type, $types ) ) {
283
284
			$class_name = $this->make_class_name( $name, $type );
285
			$parent     = '\\' . __NAMESPACE__ . '\Abstracts\\' . implode( '_', array_map( 'ucfirst', explode( '-', $type ) ) );
286
			$class      = class_exists( $class_name ) ? new $class_name( $args ) : false;
287
288
			return $class instanceof $parent ? $class : null;
289
		}
290
291
		return null;
292
	}
293
294
	/**
295
	 * Make class name from slug.
296
	 *
297
	 * Standardizes object naming and class names: <object-name> becomes <Class_Name>.
298
	 * The plugin autoloader uses a similar pattern.
299
	 *
300
	 * @since  3.0.0
301
	 * @access private
302
	 *
303
	 * @param  string $name Object name.
304
	 * @param  string $type Object type.
305
	 *
306
	 * @return string The class name complete with its full namespace.
307
	 */
308
	private function make_class_name( $name, $type ) {
309
310
		if ( 'calendar' == $type ) {
311
			$namespace = '\\' . __NAMESPACE__ . '\Calendars\\';
312
		} elseif ( 'calendar-view' == $type ) {
313
			$namespace = '\\' . __NAMESPACE__ . '\Calendars\Views\\';
314
		} elseif ( 'feed' == $type ) {
315
			$namespace = '\\' . __NAMESPACE__ . '\Feeds\\';
316
		} elseif ( 'field' == $type ) {
317
			$namespace = '\\' . __NAMESPACE__ . '\Admin\Fields\\';
318
		} elseif ( 'admin-page' == $type ) {
319
			$namespace = '\\' . __NAMESPACE__ . '\Admin\Pages\\';
320
		} else {
321
			return '';
322
		}
323
324
		$class_name = implode( '_', array_map( 'ucfirst', explode( '-', $name ) ) );
325
326
		return $namespace . $class_name;
327
	}
328
329
}
330