Calendars   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 145
Duplicated Lines 27.59 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 40
loc 145
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 27 27 6
B add_fields() 13 59 6
B add_sections() 0 26 4

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
 * Calendar Settings Page
4
 *
5
 * @package SimpleCalendar/Admin
6
 */
7
namespace SimpleCalendar\Admin\Pages;
8
9
use SimpleCalendar\Abstracts\Calendar;
10
use SimpleCalendar\Abstracts\Admin_Page;
11
12
if ( ! defined( 'ABSPATH' ) ) {
13
	exit;
14
}
15
16
/**
17
 * Calendar settings.
18
 *
19
 * Handles settings for specific calendar types and outputs the markup the settings page. a settings page.
20
 *
21
 * @since 3.0.0
22
 */
23
class Calendars extends Admin_Page {
24
25
	/**
26
	 * Calendar Types.
27
	 *
28
	 * @access private
29
	 * @var array
30
	 */
31
	private $calendar_types = array();
32
33
	/**
34
	 * Constructor.
35
	 *
36
	 * @since 3.0.0
37
	 */
38 View Code Duplication
	public function __construct() {
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...
39
40
		$this->id               = 'calendars';
41
		$this->option_group     = 'settings';
42
		$this->label            = __( 'Calendars', 'google-calendar-events' );
43
		//$this->description      = __( 'Manage calendar preferences and calendar types settings and options.', 'google-calendar-events' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44
45
		$calendars = simcal_get_calendar_types();
46
		$calendar_settings = array();
47
		if ( ! empty( $calendars ) && is_array( $calendars ) ) {
48
			foreach ( $calendars as $calendar => $views ) {
49
50
				$calendar_type = simcal_get_calendar( $calendar );
51
52
				if ( $calendar_type instanceof Calendar ) {
53
					$settings = $calendar_type->settings_fields();
54
					if ( ! empty( $settings ) ) {
55
						$calendar_settings[ $calendar ] = $settings;
56
					}
57
				}
58
			}
59
		}
60
61
		$this->calendar_types = $calendar_settings;
62
		$this->sections       = $this->add_sections();
63
		$this->fields         = $this->add_fields();
64
	}
65
66
	/**
67
	 * Add sections.
68
	 *
69
	 * @since  3.0.0
70
	 *
71
	 * @return array
72
	 */
73
	public function add_sections() {
74
75
		$sections = array(
76
			'general' => array(
77
				'title'       => __( 'General', 'google-calendar-events' ),
78
				'description' => '',
79
			),
80
		);
81
82
		$calendar_types = $this->calendar_types;
83
84
		if ( ! empty( $calendar_types ) && is_array( $calendar_types ) ) {
85
			foreach ( $calendar_types as $calendar_type => $type ) {
86
87
				$sections[ $calendar_type ] = array(
88
					'title' => $type['name'],
89
					'description' => $type['description'],
90
				);
91
92
			}
93
		}
94
95
		arsort( $calendar_types );
96
97
		return apply_filters( 'simcal_add_' . $this->option_group . '_' . $this->id .'_sections', $sections );
98
	}
99
100
	/**
101
	 * Add fields.
102
	 *
103
	 * @since  3.0.0
104
	 *
105
	 * @return array
106
	 */
107
	public function add_fields() {
108
109
		$fields       = array();
110
		$feed_types   = $this->calendar_types;
111
		$this->values = get_option( 'simple-calendar_' . $this->option_group . '_' . $this->id );
112
113
		foreach ( $this->sections as $section => $contents ) :
114
115
			if ( 'general' == $section ) {
116
117
				$options    = array();
118
				$post_types = get_post_types(
119
					array(
120
						'public' => false,
121
						'publicly_queriable' => false,
122
						'show_ui' => false,
123
					),
124
					'objects',
125
					'not'
126
				);
127
				unset( $post_types['attachment'] );
128
				unset( $post_types['calendar'] );
129
				unset( $post_types['gce_feed'] );
130
				foreach ( $post_types as $slug => $post_type ) {
131
					$options[ $slug ] = $post_type->label;
132
				}
133
				asort( $options );
134
135
				$fields[ $section ][] = array(
136
					'type'        => 'select',
137
					'multiselect' => 'multiselect',
138
					'enhanced'    => 'enhanced',
139
					'title'       => __( 'Attach Calendars', 'google-calendar-events' ),
140
					'tooltip'     => __( 'You can choose on which content types to add the ability to attach calendars.', 'google-calendar-events' ),
141
					'name'        => 'simple-calendar_' . $this->option_group . '_' . $this->id . '[' . $section . '][attach_calendars_posts]',
142
					'id'          => 'simple-calendar-' . $this->option_group . '-' . $this->id . '-attach-calendars-posts',
143
					'value'       => $this->get_option_value( $section, 'attach_calendars_posts' ),
144
					'default'     => 'post,page',
145
					'options'     => $options,
146
				);
147
148 View Code Duplication
			} elseif ( isset( $feed_types[ $section ]['fields'] ) ) {
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...
149
150
				foreach ( $feed_types[ $section ]['fields'] as $key => $args ) {
151
152
					$fields[ $section ][] = array_merge( $args, array(
153
						'name'  => 'simple-calendar_' . $this->option_group . '_' . $this->id . '[' . $section . '][' . $key . ']',
154
						'id'    => 'simple-calendar-' . $this->option_group . '-' . $this->id . '-' . $key,
155
						'value' => $this->get_option_value( $section, $key )
156
					) );
157
158
				}
159
160
			}
161
162
		endforeach;
163
164
		return apply_filters( 'simcal_add_' . $this->option_group . '_' . $this->id . '_fields', $fields );
165
	}
166
167
}
168