Completed
Push — master ( 9e0835...2461e8 )
by Jean-Christophe
03:27
created

HtmlList::setRelaxed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Ajax\semantic\html\elements;
4
5
use Ajax\semantic\html\base\HtmlSemCollection;
6
use Ajax\semantic\html\content\HtmlListItem;
7
use Ajax\semantic\html\collections\form\HtmlFormCheckbox;
8
use Ajax\JsUtils;
9
10
class HtmlList extends HtmlSemCollection{
11
	protected $_hasCheckedList;
12
13
	public function __construct( $identifier, $items=array()){
14
		parent::__construct( $identifier, "div", "ui list");
15
		$this->addItems($items);
16
		$this->_hasCheckedList=false;
17
	}
18
19
20
	protected function createItem($value){
21
		$count=$this->count();
22
		if(\is_array($value)){
23
			$item=new HtmlListItem("item-".$this->identifier."-".$count, $value[0]);
24
			$item->addIcon($value[1]);
25
		}else
26
			$item= new HtmlListItem("item-".$this->identifier."-".$count, $value);
27
		return $item;
28
	}
29
30
	public function addHeader($niveau,$content){
31
		return $this->wrap(new HtmlHeader("header-".$this->identifier,$niveau,$content,"page"));
32
	}
33
34
	public function itemsAs($tagName){
35
		return $this->contentAs($tagName);
36
	}
37
38
	public function asLink(){
39
		$this->addToPropertyCtrl("class", "link", array("link"));
40
		return $this->contentAs("a");
41
	}
42
43
	public function addList($items=array()){
44
		$list=new HtmlList("",$items);
45
		$list->setClass("list");
46
		return $this->addItem($list);
47
	}
48
49
	public function setCelled(){
50
		return $this->addToProperty("class", "celled");
51
	}
52
53
	public function run(JsUtils $js){
54
		if($this->_hasCheckedList===true){
55
			$jsCode=include dirname(__FILE__).'/../../components/jsTemplates/tplCheckedList.php';
56
			$jsCode=\str_replace("%identifier%","#".$this->identifier,$jsCode);
57
			$this->executeOnRun($jsCode);
58
		}
59
		return parent::run($js);
60
	}
61
62
	public function setRelaxed(){
63
		return $this->addToProperty("class", "relaxed");
64
	}
65
66
	public function addCheckedList($items=array(),$masterItem=NULL,$values=array()){
67
		$count=$this->count();
68
		$identifier=$this->identifier."-".$count;
69
		if(isset($masterItem)){
70
			$masterO=new HtmlFormCheckbox("master-".$identifier,$masterItem);
71
			$masterO->getField()->addToProperty("class","master");
72
			$masterO->setClass("item");
73
			$this->addItem($masterO);
74
		}
75
		$fields=array();
76
		$i=0;
77
		foreach ($items as $val=>$caption){
78
			$itemO=new HtmlFormCheckbox($identifier."-".$i++,$caption,$val,"child");
79
			if(\array_search($val, $values)!==false){
80
				$itemO->getInput()->getField()->setProperty("checked", "");
81
			}
82
			$itemO->setClass("item");
83
			$fields[]=$itemO;
84
		}
85
		if(isset($masterO)===true){
86
			$list=new HtmlList("",$fields);
87
			$list->setClass("list");
88
			$masterO->addContent($list);
0 ignored issues
show
Bug introduced by
The variable $masterO does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
89
		}else{
90
			$this->addList($fields);
91
		}
92
		$this->_hasCheckedList=true;
93
		return $this;
94
	}
95
}