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 |
||
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() { |
||
44 | |||
45 | public function setHeader($header) { |
||
46 | $this->header=$header; |
||
47 | return $this; |
||
48 | } |
||
49 | |||
50 | public function getFooter() { |
||
53 | |||
54 | public function setFooter($footer) { |
||
58 | |||
59 | View Code Duplication | public function addHeader($content) { |
|
|
|||
60 | $header=new HtmlBsDoubleElement("header-".$this->identifier); |
||
67 | |||
68 | View Code Duplication | public function addHeaderH($content, $niveau="1") { |
|
75 | |||
76 | View Code Duplication | public function addFooter($content) { |
|
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) { |
||
96 | |||
97 | /* |
||
98 | * (non-PHPdoc) |
||
99 | * @see BaseHtml::run() |
||
100 | */ |
||
101 | public function run(JsUtils $js) { |
||
111 | |||
112 | public function setCollapsable($_collapsable) { |
||
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) { |
||
138 | } |
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.