Passed
Push — master ( f0f16f...a2933d )
by Jean-Christophe
02:00
created

HtmlList::addCheckedList()   D

Complexity

Conditions 11
Paths 150

Size

Total Lines 44
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 35
nc 150
nop 5
dl 0
loc 44
rs 4.9629
c 0
b 0
f 0

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
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
use Ajax\semantic\html\modules\checkbox\AbstractCheckbox;
10
11
class HtmlList extends HtmlSemCollection {
12
	protected $_hasCheckedList;
13
14
	public function __construct($identifier, $items=array()) {
15
		parent::__construct($identifier, "div", "ui list");
16
		$this->addItems($items);
17
		$this->_hasCheckedList=false;
18
	}
19
20
	protected function createItem($value) {
21
		$count=$this->count();
22
		$item=new HtmlListItem("item-" . $this->identifier . "-" . $count, $value);
23
		return $item;
24
	}
25
26
	public function addHeader($niveau, $content) {
27
		$header=new HtmlHeader("header-" . $this->identifier, $niveau, $content, "page");
28
		$this->wrap($header);
29
		return $header;
30
	}
31
32
	public function getItemPart($index,$partName="header"){
33
		return $this->getItem($index)->getPart($partName);
0 ignored issues
show
Bug introduced by
The method getPart() does not exist on Ajax\common\html\HtmlDoubleElement. It seems like you code against a sub-type of Ajax\common\html\HtmlDoubleElement such as Ajax\semantic\html\content\view\HtmlViewContent or Ajax\semantic\html\content\HtmlAbsractItem or Ajax\semantic\html\collections\table\HtmlTable or Ajax\semantic\html\content\view\HtmlViewItem or Ajax\bootstrap\html\base\HtmlNavElement or Ajax\bootstrap\html\HtmlForm or Ajax\semantic\html\base\HtmlSemNavElement. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
		return $this->getItem($index)->/** @scrutinizer ignore-call */ getPart($partName);
Loading history...
34
	}
35
36
	public function itemsAs($tagName) {
37
		return $this->contentAs($tagName);
38
	}
39
40
	public function asLinks($hrefs=[],$target=NUll) {
41
		$this->addToPropertyCtrl("class", "link", array ("link" ));
42
		return parent::asLinks($hrefs,$target);
43
	}
44
45
	public function addList($items=array()) {
46
		$list=new HtmlList("", $items);
47
		$list->setClass("list");
48
		return $this->addItem($list);
49
	}
50
51
	protected function getItemToAdd($item){
52
		$itemO=parent::getItemToAdd($item);
53
		if($itemO instanceof AbstractCheckbox)
54
			$itemO->addClass("item");
55
		return $itemO;
56
	}
57
58
	public function setCelled() {
59
		return $this->addToProperty("class", "celled");
60
	}
61
62
	public function setBulleted() {
63
		return $this->addToProperty("class", "bulleted");
64
	}
65
66
	public function setOrdered() {
67
		return $this->addToProperty("class", "ordered");
68
	}
69
70
	public function run(JsUtils $js) {
71
		if ($this->_hasCheckedList === true) {
72
			$jsCode=include dirname(__FILE__) . '/../../components/jsTemplates/tplCheckedList.php';
73
			$jsCode=\str_replace("%identifier%", "#" . $this->identifier, $jsCode);
74
			$this->executeOnRun($jsCode);
75
		}
76
		return parent::run($js);
77
	}
78
79
	public function setRelaxed() {
80
		return $this->addToProperty("class", "relaxed");
81
	}
82
83
	public function setSelection() {
84
		return $this->addToProperty("class", "selection");
85
	}
86
87
	public function setDivided() {
88
		return $this->addToProperty("class", "divided");
89
	}
90
91
	public function setHorizontal() {
92
		return $this->addToProperty("class", "horizontal");
93
	}
94
95
	/**
96
	 * Adds a grouped checked box to the list
97
	 * @param array $items
98
	 * @param string|array|null $masterItem
99
	 * @param array|null $values
100
	 * @param string $notAllChecked
101
	 * @return HtmlList
102
	 */
103
	public function addCheckedList($items=array(), $masterItem=NULL, $values=array(),$notAllChecked=false,$name=null) {
104
		$count=$this->count();
105
		$identifier=$this->identifier . "-" . $count;
106
		if (isset($masterItem)) {
107
			if(\is_array($masterItem)){
108
				$masterO=new HtmlFormCheckbox("master-" . $identifier, @$masterItem[0],@$masterItem[1]);
109
				if(isset($name))
110
					$masterO->setName($name);
111
				if(isset($masterItem[1])){
112
					if(\array_search($masterItem[1], $values)!==false){
0 ignored issues
show
Bug introduced by
It seems like $values can also be of type null; however, parameter $haystack of array_search() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
					if(\array_search($masterItem[1], /** @scrutinizer ignore-type */ $values)!==false){
Loading history...
113
						$masterO->getDataField()->setProperty("checked", "");
114
					}
115
				}
116
			}else{
117
				$masterO=new HtmlFormCheckbox("master-" . $identifier, $masterItem);
118
			}
119
			if($notAllChecked){
120
				$masterO->getDataField()->addClass("_notAllChecked");
121
			}
122
			$masterO->getHtmlCk()->addToProperty("class", "master");
123
			$masterO->setClass("item");
124
			$this->addItem($masterO);
125
		}
126
		$fields=array ();
127
		$i=0;
128
		foreach ( $items as $val => $caption ) {
129
			$itemO=new HtmlFormCheckbox($identifier . "-" . $i++, $caption, $val, "child");
130
			if (\array_search($val, $values) !== false) {
131
				$itemO->getDataField()->setProperty("checked", "");
132
			}
133
			if(isset($name))
134
				$itemO->setName($name);
135
			$itemO->setClass("item");
136
			$fields[]=$itemO;
137
		}
138
		if (isset($masterO) === true) {
139
			$list=new HtmlList("", $fields);
140
			$list->setClass("list");
141
			$masterO->addContent($list);
142
		} else {
143
			$this->addList($fields);
144
		}
145
		$this->_hasCheckedList=true;
146
		return $this;
147
	}
148
149
	public function setIcons($icons){
150
		if(!\is_array($icons)){
151
			$icons=\array_fill(0, \sizeof($this->content), $icons);
152
		}
153
		$max=\min(\sizeof($icons),\sizeof($this->content));
154
		for($i=0;$i<$max;$i++){
155
			$this->content[$i]->addIcon($icons[$i]);
156
		}
157
		return $this;
158
	}
159
}
160