Field_Checkboxes::getInput()   C
last analyzed

Complexity

Conditions 11
Paths 98

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 98
nop 0
dl 0
loc 51
rs 6.9224
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
 * Part of the Joomla Framework Form Package
4
 *
5
 * @copyright  Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
6
 * @license    GNU General Public License version 2 or later; see LICENSE
7
 */
8
9
namespace Joomla\Form;
10
11
use Joomla\Form\Html\Select as HtmlSelect;
12
use Joomla\Language\Text;
13
14
/**
15
 * Form Field class for the Joomla Framework.
16
 * Displays options as a list of check boxes.
17
 * Multiselect may be forced to be true.
18
 *
19
 * @see         JFormFieldCheckbox
20
 * @since       1.0
21
 * @deprecated  The joomla/form package is deprecated
22
 */
23
class Field_Checkboxes extends Field
0 ignored issues
show
Deprecated Code introduced by
The class Joomla\Form\Field has been deprecated with message: The joomla/form package is deprecated

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
24
{
25
	/**
26
	 * The form field type.
27
	 *
28
	 * @var    string
29
	 * @since  1.0
30
	 */
31
	protected $type = 'Checkboxes';
32
33
	/**
34
	 * Flag to tell the field to always be in multiple values mode.
35
	 *
36
	 * @var    boolean
37
	 * @since  1.0
38
	 */
39
	protected $forceMultiple = true;
40
41
	/**
42
	 * Method to get the field input markup for check boxes.
43
	 *
44
	 * @return  string  The field input markup.
45
	 *
46
	 * @since   1.0
47
	 */
48
	protected function getInput()
49
	{
50
		$html = array();
51
52
		// Initialize some field attributes.
53
		$class = $this->element['class'] ? ' class="checkboxes ' . (string) $this->element['class'] . '"' : ' class="checkboxes"';
54
		$checkedOptions = explode(',', (string) $this->element['checked']);
55
56
		// Start the checkbox field output.
57
		$html[] = '<fieldset id="' . $this->id . '"' . $class . '>';
58
59
		// Get the field options.
60
		$options = $this->getOptions();
61
62
		// Build the checkbox field output.
63
		$html[] = '<ul>';
64
65
		foreach ($options as $i => $option)
66
		{
67
			// Initialize some option attributes.
68
			if (!isset($this->value) || empty($this->value))
69
			{
70
				$checked = (in_array((string) $option->value, (array) $checkedOptions) ? ' checked="checked"' : '');
71
			}
72
			else
73
			{
74
				$value = !is_array($this->value) ? explode(',', $this->value) : $this->value;
75
				$checked = (in_array((string) $option->value, $value) ? ' checked="checked"' : '');
76
			}
77
78
			$class = !empty($option->class) ? ' class="' . $option->class . '"' : '';
79
			$disabled = !empty($option->disable) ? ' disabled="disabled"' : '';
80
81
			// Initialize some JavaScript option attributes.
82
			$onclick = !empty($option->onclick) ? ' onclick="' . $option->onclick . '"' : '';
83
84
			$html[] = '<li>';
85
			$html[] = '<input type="checkbox" id="' . $this->id . $i . '" name="' . $this->name . '"' . ' value="'
86
				. htmlspecialchars($option->value, ENT_COMPAT, 'UTF-8') . '"' . $checked . $class . $onclick . $disabled . '/>';
87
88
			$html[] = '<label for="' . $this->id . $i . '"' . $class . '>' . Text::_($option->text) . '</label>';
0 ignored issues
show
Deprecated Code introduced by
The method Joomla\Language\Text::_() has been deprecated with message: 2.0 Will be replaced with a `translate` method.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
89
			$html[] = '</li>';
90
		}
91
92
		$html[] = '</ul>';
93
94
		// End the checkbox field output.
95
		$html[] = '</fieldset>';
96
97
		return implode($html);
98
	}
99
100
	/**
101
	 * Method to get the field options.
102
	 *
103
	 * @return  array  The field option objects.
104
	 *
105
	 * @since   1.0
106
	 */
107
	protected function getOptions()
108
	{
109
		$options = array();
110
111
		foreach ($this->element->children() as $option)
112
		{
113
			// Only add <option /> elements.
114
			if ($option->getName() != 'option')
115
			{
116
				continue;
117
			}
118
119
			// Create a new option object based on the <option /> element.
120
			$tmp = HtmlSelect::option(
121
				(string) $option['value'], trim((string) $option), 'value', 'text', ((string) $option['disabled'] == 'true')
122
			);
123
124
			// Set some option attributes.
125
			$tmp->class = (string) $option['class'];
126
127
			// Set some JavaScript option attributes.
128
			$tmp->onclick = (string) $option['onclick'];
129
130
			// Add the option object to the result set.
131
			$options[] = $tmp;
132
		}
133
134
		reset($options);
135
136
		return $options;
137
	}
138
}
139