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 | public static $index=0; |
||
13 | |||
14 | public function __construct($instance=NULL){ |
||
20 | |||
21 | public function getValues(){ |
||
30 | |||
31 | public function getIdentifier(){ |
||
37 | |||
38 | public function getValue($index){ |
||
42 | |||
43 | private function _getValue($property,$index){ |
||
66 | |||
67 | public function insertField($index,$field){ |
||
71 | |||
72 | public function insertInField($index,$field){ |
||
85 | |||
86 | public function addField($field){ |
||
90 | |||
91 | public function count(){ |
||
94 | |||
95 | public function visiblePropertiesCount(){ |
||
98 | |||
99 | private function showableProperty(\ReflectionProperty $rProperty){ |
||
102 | |||
103 | public function setInstance($instance) { |
||
136 | |||
137 | private function getDefaultProperties(){ |
||
148 | |||
149 | public function setVisibleProperties($visibleProperties) { |
||
153 | |||
154 | public function setValueFunction($index,$callback){ |
||
158 | |||
159 | public function setIdentifierFunction($callback){ |
||
163 | |||
164 | public static function setIndex($index) { |
||
167 | |||
168 | public function getProperties() { |
||
171 | |||
172 | /** |
||
173 | * Associates a $callback function after the compilation of the field at $index position |
||
174 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
175 | * @param int $index postion of the compiled field |
||
176 | * @param callable $callback function called after the field compilation |
||
177 | * @return \Ajax\semantic\widgets\datatable\InstanceViewer |
||
178 | */ |
||
179 | public function afterCompile($index,$callback){ |
||
183 | } |