HtmlPanel   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 123
Duplicated Lines 18.7 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 20
c 0
b 0
f 0
lcom 1
cbo 7
dl 23
loc 123
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeader() 0 3 1
A getFooter() 0 3 1
A setFooter() 0 4 1
A __construct() 0 16 4
A setHeader() 0 4 1
A addHeader() 8 8 1
A addHeaderH() 7 7 1
A addFooter() 8 8 1
A setStyle() 0 5 2
A run() 0 10 3
A setCollapsable() 0 16 2
A show() 0 4 2

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\bootstrap\html;
4
5
use Ajax\bootstrap\html\base\HtmlBsDoubleElement;
6
use Ajax\bootstrap\html\base\CssRef;
7
use Ajax\JsUtils;
8
use Ajax\service\PhalconUtils;
9
10
/**
11
 * Composant Twitter Bootstrap panel
12
 * @see http://getbootstrap.com/components/#panels
13
 * @author jc
14
 * @version 1.001
15
 */
16
class HtmlPanel extends HtmlBsDoubleElement {
17
	protected $header;
18
	protected $footer;
19
	protected $_collapsable;
20
	protected $collapseBegin;
21
	protected $collapseEnd;
22
	protected $_showOnStartup;
23
24
	public function __construct($identifier, $content=NULL, $header=NULL, $footer=NULL) {
25
		parent::__construct($identifier, "div");
26
		$this->_template=include 'templates/tplPanel.php';
27
		$this->setProperty("class", "panel panel-default");
28
		$this->_collapsable=false;
29
		$this->_showOnStartup=false;
30
		if ($content!==NULL) {
31
			$this->setContent($content);
32
		}
33
		if ($header!==NULL) {
34
			$this->addHeader($header);
35
		}
36
		if ($footer!==NULL) {
37
			$this->addFooter($footer);
38
		}
39
	}
40
41
	public function getHeader() {
42
		return $this->header;
43
	}
44
45
	public function setHeader($header) {
46
		$this->header=$header;
47
		return $this;
48
	}
49
50
	public function getFooter() {
51
		return $this->footer;
52
	}
53
54
	public function setFooter($footer) {
55
		$this->footer=$footer;
56
		return $this;
57
	}
58
59 View Code Duplication
	public function addHeader($content) {
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...
60
		$header=new HtmlBsDoubleElement("header-".$this->identifier);
61
		$header->setTagName("div");
62
		$header->setClass("panel-heading");
63
		$header->setContent($content);
64
		$this->header=$header;
65
		return $header;
66
	}
67
68 View Code Duplication
	public function addHeaderH($content, $niveau="1") {
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...
69
		$headerH=new HtmlBsDoubleElement("header-h-".$this->identifier);
70
		$headerH->setContent($content);
71
		$headerH->setTagName("h".$niveau);
72
		$headerH->setClass("panel-title");
73
		return $this->addHeader($headerH);
74
	}
75
76 View Code Duplication
	public function addFooter($content) {
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...
77
		$footer=new HtmlBsDoubleElement("footer-".$this->identifier);
78
		$footer->setTagName("div");
79
		$footer->setClass("panel-footer");
80
		$footer->setContent($content);
81
		$this->footer=$footer;
82
		return $this;
83
	}
84
85
	/**
86
	 * define the Panel style
87
	 * avaible values : "panel-default","panel-primary","panel-success","panel-info","panel-warning","panel-danger"
88
	 * @param string|int $cssStyle
89
	 * @return \Ajax\bootstrap\html\HtmlPanel default : "panel-default"
90
	 */
91
	public function setStyle($cssStyle) {
92
		if (!PhalconUtils::startsWith($cssStyle, "panel"))
93
			$cssStyle="panel".$cssStyle;
94
		return $this->addToPropertyCtrl("class", $cssStyle, CssRef::Styles("panel"));
95
	}
96
97
	/*
98
	 * (non-PHPdoc)
99
	 * @see BaseHtml::run()
100
	 */
101
	public function run(JsUtils $js) {
102
		if ($this->_collapsable) {
103
			$this->_bsComponent=$js->bootstrap()->collapse("#lnk-".$this->identifier);
104
			$this->_bsComponent->setCollapsed("#collapse-".$this->identifier);
105
			if ($this->_showOnStartup===true) {
106
				$this->_bsComponent->show();
107
			}
108
		}
109
		return $this->_bsComponent;
110
	}
111
112
	public function setCollapsable($_collapsable) {
113
		$this->_collapsable=$_collapsable;
114
		if ($_collapsable) {
115
			$this->header->setRole("tab");
116
			$lnk=new HtmlLink("lnk-".$this->identifier);
117
			$lnk->setHref("#collapse-".$this->identifier);
118
			$lnk->setContent($this->header->getContent());
119
			$this->header->setContent($lnk);
120
			$this->collapseBegin='<div id="collapse-'.$this->identifier.'" class="panel-collapse collapse" role="tabpanel" aria-labelledby="header-'.$this->identifier.'">';
121
			$this->collapseEnd="</div>";
122
		} else {
123
			$this->collapseBegin="";
124
			$this->collapseEnd="";
125
		}
126
		return $this;
127
	}
128
129
	/**
130
	 * Shows the panel body on startup if panel is collapsable.
131
	 * @param boolean $value
132
	 * @return $this default : false
133
	 */
134
	public function show($value) {
135
		if ($this->_collapsable)
136
			$this->_showOnStartup=$value;
137
	}
138
}