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 | protected $_postCompile; |
||
26 | protected $_preCompile; |
||
27 | |||
28 | /** |
||
29 | * |
||
30 | * @param JsUtils $js |
||
31 | * @return SimpleExtComponent |
||
32 | */ |
||
33 | abstract public function run(JsUtils $js); |
||
34 | |||
35 | private function _callSetter($setter,$key,$value,&$array){ |
||
48 | |||
49 | protected function getTemplate(JsUtils $js=NULL) { |
||
52 | |||
53 | protected function ctrl($name, $value, $typeCtrl) { |
||
65 | |||
66 | |||
67 | |||
68 | protected function setMemberCtrl(&$name, $value, $typeCtrl) { |
||
74 | |||
75 | protected function addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ") { |
||
82 | |||
83 | |||
84 | |||
85 | protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") { |
||
94 | |||
95 | protected function addToMember(&$name, $value, $separator=" ") { |
||
99 | |||
100 | |||
101 | |||
102 | protected function removeOldValues(&$oldValue, $allValues) { |
||
106 | |||
107 | protected function _getElementBy($callback,$elements){ |
||
125 | |||
126 | protected function setWrapBefore($wrapBefore) { |
||
130 | |||
131 | protected function setWrapAfter($wrapAfter) { |
||
135 | |||
136 | public function getTagName() { |
||
139 | |||
140 | public function setTagName($tagName) { |
||
144 | |||
145 | public function fromArray($array) { |
||
157 | |||
158 | public function fromDatabaseObjects($objects, $function) { |
||
166 | |||
167 | public function fromDatabaseObject($object, $function) { |
||
169 | |||
170 | public function wrap($before, $after="") { |
||
177 | |||
178 | |||
179 | |||
180 | public function getElementById($identifier, $elements) { |
||
183 | |||
184 | public function getBsComponent() { |
||
187 | |||
188 | public function setBsComponent($bsComponent) { |
||
192 | |||
193 | protected function compile_once(JsUtils $js=NULL, &$view=NULL) { |
||
209 | |||
210 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
239 | |||
240 | public function __toString() { |
||
243 | |||
244 | public function onPostCompile($callback){ |
||
247 | |||
248 | public function onPreCompile($callback){ |
||
251 | } |
||
252 |
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.