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 InstanceViewer 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 InstanceViewer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class InstanceViewer { |
||
6 | protected $instance; |
||
7 | protected $reflect; |
||
8 | protected $properties; |
||
9 | protected $visibleProperties; |
||
10 | protected $values; |
||
11 | protected $afterCompile; |
||
12 | protected $captions; |
||
13 | |||
14 | |||
15 | public static $index=0; |
||
16 | |||
17 | public function __construct($instance=NULL,$captions=NULL){ |
||
24 | |||
25 | public function getValues(){ |
||
34 | |||
35 | public function getIdentifier(){ |
||
41 | |||
42 | public function getValue($index){ |
||
46 | |||
47 | protected function _beforeAddProperty($index,&$field){ |
||
50 | |||
51 | protected function _getDefaultValue($name,$value,$index){ |
||
54 | |||
55 | protected function _getValue($property,$index){ |
||
85 | |||
86 | public function insertField($index,$field){ |
||
90 | |||
91 | public function insertInField($index,$field){ |
||
104 | |||
105 | public function addField($field){ |
||
109 | |||
110 | public function count(){ |
||
113 | |||
114 | public function visiblePropertiesCount(){ |
||
117 | |||
118 | public function getProperty($index){ |
||
121 | |||
122 | protected function showableProperty(\ReflectionProperty $rProperty){ |
||
125 | |||
126 | public function setInstance($instance) { |
||
161 | |||
162 | protected function getDefaultProperties(){ |
||
173 | |||
174 | public function setVisibleProperties($visibleProperties) { |
||
178 | |||
179 | public function setValueFunction($index,$callback){ |
||
183 | |||
184 | public function setIdentifierFunction($callback){ |
||
188 | |||
189 | public static function setIndex($index) { |
||
192 | |||
193 | public function getProperties() { |
||
196 | |||
197 | public function getCaption($index){ |
||
208 | |||
209 | public function getCaptions(){ |
||
225 | |||
226 | public function setCaption($index,$caption){ |
||
232 | |||
233 | public function setCaptions($captions) { |
||
237 | |||
238 | /** |
||
239 | * Associates a $callback function after the compilation of the field at $index position |
||
240 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
241 | * @param int $index postion of the compiled field |
||
242 | * @param callable $callback function called after the field compilation |
||
243 | * @return \Ajax\semantic\widgets\datatable\InstanceViewer |
||
244 | */ |
||
245 | public function afterCompile($index,$callback){ |
||
249 | } |
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.