HtmlButtontoolbar::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 5
1
<?php
2
3
namespace Ajax\bootstrap\html;
4
5
/**
6
 * Twitter Bootstrap HTML Button toolbar
7
 * @see http://getbootstrap.com/components/#btn-groups-toolbar
8
 * @author jc
9
 * @version 1.001
10
 */
11
use Ajax\bootstrap\html\HtmlButtongroups;
12
13
class HtmlButtontoolbar extends HtmlButtongroups {
14
15
	public function __construct($identifier, $elements=array(), $cssStyle=NULL, $size=NULL, $tagName="div") {
16
		parent::__construct($identifier, $elements, $cssStyle, $size, $tagName);
17
		$this->setClass("btn-toolbar");
18
	}
19
20
	/*
21
	 * (non-PHPdoc)
22
	 * @see \Ajax\bootstrap\html\HtmlButtongroups::addElement()
23
	 */
24
	public function addElement($element) {
25
		if ($element instanceof HtmlButtongroups) {
26
			$this->elements []=$element;
27
		} else {
28
			$this->getLastButtonGroup()->addElement($element);
29
		}
30
	}
31
32
	/**
33
	 * Add and return a new buttongroup
34
	 * @return \Ajax\bootstrap\html\HtmlButtongroups
35
	 */
36
	public function addGroup() {
37
		$nb=sizeof($this->elements);
38
		$bg=new HtmlButtongroups($this->identifier."-buttongroups-".$nb);
39
		$this->elements []=$bg;
40
		return $bg;
41
	}
42
43
	/**
44
	 *
45
	 * @return HtmlButtongroups
46
	 */
47
	private function getLastButtonGroup() {
48
		$nb=sizeof($this->elements);
49
		if ($nb>0)
50
			$bg=$this->elements [$nb-1];
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->elements[$nb - 1]; of type Ajax\bootstrap\html\Html...p\html\HtmlButtongroups adds the type Ajax\bootstrap\html\HtmlButton to the return on line 55 which is incompatible with the return type documented by Ajax\bootstrap\html\Html...bar::getLastButtonGroup of type Ajax\bootstrap\html\HtmlButtongroups.
Loading history...
51
		else {
52
			$bg=new HtmlButtongroups($this->identifier."-buttongroups-".$nb);
53
			$this->elements []=$bg;
54
		}
55
		return $bg;
56
	}
57
58
	/**
59
	 * return the Buttongroups at position $index
60
	 * @return \Ajax\bootstrap\html\HtmlButtongroups
61
	 */
62
	public function getGroup($index) {
63
		return parent::getElement($index);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getElement() instead of getGroup()). Are you sure this is correct? If so, you might want to change this to $this->getElement().

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...
64
	}
65
66
	public function getLastGroup() {
67
		$bg=null;
68
		$nb=sizeof($this->elements);
69
		if ($nb>0)
70
			$bg=$this->elements [$nb-1];
71
		return $bg;
72
	}
73
74
	/*
75
	 * (non-PHPdoc)
76
	 * @see \Ajax\bootstrap\html\HtmlButtongroups::getElement()
77
	 */
78
	public function getElement($index) {
79
		$element=null;
80
		$i=0;
81
		if (is_int($index)) {
82
			$elements=array ();
83
			foreach ( $this->elements as $group ) {
84
				$elements=array_merge($elements, $group->getElements());
0 ignored issues
show
Bug introduced by
The method getElements does only exist in Ajax\bootstrap\html\HtmlButtongroups, but not in Ajax\bootstrap\html\HtmlButton.

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...
85
			}
86
			if ($index<sizeof($elements)) {
87
				$element=$elements [$index];
88
			}
89
		} else {
90
			while ( $element==null&&$i<sizeof($this->elements) ) {
91
				$element=$this->elements [$i]->getElement($index);
0 ignored issues
show
Bug introduced by
The method getElement does only exist in Ajax\bootstrap\html\HtmlButtongroups, but not in Ajax\bootstrap\html\HtmlButton.

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...
92
				$i++;
93
			}
94
		}
95
		return $element;
96
	}
97
}