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:
Complex classes like BaseHtml 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 BaseHtml, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | abstract class BaseHtml extends BaseWidget { |
||
18 | use BaseHtmlEventsTrait,BaseHtmlPropertiesTrait; |
||
19 | protected $_template; |
||
20 | protected $tagName; |
||
21 | protected $_wrapBefore=array (); |
||
22 | protected $_wrapAfter=array (); |
||
23 | protected $_bsComponent; |
||
24 | protected $_compiled=false; |
||
25 | |||
26 | /** |
||
27 | * |
||
28 | * @param JsUtils $js |
||
29 | * @return SimpleExtComponent |
||
30 | */ |
||
31 | abstract public function run(JsUtils $js); |
||
32 | |||
33 | private function _callSetter($setter,$key,$value,&$array){ |
||
34 | $result=false; |
||
35 | if (method_exists($this, $setter) && !JString::startswith($key, "_")) { |
||
36 | try { |
||
37 | $this->$setter($value); |
||
38 | unset($array[$key]); |
||
39 | $result=true; |
||
40 | } catch ( \Exception $e ) { |
||
41 | $result=false; |
||
42 | } |
||
43 | } |
||
44 | return $result; |
||
45 | } |
||
46 | |||
47 | protected function getTemplate(JsUtils $js=NULL) { |
||
48 | return PropertyWrapper::wrap($this->_wrapBefore, $js) . $this->_template . PropertyWrapper::wrap($this->_wrapAfter, $js); |
||
49 | } |
||
50 | |||
51 | protected function ctrl($name, $value, $typeCtrl) { |
||
52 | View Code Duplication | if (\is_array($typeCtrl)) { |
|
|
|||
53 | if (array_search($value, $typeCtrl) === false) { |
||
54 | throw new \Exception("La valeur passée `" . $value . "` à la propriété `" . $name . "` ne fait pas partie des valeurs possibles : {" . implode(",", $typeCtrl) . "}"); |
||
55 | } |
||
56 | } else { |
||
57 | if (!$typeCtrl($value)) { |
||
58 | throw new \Exception("La fonction " . $typeCtrl . " a retourné faux pour l'affectation de la propriété " . $name); |
||
59 | } |
||
60 | } |
||
61 | return true; |
||
62 | } |
||
63 | |||
64 | |||
65 | |||
66 | protected function setMemberCtrl(&$name, $value, $typeCtrl) { |
||
67 | if ($this->ctrl($name, $value, $typeCtrl) === true) { |
||
68 | return $name=$value; |
||
69 | } |
||
70 | return $this; |
||
71 | } |
||
72 | |||
73 | protected function addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ") { |
||
74 | if (\is_array($typeCtrl)) { |
||
75 | $this->removeOldValues($name, $typeCtrl); |
||
76 | $name.=$separator . $value; |
||
77 | } |
||
78 | return $this; |
||
79 | } |
||
80 | |||
81 | |||
82 | |||
83 | protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") { |
||
84 | if ($this->ctrl($name, $value, $typeCtrl) === true) { |
||
85 | if (\is_array($typeCtrl)) { |
||
86 | $this->removeOldValues($name, $typeCtrl); |
||
87 | } |
||
88 | $name.=$separator . $value; |
||
89 | } |
||
90 | return $this; |
||
91 | } |
||
92 | |||
93 | protected function addToMember(&$name, $value, $separator=" ") { |
||
94 | $name=str_ireplace($value, "", $name) . $separator . $value; |
||
95 | return $this; |
||
96 | } |
||
97 | |||
98 | |||
99 | |||
100 | protected function removeOldValues(&$oldValue, $allValues) { |
||
101 | $oldValue=str_ireplace($allValues, "", $oldValue); |
||
102 | $oldValue=trim($oldValue); |
||
103 | } |
||
104 | |||
105 | protected function _getElementBy($callback,$elements){ |
||
106 | if (\is_array($elements)) { |
||
107 | $elements=\array_values($elements); |
||
108 | $flag=false; |
||
109 | $index=0; |
||
110 | while ( !$flag && $index < sizeof($elements) ) { |
||
111 | if ($elements[$index] instanceof BaseHtml) |
||
112 | $flag=($callback($elements[$index])); |
||
113 | $index++; |
||
114 | } |
||
115 | if ($flag === true) |
||
116 | return $elements[$index - 1]; |
||
117 | } elseif ($elements instanceof BaseHtml) { |
||
118 | if ($callback($elements)) |
||
119 | return $elements; |
||
120 | } |
||
121 | return null; |
||
122 | } |
||
123 | |||
124 | protected function setWrapBefore($wrapBefore) { |
||
125 | $this->_wrapBefore=$wrapBefore; |
||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | protected function setWrapAfter($wrapAfter) { |
||
130 | $this->_wrapAfter=$wrapAfter; |
||
131 | return $this; |
||
132 | } |
||
133 | |||
134 | public function getTagName() { |
||
135 | return $this->tagName; |
||
136 | } |
||
137 | |||
138 | public function setTagName($tagName) { |
||
139 | $this->tagName=$tagName; |
||
140 | return $this; |
||
141 | } |
||
142 | |||
143 | public function fromArray($array) { |
||
144 | View Code Duplication | foreach ( $this as $key => $value ) { |
|
145 | if(array_key_exists($key, $array)===true) |
||
146 | $this->_callSetter("set" . ucfirst($key), $key, $array[$key], $array); |
||
147 | } |
||
148 | View Code Duplication | foreach ( $array as $key => $value ) { |
|
149 | if($this->_callSetter($key, $key, $value, $array)===false){ |
||
150 | $this->_callSetter("set" . ucfirst($key), $key, $value, $array); |
||
151 | } |
||
152 | } |
||
153 | return $array; |
||
154 | } |
||
155 | |||
156 | public function fromDatabaseObjects($objects, $function) { |
||
164 | |||
165 | public function fromDatabaseObject($object, $function) { |
||
167 | |||
168 | public function wrap($before, $after="") { |
||
175 | |||
176 | |||
177 | |||
178 | public function getElementById($identifier, $elements) { |
||
181 | |||
182 | public function getBsComponent() { |
||
183 | return $this->_bsComponent; |
||
184 | } |
||
185 | |||
186 | public function setBsComponent($bsComponent) { |
||
187 | $this->_bsComponent=$bsComponent; |
||
188 | return $this; |
||
189 | } |
||
190 | |||
191 | protected function compile_once(JsUtils $js=NULL, &$view=NULL) { |
||
192 | if(!$this->_compiled){ |
||
193 | if(isset($js)){ |
||
194 | $beforeCompile=$js->getParam("beforeCompileHtml"); |
||
195 | if(\is_callable($beforeCompile)){ |
||
196 | $beforeCompile($this,$js,$view); |
||
197 | } |
||
198 | } |
||
202 | |||
203 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
224 | |||
225 | public function __toString() { |
||
228 | } |
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.