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){ |
||
| 101 | |||
| 102 | protected function _getValue($property,$index){ |
||
| 127 | |||
| 128 | public function insertField($index,$field){ |
||
| 132 | |||
| 133 | public function insertInField($index,$field){ |
||
| 146 | |||
| 147 | public function addField($field){ |
||
| 151 | |||
| 152 | public function count(){ |
||
| 155 | |||
| 156 | public function visiblePropertiesCount(){ |
||
| 159 | |||
| 160 | public function getProperty($index){ |
||
| 163 | |||
| 164 | public function getFieldName($index){ |
||
| 175 | |||
| 176 | |||
| 177 | protected function showableProperty(\ReflectionProperty $rProperty){ |
||
| 180 | |||
| 181 | public function setInstance($instance) { |
||
| 197 | |||
| 198 | private function setInstanceProperty($property){ |
||
| 220 | |||
| 221 | protected function getDefaultProperties(){ |
||
| 232 | |||
| 233 | public function setVisibleProperties($visibleProperties) { |
||
| 237 | |||
| 238 | public function setValueFunction($index,$callback){ |
||
| 242 | |||
| 243 | public function setIdentifierFunction($callback){ |
||
| 247 | |||
| 248 | public static function setIndex($index) { |
||
| 251 | |||
| 252 | public function getProperties() { |
||
| 255 | |||
| 256 | public function getCaption($index){ |
||
| 267 | |||
| 268 | public function getCaptions(){ |
||
| 288 | |||
| 289 | public function setCaption($index,$caption){ |
||
| 295 | |||
| 296 | public function setCaptions($captions) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Associates a $callback function after the compilation of the field at $index position |
||
| 303 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
| 304 | * @param int $index postion of the compiled field |
||
| 305 | * @param callable $callback function called after the field compilation |
||
| 306 | * @return \Ajax\semantic\widgets\datatable\InstanceViewer |
||
| 307 | */ |
||
| 308 | public function afterCompile($index,$callback){ |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Defines a callback function to call for modifying captions |
||
| 315 | * function parameters are $captions: the captions to modify and $instance: the active model instance |
||
| 316 | * @param callable $captionCallback |
||
| 317 | * @return \Ajax\semantic\widgets\base\InstanceViewer |
||
| 318 | */ |
||
| 319 | public function setCaptionCallback($captionCallback) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Defines the default function which displays fields value |
||
| 326 | * @param callable $defaultValueFunction function parameters are : $name : the field name, $value : the field value ,$index : the field index, $instance : the active instance of model |
||
| 327 | * @return \Ajax\semantic\widgets\base\InstanceViewer |
||
| 328 | */ |
||
| 329 | public function setDefaultValueFunction($defaultValueFunction) { |
||
| 333 | |||
| 334 | public function getVisibleProperties() { |
||
| 337 | |||
| 338 | } |