Completed
Push — 1.10.x ( 3d9ba2...d7355d )
by
unknown
52:02
created

HTML_QuickForm_receivers::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 9
nc 1
nop 3
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Form element to select receivers
6
 * This element contains 1 radio-buttons. One with label 'everybody' and one
7
 * with label 'select users/groups'. Only if the second radio-button is
8
 * selected, 2 select-list show up. The user can move items between the 2
9
 * checkboxes.
10
 */
11
class HTML_QuickForm_receivers extends HTML_QuickForm_group
12
{
13
	/**
14
	 * Array of all receivers
15
	 */
16
	var $receivers;
17
	/**
18
	 * Array of selected receivers
19
	 */
20
	var $receivers_selected;
21
	/**
22
	 * Constructor
23
	 * @param string $elementName
24
	 * @param string $elementLabel
25
	 * @param array $attributes This should contain the keys 'receivers' and
26
	 * 'receivers_selected'
27
	 */
28
	public function __construct($elementName = null, $elementLabel = null, $attributes = null)
29
	{
30
		$this->receivers = $attributes['receivers'];
31
		$this->receivers_selected = $attributes['receivers_selected'];
32
		unset($attributes['receivers']);
33
		unset($attributes['receivers_selected']);
34
		parent::__construct($elementName, $elementLabel, $attributes);
0 ignored issues
show
Bug introduced by
It seems like $elementLabel defined by parameter $elementLabel on line 28 can also be of type string; however, HTML_QuickForm_group::__construct() does only seem to accept array|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
35
		$this->_persistantFreeze = true;
36
		$this->_appendName = true;
37
		$this->_type = 'receivers';
38
	}
39
	/**
40
	 * Create the form elements to build this element group
41
	 */
42
	function _createElements()
43
	{
44
		$this->_elements[] = new HTML_QuickForm_Radio('receivers', '', get_lang('Everybody'), '0', array ('onclick' => 'javascript:receivers_hide(\'receivers_to\')'));
45
		$this->_elements[0]->setChecked(true);
46
		$this->_elements[] = new HTML_QuickForm_Radio('receivers', '', get_lang('SelectGroupsUsers'), '1', array ('onclick' => 'javascript:receivers_show(\'receivers_to\')'));
47
		$this->_elements[] = new HTML_QuickForm_advmultiselect('to', '', $this->receivers);
48
		$this->_elements[2]->setSelected($this->receivers_selected);
0 ignored issues
show
Bug introduced by
The method setSelected does only exist in HTML_QuickForm_advmultiselect, but not in HTML_QuickForm_radio.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
49
	}
50
	/**
51
	 * HTML representation
52
	 */
53
	public function toHtml()
54
	{
55
		include_once ('HTML/QuickForm/Renderer/Default.php');
56
		$this->_separator = '<br/>';
57
		$renderer = & new HTML_QuickForm_Renderer_Default();
58
		$renderer->setElementTemplate('{element}');
59
		$select_boxes = $this->_elements[2];
60
		$select_boxes->setElementTemplate('<div style="margin-left:20px;display:block;" id="receivers_'.$select_boxes->getName().'">'.$select_boxes->_elementTemplate.'</div>');
61
		parent :: accept($renderer);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (accept() instead of toHtml()). Are you sure this is correct? If so, you might want to change this to $this->accept().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
62
		$js = $this->getElementJS();
63
		return $renderer->toHtml().$js;
64
	}
65
66
	/**
67
	 * Get the necessary javascript
68
	 */
69
    public function getElementJS()
70
	{
71
		$js = "<script type=\"text/javascript\">
72
					/* <![CDATA[ */
73
					receivers_hide('receivers_to');
74
					function receivers_show(item) {
75
						el = document.getElementById(item);
76
						el.style.display='';
77
					}
78
					function receivers_hide(item) {
79
						el = document.getElementById(item);
80
						el.style.display='none';
81
					}
82
					/* ]]> */
83
					</script>\n";
84
		return $js;
85
	}
86
	/**
87
	 * accept renderer
88
	 */
89
	function accept(& $renderer, $required = false, $error = null)
90
	{
91
		$renderer->renderElement($this, $required, $error);
92
	}
93
}
94
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
95