Completed
Push — master ( 5f7aee...f5f495 )
by Jean-Christophe
03:30
created

HtmlList::setIcons()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
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
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
	protected function createItem($value) {
20
		$count=$this->count();
21
		$item=new HtmlListItem("item-" . $this->identifier . "-" . $count, $value);
22
		return $item;
23
	}
24
25
	public function addHeader($niveau, $content) {
26
		$header=new HtmlHeader("header-" . $this->identifier, $niveau, $content, "page");
27
		$this->wrap($header);
28
		return $header;
29
	}
30
31
	public function getItemPart($index,$partName="header"){
32
		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\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...
33
	}
34
35
	public function itemsAs($tagName) {
36
		return $this->contentAs($tagName);
37
	}
38
39
	public function asLink() {
40
		$this->addToPropertyCtrl("class", "link", array ("link" ));
41
		return $this->contentAs("a");
42
	}
43
44
	public function addList($items=array()) {
45
		$list=new HtmlList("", $items);
46
		$list->setClass("list");
47
		return $this->addItem($list);
48
	}
49
50
	public function setCelled() {
51
		return $this->addToProperty("class", "celled");
52
	}
53
54
	public function setBulleted() {
55
		return $this->addToProperty("class", "bulleted");
56
	}
57
58
	public function setOrdered() {
59
		return $this->addToProperty("class", "ordered");
60
	}
61
62
	public function run(JsUtils $js) {
63
		if ($this->_hasCheckedList === true) {
64
			$jsCode=include dirname(__FILE__) . '/../../components/jsTemplates/tplCheckedList.php';
65
			$jsCode=\str_replace("%identifier%", "#" . $this->identifier, $jsCode);
66
			$this->executeOnRun($jsCode);
67
		}
68
		return parent::run($js);
69
	}
70
71
	public function setRelaxed() {
72
		return $this->addToProperty("class", "relaxed");
73
	}
74
75
	public function setSelection() {
76
		return $this->addToProperty("class", "selection");
77
	}
78
79
	public function setDivided() {
80
		return $this->addToProperty("class", "divided");
81
	}
82
83
	public function setHorizontal() {
84
		return $this->addToProperty("class", "horizontal");
85
	}
86
87
	public function addCheckedList($items=array(), $masterItem=NULL, $values=array()) {
88
		$count=$this->count();
89
		$identifier=$this->identifier . "-" . $count;
90
		if (isset($masterItem)) {
91
			$masterO=new HtmlFormCheckbox("master-" . $identifier, $masterItem);
92
			$masterO->getHtmlCk()->addToProperty("class", "master");
93
			$masterO->setClass("item");
94
			$this->addItem($masterO);
95
		}
96
		$fields=array ();
97
		$i=0;
98
		foreach ( $items as $val => $caption ) {
99
			$itemO=new HtmlFormCheckbox($identifier . "-" . $i++, $caption, $val, "child");
100
			if (\array_search($val, $values) !== false) {
101
				$itemO->getField()->setProperty("checked", "");
102
			}
103
			$itemO->setClass("item");
104
			$fields[]=$itemO;
105
		}
106
		if (isset($masterO) === true) {
107
			$list=new HtmlList("", $fields);
108
			$list->setClass("list");
109
			$masterO->addContent($list);
110
		} else {
111
			$this->addList($fields);
112
		}
113
		$this->_hasCheckedList=true;
114
		return $this;
115
	}
116
117
	public function setIcons($icons){
118
		if(!\is_array($icons)){
119
			$icons=\array_fill(0, \sizeof($this->content), $icons);
120
		}
121
		$max=\min(\sizeof($icons),\sizeof($this->content));
122
		for($i=0;$i<$max;$i++){
123
			$this->content[$i]->addIcon($icons[$i]);
124
		}
125
		return $this;
126
	}
127
}