Checkbox::html()   C
last analyzed

Complexity

Conditions 11
Paths 12

Size

Total Lines 62
Code Lines 40

Duplication

Lines 14
Ratio 22.58 %

Importance

Changes 0
Metric Value
cc 11
eloc 40
nc 12
nop 0
dl 14
loc 62
rs 6.1722
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Checkbox Field
4
 *
5
 * @package SimpleCalendar/Admin
6
 */
7
namespace SimpleCalendar\Admin\Fields;
8
9
use SimpleCalendar\Abstracts\Field;
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit; // Exit if accessed directly.
13
}
14
15
/**
16
 * Checkbox input field.
17
 *
18
 * Outputs one single checkbox or a fieldset of checkboxes for multiple choices.
19
 *
20
 * @since 3.0.0
21
 */
22
class Checkbox extends Field {
23
24
	/**
25
	 * Construct.
26
	 *
27
	 * @since 3.0.0
28
	 *
29
	 * @param array $field
30
	 */
31
	public function __construct( $field ) {
32
		$this->type_class = 'simcal-field-checkboxes';
33
		parent::__construct( $field );
34
	}
35
36
	/**
37
	 * Outputs the field markup.
38
	 *
39
	 * @since 3.0.0
40
	 */
41
	public function html() {
42
43
		if ( ! empty( $this->options ) && count( (array) $this->options ) > 1 ) {
44
45
			if ( ! empty( $this->description ) ) {
46
				echo '<p class="description">' . wp_kses_post( $this->description ) . ' ' . $this->tooltip . '</p>';
47
			}
48
49
			?>
50
			<fieldset class="<?php echo $this->class; ?>" <?php echo ! empty( $this->style ) ? 'style="' . $this->style . '"' : ''; ?>>
51
				<?php
52
53
				if ( ! empty( $this->title ) ) {
54
					echo '<legend class="screen-reader-text"><span>' . $this->title . '</span></legend>';
55
				}
56
57
				?>
58
				<ul>
59 View Code Duplication
					<?php foreach ( $this->options as $option => $name ) : ?>
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...
60
						<li>
61
							<label for="<?php echo $this->id . '-' . trim( strval( $option ) ); ?>">
62
								<input name="<?php echo $this->name; ?>"
63
								       id="<?php echo $this->id . '-' . trim( strval( $option ) ); ?>"
64
								       class="simcal-field simcal-field-checkbox"
65
								       type="checkbox"
66
								       value="<?php echo trim( strval( $option ) ); ?>"
67
										<?php checked( $this->value, 'yes', true ); ?>
68
									<?php echo $this->attributes; ?>
69
									/><?php echo esc_attr( $name ); ?>
70
							</label>
71
						</li>
72
					<?php endforeach; ?>
73
				</ul>
74
			</fieldset>
75
			<?php
76
77
		} else {
78
79
			?>
80
			<span class="simcal-field-bool" <?php echo $this->style ? 'style="' . $this->style . '"' : ''; ?>>
81
				<?php if ( ! empty( $this->title ) ) : ?>
82
					<span class="screen-reader-text"><?php echo $this->title; ?></span>
83
				<?php endif; ?>
84
				<input name="<?php echo $this->name; ?>"
85
				       type="checkbox"
86
				       id="<?php echo $this->id; ?>"
87
				       class="simcal-field simcal-field-checkbox <?php echo $this->class; ?>"
88
				       value="yes"
89
					<?php checked( $this->value, 'yes', true ); ?>
90
					<?php echo $this->attributes; ?>/><?php echo ( ! empty( $this->text ) ? $this->text : __( 'Yes', 'google-calendar-events' ) ); ?>
91
			</span>
92
			<?php
93
94
			echo $this->tooltip;
95
96
			if ( ! empty( $this->description ) ) {
97
				echo '<p class="description">' . wp_kses_post( $this->description ) . '</p>';
98
			}
99
100
		}
101
102
	}
103
104
}
105