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 | private $instance; |
||
| 7 | private $reflect; |
||
| 8 | private $properties; |
||
| 9 | private $captions; |
||
| 10 | private $visibleProperties; |
||
| 11 | private $values; |
||
| 12 | private $afterCompile; |
||
| 13 | private static $index=0; |
||
| 14 | |||
| 15 | public function __construct($instance=NULL,$captions=NULL){ |
||
| 22 | |||
| 23 | public function getCaption($index){ |
||
| 29 | |||
| 30 | public function getCaptions(){ |
||
| 46 | |||
| 47 | public function getValues(){ |
||
| 56 | |||
| 57 | public function getIdentifier(){ |
||
| 64 | |||
| 65 | public function getValue($index){ |
||
| 69 | |||
| 70 | private function _getValue($property,$index){ |
||
| 93 | |||
| 94 | public function insertField($index,$field){ |
||
| 98 | |||
| 99 | public function insertInField($index,$field){ |
||
| 112 | |||
| 113 | public function addField($field){ |
||
| 117 | |||
| 118 | public function count(){ |
||
| 121 | |||
| 122 | public function visiblePropertiesCount(){ |
||
| 125 | |||
| 126 | private function showableProperty(\ReflectionProperty $rProperty){ |
||
| 129 | |||
| 130 | public function setInstance($instance) { |
||
| 163 | |||
| 164 | private function getDefaultProperties(){ |
||
| 175 | |||
| 176 | public function setCaptions($captions) { |
||
| 180 | |||
| 181 | public function setVisibleProperties($visibleProperties) { |
||
| 185 | |||
| 186 | public function setValueFunction($index,$callback){ |
||
| 190 | |||
| 191 | public function setIdentifierFunction($callback){ |
||
| 195 | |||
| 196 | public static function setIndex($index) { |
||
| 199 | |||
| 200 | public function getProperties() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Associates a $callback function after the compilation of the field at $index position |
||
| 206 | * The $callback function can take the following arguments : $field=>the compiled field, $index: the field position, $instance : the active instance of the object |
||
| 207 | * @param int $index postion of the compiled field |
||
| 208 | * @param callable $callback function called after the field compilation |
||
| 209 | * @return \Ajax\semantic\widgets\datatable\InstanceViewer |
||
| 210 | */ |
||
| 211 | public function afterCompile($index,$callback){ |
||
| 215 | } |