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 |
||
| 6 | class InstanceViewer { |
||
| 7 | protected $widgetIdentifier; |
||
| 8 | protected $instance; |
||
| 9 | protected $reflect; |
||
| 10 | protected $properties; |
||
| 11 | protected $visibleProperties; |
||
| 12 | protected $values; |
||
| 13 | protected $afterCompile; |
||
| 14 | protected $captions; |
||
| 15 | protected $captionCallback; |
||
| 16 | protected $defaultValueFunction; |
||
| 17 | |||
| 18 | |||
| 19 | public static $index=0; |
||
| 20 | |||
| 21 | public function __construct($identifier,$instance=NULL,$captions=NULL){ |
||
| 31 | |||
| 32 | public function moveFieldTo($from,$to){ |
||
| 38 | |||
| 39 | public function swapFields($index1,$index2){ |
||
| 45 | |||
| 46 | public function removeField($index){ |
||
| 52 | |||
| 53 | public function getValues(){ |
||
| 62 | |||
| 63 | public function getIdentifier($index=NULL){ |
||
| 71 | |||
| 72 | public function getValue($index){ |
||
| 76 | |||
| 77 | protected function _beforeAddProperty($index,&$field){ |
||
| 80 | |||
| 81 | protected function _getDefaultValue($name,$value,$index){ |
||
| 85 | |||
| 86 | protected function _getPropertyValue(\ReflectionProperty $property,$index){ |
||
| 96 | |||
| 97 | protected function _getValue($property,$index){ |
||
| 122 | |||
| 123 | public function insertField($index,$field){ |
||
| 127 | |||
| 128 | public function insertInField($index,$field){ |
||
| 141 | |||
| 142 | public function addField($field){ |
||
| 146 | |||
| 147 | public function count(){ |
||
| 150 | |||
| 151 | public function visiblePropertiesCount(){ |
||
| 154 | |||
| 155 | public function getProperty($index){ |
||
| 158 | |||
| 159 | public function getFieldName($index){ |
||
| 170 | |||
| 171 | |||
| 172 | protected function showableProperty(\ReflectionProperty $rProperty){ |
||
| 175 | |||
| 176 | public function setInstance($instance) { |
||
| 192 | |||
| 193 | private function setInstanceProperty($property){ |
||
| 215 | |||
| 216 | protected function getDefaultProperties(){ |
||
| 227 | |||
| 228 | public function setVisibleProperties($visibleProperties) { |
||
| 232 | |||
| 233 | public function setValueFunction($index,$callback){ |
||
| 237 | |||
| 238 | public function setIdentifierFunction($callback){ |
||
| 242 | |||
| 243 | public static function setIndex($index) { |
||
| 246 | |||
| 247 | public function getProperties() { |
||
| 250 | |||
| 251 | public function getCaption($index){ |
||
| 262 | |||
| 263 | public function getCaptions(){ |
||
| 283 | |||
| 284 | public function setCaption($index,$caption){ |
||
| 290 | |||
| 291 | public function setCaptions($captions) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Associates a $callback function after the compilation of the field at $index position |
||
| 298 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
| 299 | * @param int $index postion of the compiled field |
||
| 300 | * @param callable $callback function called after the field compilation |
||
| 301 | * @return \Ajax\semantic\widgets\datatable\InstanceViewer |
||
| 302 | */ |
||
| 303 | public function afterCompile($index,$callback){ |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Defines a callback function to call for modifying captions |
||
| 310 | * function parameters are $captions: the captions to modify and $instance: the active model instance |
||
| 311 | * @param callable $captionCallback |
||
| 312 | * @return \Ajax\semantic\widgets\base\InstanceViewer |
||
| 313 | */ |
||
| 314 | public function setCaptionCallback($captionCallback) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Defines the default function which displays fields value |
||
| 321 | * @param callable $defaultValueFunction function parameters are : $name : the field name, $value : the field value ,$index : the field index, $instance : the active instance of model |
||
| 322 | * @return \Ajax\semantic\widgets\base\InstanceViewer |
||
| 323 | */ |
||
| 324 | public function setDefaultValueFunction($defaultValueFunction) { |
||
| 328 | } |