Completed
Push — master ( 66af5c...1d23a1 )
by Jean-Christophe
03:38
created

HtmlMenu   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 188
Duplicated Lines 6.38 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 39
c 6
b 0
f 0
lcom 2
cbo 10
dl 12
loc 188
rs 8.2857

29 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setType() 0 3 1
A setActiveItem() 0 7 2
A getItemToInsert() 0 8 4
A afterInsert() 0 8 2
A addItem() 0 4 1
A insertItem() 0 4 1
A generateMenuAsItem() 5 12 2
A addMenuAsItem() 0 3 1
A addPopupAsItem() 0 9 1
A addDropdownAsItem() 0 7 2
A createItem() 0 4 1
A setInverted() 0 3 1
A setSecondary() 0 3 1
A setVertical() 0 3 1
A setPosition() 0 3 1
A setPointing() 0 3 1
A asTab() 0 6 2
A asPagination() 0 4 1
A setFloated() 0 3 1
A floatRight() 0 3 1
A floatLeft() 0 3 1
A setFixed() 0 3 1
A setFluid() 0 3 1
A setCompact() 0 3 1
A fromDatabaseObject() 0 7 2
A setWidth() 7 7 2
A addImage() 0 3 1
A vertical() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Ajax\semantic\html\collections\menus;
4
5
use Ajax\common\html\HtmlDoubleElement;
6
use Ajax\semantic\html\base\constants\Direction;
7
use Ajax\semantic\html\base\HtmlSemCollection;
8
use Ajax\semantic\html\base\HtmlSemDoubleElement;
9
use Ajax\semantic\html\base\constants\Wide;
10
use Ajax\common\html\html5\HtmlImg;
11
use Ajax\bootstrap\html\HtmlInput;
12
use Ajax\semantic\html\modules\HtmlDropdown;
13
use Ajax\common\html\BaseHtml;
14
use Ajax\semantic\html\modules\HtmlPopup;
15
use Ajax\semantic\html\elements\HtmlIcon;
16
use Ajax\semantic\html\elements\html5\HtmlLink;
17
18
/**
19
 * Semantic Menu component
20
 * @see http://semantic-ui.com/collections/menu.html
21
 * @author jc
22
 * @version 1.001
23
 */
24
class HtmlMenu extends HtmlSemCollection {
25
26
	public function __construct($identifier, $items=array()) {
27
		parent::__construct($identifier, "div","ui menu");
28
		$this->addItems($items);
29
	}
30
31
	/**
32
	 * Sets the menu type
33
	 * @param string $type one of text,item
34
	 * @return \Ajax\semantic\html\collections\HtmlMenu
35
	 */
36
	public function setType($type=""){
37
		return $this->addToPropertyCtrl("class", $type, array("","item","text"));
38
	}
39
40
	public function setActiveItem($index){
41
		$item=$this->getItem($index);
42
		if($item!==null){
43
			$item->addToProperty("class", "active");
44
		}
45
		return $this;
46
	}
47
	private function getItemToInsert($item){
48
		if($item instanceof HtmlInput || $item instanceof HtmlImg || $item instanceof HtmlIcon){
49
			$itemO=new HtmlSemDoubleElement("item-".$this->identifier,"div","");
50
			$itemO->setContent($item);
51
			$item=$itemO;
52
		}
53
		return $item;
54
	}
55
56
	private function afterInsert($item){
57
		if(!$item instanceof HtmlMenu)
58
			$item->addToPropertyCtrl("class", "item",array("item"));
59
		else{
60
			$this->setSecondary();
61
		}
62
		return $item;
63
	}
64
	/**
65
	 * {@inheritDoc}
66
	 * @see \Ajax\common\html\html5\HtmlCollection::addItem()
67
	 */
68
	public function addItem($item){
69
		$item=parent::addItem($this->getItemToInsert($item));
70
		return $this->afterInsert($item);
71
	}
72
73
	/**
74
	 * {@inheritDoc}
75
	 * @see \Ajax\common\html\HtmlCollection::insertItem()
76
	 */
77
	public function insertItem($item,$position=0){
78
		$item=parent::insertItem($this->getItemToInsert($item),$position);
79
		return $this->afterInsert($item);
80
	}
81
82
	public function generateMenuAsItem($menu,$header=null){
83
		$count=$this->count();
84
		$item=new HtmlSemDoubleElement("item-".$this->identifier."-".$count,"div");
85 View Code Duplication
		if(isset($header)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
			$headerItem=new HtmlSemDoubleElement("item-header-".$this->identifier."-".$count,"div","header");
87
			$headerItem->setContent($header);
88
			$item->addContent($headerItem);
89
		}
90
		$menu->setClass("menu");
91
		$item->addContent($menu);
92
		return $item;
93
	}
94
	public function addMenuAsItem($menu,$header=null){
95
		return $this->addItem($this->generateMenuAsItem($menu,$header));
96
	}
97
98
	public function addPopupAsItem($value,$identifier, $content=""){
99
		$value=new HtmlSemDoubleElement($identifier,"a","browse item",$value);
100
		$value->addContent(new HtmlIcon("", "dropdown"));
101
		$value=$this->addItem($value);
102
		$popup=new HtmlPopup($value,"popup-".$this->identifier."-".$this->count(),$content);
103
		$popup->setFlowing()->setPosition("bottom left")->setOn("click");
104
		$this->wrap("",$popup);
105
		return $popup;
106
	}
107
108
	public function addDropdownAsItem($value,$items=NULL){
109
		$dd=$value;
110
		if(\is_string($value)){
111
			$dd=new HtmlDropdown("dropdown-".$this->identifier."-".$this->count(),$value,$items);
112
		}
113
		return $this->addItem($dd);
114
	}
115
	/**
116
	 * {@inheritDoc}
117
	 * @see \Ajax\common\html\html5\HtmlCollection::createItem()
118
	 */
119
	protected function createItem($value) {
120
		$itemO=new HtmlLink("item-".\sizeof($this->content),"",$value);
121
		return $itemO->setClass("item");
122
	}
123
124
	public function setInverted(){
125
		return $this->addToProperty("class", "inverted");
126
	}
127
128
	public function setSecondary(){
129
		return $this->addToProperty("class", "secondary");
130
	}
131
132
	public function setVertical(){
133
		return $this->addToPropertyCtrl("class", "vertical",array("vertical"));
134
	}
135
136
	public function setPosition($value="right"){
137
		return $this->addToPropertyCtrl("class", $value,array("right","left"));
138
	}
139
140
	public function setPointing($value=Direction::NONE){
141
		return $this->addToPropertyCtrl("class", $value." pointing",Direction::getConstantValues("pointing"));
142
	}
143
144
	public function asTab($vertical=false){
145
		$this->apply(function(HtmlDoubleElement &$item){$item->setTagName("a");});
146
		if($vertical===true)
147
			$this->setVertical();
148
		return $this->addToProperty("class", "tabular");
149
	}
150
151
	public function asPagination(){
152
		$this->apply(function(HtmlDoubleElement &$item){$item->setTagName("a");});
153
			return $this->addToProperty("class", "pagination");
154
	}
155
156
	public function setFloated($direction="right"){
157
		return $this->addToPropertyCtrl("class", $direction." floated", Direction::getConstantValues("floated"));
158
	}
159
160
	public function floatRight(){
161
		return $this->setFloated();
162
	}
163
164
	public function floatLeft(){
165
		return $this->setFloated("left");
166
	}
167
168
	public function setFixed(){
169
		return $this->addToProperty("class", "fixed");
170
	}
171
172
	public function setFluid(){
173
		return $this->addToProperty("class", "fluid");
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->addToProperty('class', 'fluid'); (Ajax\semantic\html\collections\menus\HtmlMenu) is incompatible with the return type of the parent method Ajax\semantic\html\base\...SemCollection::setFluid of type Ajax\semantic\html\base\HtmlSemDoubleElement.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
174
	}
175
176
	public function setCompact(){
177
		return $this->addToProperty("class", "compact");
178
	}
179
180
	/* (non-PHPdoc)
181
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
182
	 */
183
	public function fromDatabaseObject($object, $function) {
184
		$return=$function($object);
185
		if(\is_array($return))
186
			$this->addItems($return);
187
		else
188
		$this->addItem($return);
189
	}
190
191
	/**
192
	 * Defines the menu width
193
	 * @param int $width
194
	 * @return \Ajax\semantic\html\collections\menus\HtmlMenu
195
	 */
196 View Code Duplication
	public function setWidth($width){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
197
		if(\is_int($width)){
198
			$width=Wide::getConstants()["W".$width];
199
		}
200
		$this->addToPropertyCtrl("class", $width, Wide::getConstants());
201
		return $this->addToPropertyCtrl("class", "item",array("item"));
202
	}
203
204
	public function addImage($identifier, $src="", $alt=""){
205
		return $this->addItem(new HtmlImg($identifier,$src,$alt));
206
	}
207
208
	public static function vertical($identifier,$items=array()){
209
		return (new HtmlMenu($identifier,$items))->setVertical();
210
	}
211
}