Completed
Push — master ( eb7f5d...c7a943 )
by Jean-Christophe
03:07
created

HtmlList::itemsAs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
It seems like you code against a specific sub-type and not the parent class Ajax\common\html\HtmlDoubleElement as the method getPart() does only exist in the following sub-classes of Ajax\common\html\HtmlDoubleElement: Ajax\bootstrap\html\HtmlForm, Ajax\semantic\html\collections\table\HtmlTable, Ajax\semantic\html\content\HtmlAbsractItem, Ajax\semantic\html\content\HtmlListItem, Ajax\semantic\html\content\HtmlStepItem, Ajax\semantic\html\content\view\HtmlItem, Ajax\semantic\html\content\view\HtmlViewContent, Ajax\semantic\html\content\view\HtmlViewItem, Ajax\semantic\html\views\HtmlCard. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
34
	}
35
36
	public function itemsAs($tagName) {
37
		return $this->contentAs($tagName);
38
	}
39
40
	public function asLink() {
41
		$this->addToPropertyCtrl("class", "link", array ("link" ));
42
		return $this->contentAs("a");
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) {
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($masterItem[1])){
110
					if(\array_search($masterItem[1], $values)!==false){
111
						$masterO->getDataField()->setProperty("checked", "");
112
					}
113
				}
114
			}else{
115
				$masterO=new HtmlFormCheckbox("master-" . $identifier, $masterItem);
116
			}
117
			if($notAllChecked){
0 ignored issues
show
Bug Best Practice introduced by
The expression $notAllChecked of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
118
				$masterO->getDataField()->addClass("_notAllChecked");
119
			}
120
			$masterO->getHtmlCk()->addToProperty("class", "master");
121
			$masterO->setClass("item");
122
			$this->addItem($masterO);
123
		}
124
		$fields=array ();
125
		$i=0;
126
		foreach ( $items as $val => $caption ) {
127
			$itemO=new HtmlFormCheckbox($identifier . "-" . $i++, $caption, $val, "child");
128
			if (\array_search($val, $values) !== false) {
129
				$itemO->getDataField()->setProperty("checked", "");
130
			}
131
			$itemO->setClass("item");
132
			$fields[]=$itemO;
133
		}
134
		if (isset($masterO) === true) {
135
			$list=new HtmlList("", $fields);
136
			$list->setClass("list");
137
			$masterO->addContent($list);
138
		} else {
139
			$this->addList($fields);
140
		}
141
		$this->_hasCheckedList=true;
142
		return $this;
143
	}
144
145
	public function setIcons($icons){
146
		if(!\is_array($icons)){
147
			$icons=\array_fill(0, \sizeof($this->content), $icons);
148
		}
149
		$max=\min(\sizeof($icons),\sizeof($this->content));
150
		for($i=0;$i<$max;$i++){
151
			$this->content[$i]->addIcon($icons[$i]);
152
		}
153
		return $this;
154
	}
155
}
156