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 |
||
17 | class HtmlSemDoubleElement extends HtmlDoubleElement { |
||
18 | use BaseTrait; |
||
19 | protected $_popup=NULL; |
||
20 | protected $_dimmer=NULL; |
||
21 | |||
22 | View Code Duplication | public function __construct($identifier, $tagName="p", $baseClass="ui", $content=NULL) { |
|
30 | |||
31 | public function setPopupAttributes($variation=NULL, $popupEvent=NULL) { |
||
35 | |||
36 | public function addPopup($title="", $content="", $variation=NULL, $params=array()) { |
||
40 | |||
41 | public function addPopupHtml($html="", $variation=NULL, $params=array()) { |
||
47 | |||
48 | public function addDimmer($content=NULL) { |
||
53 | |||
54 | public function jsShowDimmer($show=true) { |
||
60 | |||
61 | public function compile(JsUtils $js=NULL, View $view=NULL) { |
||
66 | |||
67 | public function run(JsUtils $js) { |
||
76 | /* |
||
77 | * public function __call($name, $arguments){ |
||
78 | * $type=\substr($name, 0,3); |
||
79 | * $name=\strtolower(\substr($name, 3)); |
||
80 | * $names=\array_merge($this->_variations,$this->_states); |
||
81 | * $argument=@$arguments[0]; |
||
82 | * if(\array_search($name, $names)!==FALSE){ |
||
83 | * switch ($type){ |
||
84 | * case "set": |
||
85 | * if($argument===false){ |
||
86 | * $this->removePropertyValue("class", $name); |
||
87 | * }else { |
||
88 | * $this->setProperty("class", $this->_baseClass." ".$name); |
||
89 | * } |
||
90 | * break; |
||
91 | * case "add": |
||
92 | * $this->addToPropertyCtrl("class", $name,array($name)); |
||
93 | * break; |
||
94 | * default: |
||
95 | * throw new \Exception("Méthode ".$type.$name." inexistante."); |
||
96 | * } |
||
97 | * }else{ |
||
98 | * throw new \Exception("Propriété ".$name." inexistante."); |
||
99 | * } |
||
100 | * return $this; |
||
101 | * } |
||
102 | */ |
||
103 | } |
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.