GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Push — master ( 0dfd6f...55df53 )
by Jan
10:29 queued 04:40
created

FormBelegungsplanCategorySelect::__set()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 26
c 2
b 0
f 0
nc 11
nop 2
dl 0
loc 38
rs 5.2653

How to fix   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
* Contao Open Source CMS
4
*
5
* Copyright (c) Jan Karai
6
*
7
* @license LGPL-3.0+
8
*/
9
namespace Mailwurm\Belegung;
10
11
/**
12
 * Class FormBelegungsplanCategorySelect
13
 *
14
 * @property integer $mSize
15
 * @property boolean $mandatory
16
 * @property boolean $multiple
17
 * @property array   $options
18
 * @property boolean $chosen
19
 *
20
 * @author Jan Karai <https://www.sachsen-it.de>
21
 */
22
class FormBelegungsplanCategorySelect extends \Widget
23
{
24
	/**
25
	* Template
26
	*
27
	* @var string
28
	*/
29
	protected $strTemplate = 'form_belegungsplancategoryselect';
30
  
31
	/**
32
	* The CSS class prefix
33
	*
34
	* @var string
35
	*/
36
  	protected $strPrefix = 'widget widget-select';
37
38
	/**
39
	* Add specific attributes
40
	*
41
	* @param string $strKey   The attribute name
42
	* @param mixed  $varValue The attribute value
43
	*/
44
	public function __set($strKey, $varValue)
45
	{
46
		switch ($strKey)
47
		{
48
			case 'mandatory':
49
				if ($varValue)
50
				{
51
					$this->arrAttributes['required'] = 'required';
52
				}
53
				else
54
				{
55
					unset($this->arrAttributes['required']);
56
				}
57
				parent::__set($strKey, $varValue);
58
				break;
59
			case 'mSize':
60
				if ($this->multiple)
61
				{
62
					$this->arrAttributes['size'] = $varValue;
63
				}
64
				break;
65
			case 'multiple':
66
				if ($varValue != '')
67
				{
68
					$this->arrAttributes['multiple'] = 'multiple';
69
				}
70
				break;
71
			case 'options':
72
				$this->arrOptions = \StringUtil::deserialize($varValue);
0 ignored issues
show
Documentation Bug introduced by
It seems like StringUtil::deserialize($varValue) can also be of type string. However, the property $arrOptions is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
73
				break;
74
			case 'rgxp':
75
			case 'minlength':
76
			case 'maxlength':
77
				// Ignore
78
				break;
79
			default:
80
				parent::__set($strKey, $varValue);
81
				break;
82
		}
83
	}
84
  
85
	/**
86
	* Check options if the field is mandatory
87
	*/
88
	public function validate()
89
	{
90
		$mandatory = $this->mandatory;
91
		$options = $this->getPost($this->strName);
92
		// Check if there is at least one value
93
		if ($mandatory && is_array($options))
94
		{
95
			foreach ($options as $option)
96
			{
97
				if (strlen($option))
98
				{
99
					$this->mandatory = false;
100
					break;
101
				}
102
			}
103
		}
104
		$varInput = $this->validator($options);
105
		// Check for a valid option (see #4383)
106
		if (!empty($varInput) && !$this->isValidOption($varInput))
107
		{
108
			$this->addError($GLOBALS['TL_LANG']['ERR']['invalid']);
109
		}
110
		// Add class "error"
111
		if ($this->hasErrors())
112
		{
113
			$this->class = 'error';
114
		}
115
		else
116
		{
117
			$this->varValue = $varInput;
118
		}
119
		// Reset the property
120
		if ($mandatory)
121
		{
122
			$this->mandatory = true;
123
		}
124
	}
125
  
126
	/**
127
	* Return a parameter
128
	*
129
	* @param string $strKey The parameter name
130
	*
131
	* @return mixed The parameter value
132
	*/
133
	public function __get($strKey)
134
	{
135
		if ($strKey == 'options')
136
		{
137
			return $this->arrOptions;
138
		}
139
		return parent::__get($strKey);
140
	}
141
  
142
	/**
143
	* Parse the template file and return it as string
144
	*
145
	* @param array $arrAttributes An optional attributes array
146
	*
147
	* @return string The template markup
148
	*/
149
	public function parse($arrAttributes=null)
150
	{
151
		$strClass = 'select';
152 View Code Duplication
		if ($this->multiple)
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...
153
		{
154
			$this->strName .= '[]';
155
			$strClass = 'multiselect';
156
		}
157
		// Make sure there are no multiple options in single mode
158
		elseif (is_array($this->varValue))
159
		{
160
			$this->varValue = $this->varValue[0];
161
		}
162
		// Chosen
163
		if ($this->chosen)
164
		{
165
			$strClass .= ' tl_chosen';
166
		}
167
		// Custom class
168
		if ($this->strClass != '')
169
		{
170
			$strClass .= ' ' . $this->strClass;
171
		}
172
		$this->strClass = $strClass;
173
		return parent::parse($arrAttributes);
174
	}
175
  
176
	/**
177
	* Generate the options
178
	*
179
	* @return array The options array
180
	*/
181
	protected function getOptions()
182
	{
183
		$arrOptions = array();
184
		// Add empty option if there are none
185 View Code Duplication
		if (empty($this->arrOptions) || !is_array($this->arrOptions))
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...
186
		{
187
			$this->arrOptions = array(array('value' => '', 'label' => '-'));
188
		}
189
		// Generate options
190
		foreach ($this->arrOptions as $arrOption)
191
		{
192
			  $arrOptions[] = array_replace
193
				(
194
					$arrOption,
195
					array
196
					(
197
						'type'     => 'option',
198
						'value'    => $arrOption['value'],
199
						'selected' => $this->isSelected($arrOption),
200
						'label'    => $arrOption['label'],
201
					)
202
				);
203
		}
204
		return $arrOptions;
205
	}
206
  
207
	/**
208
	* Generate the widget and return it as string
209
	*
210
	* @return string The widget markup
211
	*/
212
	public function generate()
213
	{
214
		$strOptions = '';
215
		
216 View Code Duplication
		if ($this->multiple)
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...
217
		{
218
			$this->strName .= '[]';
219
		}
220
		// Make sure there are no multiple options in single mode
221
		elseif (is_array($this->varValue))
222
		{
223
			$this->varValue = $this->varValue[0];
224
		}
225
		// Add empty option if there are none
226 View Code Duplication
		if (empty($this->arrOptions) || !is_array($this->arrOptions))
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...
227
		{
228
			$this->arrOptions = array(array('value'=>'', 'label'=>'-'));
229
		}
230
		foreach ($this->arrOptions as $arrOption)
231
		{
232
			$strOptions .= sprintf('<option value="%s"%s>%s</option>',
233
									$arrOption['value'],
234
									$this->isSelected($arrOption),
235
									$arrOption['label']);
236
		}
237
		
238
		return sprintf('<select name="%s" id="ctrl_%s" class="%s"%s>%s</select>',
239
						$this->strName,
240
						$this->strId,
241
						$this->class,
242
						$this->getAttributes(),
243
						$strOptions);
244
	}
245
}
246