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){ |
||
| 80 | |||
| 81 | public function insertField($index,$field){ |
||
| 85 | |||
| 86 | public function insertInField($index,$field){ |
||
| 99 | |||
| 100 | public function addField($field){ |
||
| 104 | |||
| 105 | public function count(){ |
||
| 108 | |||
| 109 | public function visiblePropertiesCount(){ |
||
| 112 | |||
| 113 | public function getProperty($index){ |
||
| 116 | |||
| 117 | protected function showableProperty(\ReflectionProperty $rProperty){ |
||
| 120 | |||
| 121 | public function setInstance($instance) { |
||
| 156 | |||
| 157 | protected function getDefaultProperties(){ |
||
| 168 | |||
| 169 | public function setVisibleProperties($visibleProperties) { |
||
| 173 | |||
| 174 | public function setValueFunction($index,$callback){ |
||
| 178 | |||
| 179 | public function setIdentifierFunction($callback){ |
||
| 183 | |||
| 184 | public static function setIndex($index) { |
||
| 187 | |||
| 188 | public function getProperties() { |
||
| 191 | |||
| 192 | public function getCaption($index){ |
||
| 200 | |||
| 201 | public function getCaptions(){ |
||
| 217 | |||
| 218 | public function setCaptions($captions) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Associates a $callback function after the compilation of the field at $index position |
||
| 225 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
| 226 | * @param int $index postion of the compiled field |
||
| 227 | * @param callable $callback function called after the field compilation |
||
| 228 | * @return \Ajax\semantic\widgets\datatable\InstanceViewer |
||
| 229 | */ |
||
| 230 | public function afterCompile($index,$callback){ |
||
| 234 | } |