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 |
||
| 7 | class InstanceViewer { |
||
| 8 | protected $widgetIdentifier; |
||
| 9 | protected $instance; |
||
| 10 | protected $reflect; |
||
| 11 | protected $properties; |
||
| 12 | protected $visibleProperties; |
||
| 13 | protected $values; |
||
| 14 | protected $afterCompile; |
||
| 15 | protected $captions; |
||
| 16 | protected $captionCallback; |
||
| 17 | protected $defaultValueFunction; |
||
| 18 | |||
| 19 | |||
| 20 | public static $index=0; |
||
| 21 | |||
| 22 | public function __construct($identifier,$instance=NULL,$captions=NULL){ |
||
| 32 | |||
| 33 | public function moveFieldTo($from,$to){ |
||
| 39 | |||
| 40 | public function swapFields($index1,$index2){ |
||
| 46 | |||
| 47 | public function removeField($index){ |
||
| 53 | |||
| 54 | public function getValues(){ |
||
| 63 | |||
| 64 | public function getIdentifier($index=NULL){ |
||
| 76 | |||
| 77 | public function getValue($index){ |
||
| 81 | |||
| 82 | protected function _beforeAddProperty($index,&$field){ |
||
| 85 | |||
| 86 | protected function _getDefaultValue($name,$value,$index){ |
||
| 90 | |||
| 91 | protected function _getPropertyValue(\ReflectionProperty $property,$index){ |
||
| 95 | |||
| 96 | protected function _getValue($property,$index){ |
||
| 117 | |||
| 118 | protected function _postGetValue($index,$propertyName,$value){ |
||
| 131 | |||
| 132 | public function insertField($index,$field){ |
||
| 136 | |||
| 137 | public function insertInField($index,$field){ |
||
| 150 | |||
| 151 | public function addField($field){ |
||
| 155 | |||
| 156 | public function addFields($fields){ |
||
| 160 | |||
| 161 | public function count(){ |
||
| 164 | |||
| 165 | public function visiblePropertiesCount(){ |
||
| 168 | |||
| 169 | public function getProperty($index){ |
||
| 172 | |||
| 173 | public function getFieldName($index){ |
||
| 184 | |||
| 185 | |||
| 186 | protected function showableProperty(\ReflectionProperty $rProperty){ |
||
| 189 | |||
| 190 | public function setInstance($instance) { |
||
| 206 | |||
| 207 | private function setInstanceProperty($property){ |
||
| 229 | |||
| 230 | protected function getDefaultProperties(){ |
||
| 241 | |||
| 242 | public function setVisibleProperties($visibleProperties) { |
||
| 246 | |||
| 247 | public function setValueFunction($index,$callback){ |
||
| 251 | |||
| 252 | public function setIdentifierFunction($callback){ |
||
| 256 | |||
| 257 | public static function setIndex($index) { |
||
| 260 | |||
| 261 | public function getProperties() { |
||
| 264 | |||
| 265 | public function getCaption($index){ |
||
| 276 | |||
| 277 | public function getCaptions(){ |
||
| 298 | |||
| 299 | public function setCaption($index,$caption){ |
||
| 305 | |||
| 306 | public function setCaptions($captions) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Associates a $callback function after the compilation of the field at $index position |
||
| 313 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
| 314 | * @param int $index postion of the compiled field |
||
| 315 | * @param callable $callback function called after the field compilation |
||
| 316 | * @return \Ajax\semantic\widgets\datatable\InstanceViewer |
||
| 317 | */ |
||
| 318 | public function afterCompile($index,$callback){ |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Defines a callback function to call for modifying captions |
||
| 325 | * function parameters are $captions: the captions to modify and $instance: the active model instance |
||
| 326 | * @param callable $captionCallback |
||
| 327 | * @return \Ajax\semantic\widgets\base\InstanceViewer |
||
| 328 | */ |
||
| 329 | public function setCaptionCallback($captionCallback) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Defines the default function which displays fields value |
||
| 336 | * @param callable $defaultValueFunction function parameters are : $name : the field name, $value : the field value ,$index : the field index, $instance : the active instance of model |
||
| 337 | * @return \Ajax\semantic\widgets\base\InstanceViewer |
||
| 338 | */ |
||
| 339 | public function setDefaultValueFunction($defaultValueFunction) { |
||
| 343 | |||
| 344 | public function getVisibleProperties() { |
||
| 347 | |||
| 348 | } |
||
| 349 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.