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 | private function _getValue($property,$index){ |
||
70 | |||
71 | public function insertField($index,$field){ |
||
75 | |||
76 | public function insertInField($index,$field){ |
||
89 | |||
90 | public function addField($field){ |
||
94 | |||
95 | public function count(){ |
||
98 | |||
99 | public function visiblePropertiesCount(){ |
||
102 | |||
103 | private function showableProperty(\ReflectionProperty $rProperty){ |
||
106 | |||
107 | public function setInstance($instance) { |
||
140 | |||
141 | private function getDefaultProperties(){ |
||
152 | |||
153 | public function setVisibleProperties($visibleProperties) { |
||
157 | |||
158 | public function setValueFunction($index,$callback){ |
||
162 | |||
163 | public function setIdentifierFunction($callback){ |
||
167 | |||
168 | public static function setIndex($index) { |
||
171 | |||
172 | public function getProperties() { |
||
175 | |||
176 | public function getCaption($index){ |
||
184 | |||
185 | public function getCaptions(){ |
||
201 | |||
202 | public function setCaptions($captions) { |
||
206 | |||
207 | /** |
||
208 | * Associates a $callback function after the compilation of the field at $index position |
||
209 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
210 | * @param int $index postion of the compiled field |
||
211 | * @param callable $callback function called after the field compilation |
||
212 | * @return \Ajax\semantic\widgets\datatable\InstanceViewer |
||
213 | */ |
||
214 | public function afterCompile($index,$callback){ |
||
218 | } |