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 $widgetIdentifier; |
||
7 | protected $instance; |
||
8 | protected $reflect; |
||
9 | protected $properties; |
||
10 | protected $visibleProperties; |
||
11 | protected $values; |
||
12 | protected $afterCompile; |
||
13 | protected $captions; |
||
14 | protected $captionCallback; |
||
15 | protected $defaultValueFunction; |
||
16 | |||
17 | |||
18 | public static $index=0; |
||
19 | |||
20 | public function __construct($identifier,$instance=NULL,$captions=NULL){ |
||
30 | |||
31 | public function getValues(){ |
||
40 | |||
41 | public function getIdentifier($index=NULL){ |
||
49 | |||
50 | public function getValue($index){ |
||
54 | |||
55 | protected function _beforeAddProperty($index,&$field){ |
||
58 | |||
59 | protected function _getDefaultValue($name,$value,$index){ |
||
63 | |||
64 | protected function _getPropertyValue(\ReflectionProperty $property,$index){ |
||
74 | |||
75 | protected function _getValue($property,$index){ |
||
100 | |||
101 | public function insertField($index,$field){ |
||
105 | |||
106 | public function insertInField($index,$field){ |
||
119 | |||
120 | public function addField($field){ |
||
124 | |||
125 | public function count(){ |
||
128 | |||
129 | public function visiblePropertiesCount(){ |
||
132 | |||
133 | public function getProperty($index){ |
||
136 | |||
137 | public function getFieldName($index){ |
||
148 | |||
149 | |||
150 | protected function showableProperty(\ReflectionProperty $rProperty){ |
||
153 | |||
154 | public function setInstance($instance) { |
||
170 | |||
171 | private function setInstanceProperty($property){ |
||
193 | |||
194 | protected function getDefaultProperties(){ |
||
205 | |||
206 | public function setVisibleProperties($visibleProperties) { |
||
210 | |||
211 | public function setValueFunction($index,$callback){ |
||
215 | |||
216 | public function setIdentifierFunction($callback){ |
||
220 | |||
221 | public static function setIndex($index) { |
||
224 | |||
225 | public function getProperties() { |
||
228 | |||
229 | public function getCaption($index){ |
||
240 | |||
241 | public function getCaptions(){ |
||
261 | |||
262 | public function setCaption($index,$caption){ |
||
268 | |||
269 | public function setCaptions($captions) { |
||
273 | |||
274 | /** |
||
275 | * Associates a $callback function after the compilation of the field at $index position |
||
276 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
277 | * @param int $index postion of the compiled field |
||
278 | * @param callable $callback function called after the field compilation |
||
279 | * @return \Ajax\semantic\widgets\datatable\InstanceViewer |
||
280 | */ |
||
281 | public function afterCompile($index,$callback){ |
||
285 | |||
286 | /** |
||
287 | * Defines a callback function to call for modifying captions |
||
288 | * function parameters are $captions: the captions to modify and $instance: the active model instance |
||
289 | * @param callable $captionCallback |
||
290 | * @return \Ajax\semantic\widgets\base\InstanceViewer |
||
291 | */ |
||
292 | public function setCaptionCallback($captionCallback) { |
||
296 | |||
297 | /** |
||
298 | * Defines the default function which displays fields value |
||
299 | * @param callable $defaultValueFunction function parameters are : $name : the field name, $value : the field value ,$index : the field index, $instance : the active instance of model |
||
300 | * @return \Ajax\semantic\widgets\base\InstanceViewer |
||
301 | */ |
||
302 | public function setDefaultValueFunction($defaultValueFunction) { |
||
306 | |||
307 | |||
308 | } |