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 |
||
7 | class InstanceViewer { |
||
8 | protected $widgetIdentifier; |
||
9 | protected $instance; |
||
10 | protected $reflect; |
||
11 | protected $properties; |
||
12 | protected $visibleProperties; |
||
13 | protected $values; |
||
14 | protected $afterCompile; |
||
15 | protected $captions; |
||
16 | protected $captionCallback; |
||
17 | protected $defaultValueFunction; |
||
18 | |||
19 | |||
20 | public static $index=0; |
||
21 | |||
22 | public function __construct($identifier,$instance=NULL,$captions=NULL){ |
||
32 | |||
33 | public function moveFieldTo($from,$to){ |
||
39 | |||
40 | public function swapFields($index1,$index2){ |
||
46 | |||
47 | public function removeField($index){ |
||
53 | |||
54 | public function getValues(){ |
||
63 | |||
64 | public function getIdentifier($index=NULL){ |
||
76 | |||
77 | public function getValue($index){ |
||
81 | |||
82 | protected function _beforeAddProperty($index,&$field){ |
||
85 | |||
86 | protected function _getDefaultValue($name,$value,$index){ |
||
90 | |||
91 | protected function _getPropertyValue(\ReflectionProperty $property,$index){ |
||
95 | |||
96 | protected function _getValue($property,$index){ |
||
117 | |||
118 | protected function _postGetValue($index,$propertyName,$value){ |
||
131 | |||
132 | public function insertField($index,$field){ |
||
136 | |||
137 | public function insertInField($index,$field){ |
||
150 | |||
151 | public function addField($field){ |
||
155 | |||
156 | public function count(){ |
||
159 | |||
160 | public function visiblePropertiesCount(){ |
||
163 | |||
164 | public function getProperty($index){ |
||
167 | |||
168 | public function getFieldName($index){ |
||
179 | |||
180 | |||
181 | protected function showableProperty(\ReflectionProperty $rProperty){ |
||
184 | |||
185 | public function setInstance($instance) { |
||
201 | |||
202 | private function setInstanceProperty($property){ |
||
224 | |||
225 | protected function getDefaultProperties(){ |
||
236 | |||
237 | public function setVisibleProperties($visibleProperties) { |
||
241 | |||
242 | public function setValueFunction($index,$callback){ |
||
246 | |||
247 | public function setIdentifierFunction($callback){ |
||
251 | |||
252 | public static function setIndex($index) { |
||
255 | |||
256 | public function getProperties() { |
||
259 | |||
260 | public function getCaption($index){ |
||
271 | |||
272 | public function getCaptions(){ |
||
292 | |||
293 | public function setCaption($index,$caption){ |
||
299 | |||
300 | public function setCaptions($captions) { |
||
304 | |||
305 | /** |
||
306 | * Associates a $callback function after the compilation of the field at $index position |
||
307 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
308 | * @param int $index postion of the compiled field |
||
309 | * @param callable $callback function called after the field compilation |
||
310 | * @return \Ajax\semantic\widgets\datatable\InstanceViewer |
||
311 | */ |
||
312 | public function afterCompile($index,$callback){ |
||
316 | |||
317 | /** |
||
318 | * Defines a callback function to call for modifying captions |
||
319 | * function parameters are $captions: the captions to modify and $instance: the active model instance |
||
320 | * @param callable $captionCallback |
||
321 | * @return \Ajax\semantic\widgets\base\InstanceViewer |
||
322 | */ |
||
323 | public function setCaptionCallback($captionCallback) { |
||
327 | |||
328 | /** |
||
329 | * Defines the default function which displays fields value |
||
330 | * @param callable $defaultValueFunction function parameters are : $name : the field name, $value : the field value ,$index : the field index, $instance : the active instance of model |
||
331 | * @return \Ajax\semantic\widgets\base\InstanceViewer |
||
332 | */ |
||
333 | public function setDefaultValueFunction($defaultValueFunction) { |
||
337 | |||
338 | public function getVisibleProperties() { |
||
341 | |||
342 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.