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 _getValue($property,$index){ |
||
97 | |||
98 | public function insertField($index,$field){ |
||
102 | |||
103 | public function insertInField($index,$field){ |
||
116 | |||
117 | public function addField($field){ |
||
121 | |||
122 | public function count(){ |
||
125 | |||
126 | public function visiblePropertiesCount(){ |
||
129 | |||
130 | public function getProperty($index){ |
||
133 | |||
134 | public function getFieldName($index){ |
||
145 | |||
146 | |||
147 | protected function showableProperty(\ReflectionProperty $rProperty){ |
||
150 | |||
151 | public function setInstance($instance) { |
||
167 | |||
168 | private function setInstanceProperty($property){ |
||
190 | |||
191 | protected function getDefaultProperties(){ |
||
202 | |||
203 | public function setVisibleProperties($visibleProperties) { |
||
207 | |||
208 | public function setValueFunction($index,$callback){ |
||
212 | |||
213 | public function setIdentifierFunction($callback){ |
||
217 | |||
218 | public static function setIndex($index) { |
||
221 | |||
222 | public function getProperties() { |
||
225 | |||
226 | public function getCaption($index){ |
||
237 | |||
238 | public function getCaptions(){ |
||
258 | |||
259 | public function setCaption($index,$caption){ |
||
265 | |||
266 | public function setCaptions($captions) { |
||
270 | |||
271 | /** |
||
272 | * Associates a $callback function after the compilation of the field at $index position |
||
273 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
274 | * @param int $index postion of the compiled field |
||
275 | * @param callable $callback function called after the field compilation |
||
276 | * @return \Ajax\semantic\widgets\datatable\InstanceViewer |
||
277 | */ |
||
278 | public function afterCompile($index,$callback){ |
||
282 | |||
283 | /** |
||
284 | * Defines a callback function to call for modifying captions |
||
285 | * function parameters are $captions: the captions to modify and $instance: the active model instance |
||
286 | * @param callable $captionCallback |
||
287 | * @return \Ajax\semantic\widgets\base\InstanceViewer |
||
288 | */ |
||
289 | public function setCaptionCallback($captionCallback) { |
||
293 | |||
294 | public function setDefaultValueFunction($defaultValueFunction) { |
||
298 | |||
299 | |||
300 | } |