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 $widgetIdentifier; |
||
| 7 | protected $instance; |
||
| 8 | protected $reflect; |
||
| 9 | protected $properties; |
||
| 10 | protected $visibleProperties; |
||
| 11 | protected $values; |
||
| 12 | protected $afterCompile; |
||
| 13 | protected $captions; |
||
| 14 | protected $captionCallback; |
||
| 15 | protected $defaultValueFunction; |
||
| 16 | |||
| 17 | |||
| 18 | public static $index=0; |
||
| 19 | |||
| 20 | public function __construct($identifier,$instance=NULL,$captions=NULL){ |
||
| 30 | |||
| 31 | public function getValues(){ |
||
| 40 | |||
| 41 | public function getIdentifier(){ |
||
| 47 | |||
| 48 | public function getValue($index){ |
||
| 52 | |||
| 53 | protected function _beforeAddProperty($index,&$field){ |
||
| 56 | |||
| 57 | protected function _getDefaultValue($name,$value,$index){ |
||
| 61 | |||
| 62 | protected function _getValue($property,$index){ |
||
| 92 | |||
| 93 | public function insertField($index,$field){ |
||
| 97 | |||
| 98 | public function insertInField($index,$field){ |
||
| 111 | |||
| 112 | public function addField($field){ |
||
| 116 | |||
| 117 | public function count(){ |
||
| 120 | |||
| 121 | public function visiblePropertiesCount(){ |
||
| 124 | |||
| 125 | public function getProperty($index){ |
||
| 128 | |||
| 129 | protected function showableProperty(\ReflectionProperty $rProperty){ |
||
| 132 | |||
| 133 | public function setInstance($instance) { |
||
| 149 | |||
| 150 | private function setInstanceProperty($property){ |
||
| 172 | |||
| 173 | protected function getDefaultProperties(){ |
||
| 184 | |||
| 185 | public function setVisibleProperties($visibleProperties) { |
||
| 189 | |||
| 190 | public function setValueFunction($index,$callback){ |
||
| 194 | |||
| 195 | public function setIdentifierFunction($callback){ |
||
| 199 | |||
| 200 | public static function setIndex($index) { |
||
| 203 | |||
| 204 | public function getProperties() { |
||
| 207 | |||
| 208 | public function getCaption($index){ |
||
| 219 | |||
| 220 | public function getCaptions(){ |
||
| 240 | |||
| 241 | public function setCaption($index,$caption){ |
||
| 247 | |||
| 248 | public function setCaptions($captions) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Associates a $callback function after the compilation of the field at $index position |
||
| 255 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
| 256 | * @param int $index postion of the compiled field |
||
| 257 | * @param callable $callback function called after the field compilation |
||
| 258 | * @return \Ajax\semantic\widgets\datatable\InstanceViewer |
||
| 259 | */ |
||
| 260 | public function afterCompile($index,$callback){ |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Defines a callback function to call for modifying captions |
||
| 267 | * function parameters are $captions: the captions to modify and $instance: the active model instance |
||
| 268 | * @param callable $captionCallback |
||
| 269 | * @return \Ajax\semantic\widgets\base\InstanceViewer |
||
| 270 | */ |
||
| 271 | public function setCaptionCallback($captionCallback) { |
||
| 275 | |||
| 276 | public function setDefaultValueFunction($defaultValueFunction) { |
||
| 280 | |||
| 281 | |||
| 282 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.