Total Complexity | 49 |
Total Lines | 233 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 1 | Features | 0 |
Complex classes like HtmlMenu often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HtmlMenu, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class HtmlMenu extends HtmlSemCollection { |
||
30 | use AttachedTrait; |
||
31 | private $_itemHeader; |
||
32 | |||
33 | public function __construct($identifier, $items=array()) { |
||
34 | parent::__construct($identifier, "div", "ui menu"); |
||
35 | $this->addItems($items); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Sets the menu type |
||
40 | * @param string $type one of text,item |
||
41 | * @return HtmlMenu |
||
42 | */ |
||
43 | public function setType($type="") { |
||
44 | return $this->addToPropertyCtrl("class", $type, array ("","item","text" )); |
||
45 | } |
||
46 | |||
47 | public function setActiveItem($index) { |
||
53 | } |
||
54 | |||
55 | private function getItemToInsert($item) { |
||
56 | if ($item instanceof HtmlInput || $item instanceof HtmlImg || $item instanceof HtmlIcon || $item instanceof HtmlButtonGroups || $item instanceof HtmlButton || $item instanceof HtmlLabel) { |
||
57 | $itemO=new HtmlMenuItem("item-" . $this->identifier . "-" . \sizeof($this->content) , $item); |
||
58 | $itemO->addClass("no-active"); |
||
59 | $item=$itemO; |
||
60 | } |
||
61 | return $item; |
||
62 | } |
||
63 | |||
64 | private function afterInsert($item) { |
||
65 | if (!$item instanceof HtmlMenu && $item->propertyContains("class", "header")===false) |
||
66 | $item->addToPropertyCtrl("class", "item", array ("item" )); |
||
67 | else { |
||
68 | $this->setSecondary(); |
||
69 | } |
||
70 | return $item; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * |
||
75 | * {@inheritDoc} |
||
76 | * |
||
77 | * @see HtmlCollection::addItem() |
||
78 | */ |
||
79 | public function addItem($item) { |
||
80 | $number=$item; |
||
81 | $item=parent::addItem($this->getItemToInsert($item)); |
||
82 | if(\is_int($number)) |
||
83 | $item->setProperty("data-page", $number); |
||
84 | return $this->afterInsert($item); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * |
||
89 | * {@inheritDoc} |
||
90 | * |
||
91 | * @see \Ajax\common\html\HtmlCollection::insertItem() |
||
92 | */ |
||
93 | public function insertItem($item, $position=0) { |
||
94 | $item=parent::insertItem($this->getItemToInsert($item), $position); |
||
95 | return $this->afterInsert($item); |
||
96 | } |
||
97 | |||
98 | public function generateMenuAsItem($menu, $header=null) { |
||
99 | $count=$this->count(); |
||
100 | $item=new HtmlSemDoubleElement("item-" . $this->identifier . "-" . $count, "div"); |
||
101 | if (isset($header)) { |
||
102 | $headerItem=new HtmlSemDoubleElement("item-header-" . $this->identifier . "-" . $count, "div", "header"); |
||
103 | $headerItem->setContent($header); |
||
104 | $item->addContent($headerItem); |
||
105 | $this->_itemHeader=$headerItem; |
||
106 | } |
||
107 | if(\is_array($menu)){ |
||
108 | $menu=new HtmlMenu("menu-" . $this->identifier . "-" . $count,$menu); |
||
109 | } |
||
110 | $menu->setClass("menu"); |
||
111 | $item->addContent($menu); |
||
112 | return $item; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Adds an header to the menu |
||
117 | * @param String|HtmlDoubleElement $caption |
||
118 | * @return \Ajax\common\html\HtmlDoubleElement |
||
119 | */ |
||
120 | public function addHeader($caption){ |
||
121 | if(!($caption instanceof HtmlDoubleElement)){ |
||
122 | $header=new HtmlDoubleElement('','div'); |
||
123 | $header->setContent($caption); |
||
124 | }else{ |
||
125 | $header=$caption; |
||
126 | } |
||
127 | $header->addClass('item header'); |
||
128 | $this->wrapContent($header); |
||
129 | return $header; |
||
130 | } |
||
131 | |||
132 | public function addMenuAsItem($menu, $header=null) { |
||
133 | return $this->addItem($this->generateMenuAsItem($menu, $header)); |
||
134 | } |
||
135 | |||
136 | public function addPopupAsItem($value, $identifier, $content="") { |
||
144 | } |
||
145 | |||
146 | public function addDropdownAsItem($value, $items=NULL) { |
||
147 | $dd=$value; |
||
148 | if (\is_string($value)) { |
||
149 | $dd=new HtmlDropdown("dropdown-" . $this->identifier . "-" . $this->count(), $value, $items); |
||
150 | } |
||
151 | $this->addItem($dd); |
||
152 | return $dd; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * |
||
157 | * {@inheritDoc} |
||
158 | * |
||
159 | * @see HtmlCollection::createItem() |
||
160 | */ |
||
161 | protected function createItem($value) { |
||
162 | $itemO=new HtmlMenuItem($this->identifier."-item-" . \sizeof($this->content),""); |
||
163 | $itemO->setTagName("a"); |
||
164 | $itemO->setContent($value); |
||
165 | return $itemO; |
||
166 | } |
||
167 | |||
168 | public function setSecondary($value=true) { |
||
169 | if($value) |
||
170 | $this->addToProperty("class", "secondary"); |
||
171 | else |
||
172 | $this->removePropertyValue("class", "secondary"); |
||
173 | return $this; |
||
174 | } |
||
175 | |||
176 | public function setVertical() { |
||
177 | return $this->addToPropertyCtrl("class", "vertical", array ("vertical" )); |
||
178 | } |
||
179 | |||
180 | public function setPosition($value="right") { |
||
181 | return $this->addToPropertyCtrl("class", $value, array ("right","left" )); |
||
182 | } |
||
183 | |||
184 | public function setPointing($value=Direction::NONE) { |
||
185 | return $this->addToPropertyCtrl("class", $value . " pointing", Direction::getConstantValues("pointing")); |
||
186 | } |
||
187 | |||
188 | public function asTab($vertical=false) { |
||
189 | $this->apply(function (HtmlDoubleElement &$item) { |
||
190 | $item->setTagName("a"); |
||
191 | }); |
||
192 | if ($vertical === true) |
||
193 | $this->setVertical(); |
||
194 | return $this->addToProperty("class", "tabular"); |
||
195 | } |
||
196 | |||
197 | public function asPagination() { |
||
202 | } |
||
203 | |||
204 | public function setFixed() { |
||
205 | return $this->addToProperty("class", "fixed"); |
||
206 | } |
||
207 | |||
208 | public function setFluid() { |
||
209 | return $this->addToProperty("class", "fluid"); |
||
210 | } |
||
211 | |||
212 | public function setCompact() { |
||
214 | } |
||
215 | |||
216 | /* |
||
217 | * (non-PHPdoc) |
||
218 | * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
||
219 | */ |
||
220 | public function fromDatabaseObject($object, $function) { |
||
221 | $return=$function($object); |
||
222 | if (\is_array($return)) |
||
223 | $this->addItems($return); |
||
224 | else |
||
225 | $this->addItem($return); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Defines the menu width |
||
230 | * @param int $width |
||
231 | * @return \Ajax\semantic\html\collections\menus\HtmlMenu |
||
232 | */ |
||
233 | public function setWidth($width) { |
||
234 | if (\is_int($width)) { |
||
|
|||
235 | $width=Wide::getConstants()["W" . $width]; |
||
236 | } |
||
237 | $this->addToPropertyCtrl("class", $width, Wide::getConstants()); |
||
238 | return $this->addToPropertyCtrl("class", "item", array ("item" )); |
||
239 | } |
||
240 | |||
241 | public function addImage($identifier, $src="", $alt="") { |
||
242 | return $this->addItem(new HtmlImg($identifier, $src, $alt)); |
||
243 | } |
||
244 | |||
245 | public static function vertical($identifier, $items=array()) { |
||
247 | } |
||
248 | |||
249 | public function getItemHeader() { |
||
250 | return $this->_itemHeader; |
||
251 | } |
||
252 | |||
253 | public function setHasContainer(){ |
||
254 | return $this->wrapContent("<div class='ui container'>","</div>"); |
||
255 | } |
||
256 | |||
257 | public function run(JsUtils $js){ |
||
258 | if($this->identifier!=="" && !isset($this->_bsComponent)) |
||
259 | $this->onClick('if(!$(this).hasClass("dropdown")&&!$(this).hasClass("no-active")){$(this).addClass("active").siblings().removeClass("active");}',false,false); |
||
260 | $result= parent::run($js); |
||
261 | return $result->setItemSelector(">.item:not(.header)"); |
||
262 | } |
||
263 | } |
||
264 |