Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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){ |
||
21 | $this->widgetIdentifier=$identifier; |
||
22 | $this->values=[]; |
||
23 | $this->afterCompile=[]; |
||
24 | if(isset($instance)) |
||
25 | $this->setInstance($instance); |
||
26 | $this->setCaptions($captions); |
||
27 | $this->captionCallback=NULL; |
||
28 | $this->defaultValueFunction=function($name,$value){return $value;}; |
||
29 | } |
||
30 | |||
31 | public function getValues(){ |
||
32 | $values=[]; |
||
33 | $index=0; |
||
34 | $count=$this->count(); |
||
35 | while($index<$count){ |
||
36 | $values[]=$this->getValue($index++); |
||
37 | } |
||
38 | return $values; |
||
39 | } |
||
40 | |||
41 | public function getIdentifier(){ |
||
42 | $value=self::$index; |
||
43 | if(isset($this->values["identifier"])) |
||
44 | $value=$this->values["identifier"](self::$index,$this->instance); |
||
45 | return $value; |
||
46 | } |
||
47 | |||
48 | public function getValue($index){ |
||
49 | $property=$this->properties[$index]; |
||
50 | return $this->_getValue($property, $index); |
||
51 | } |
||
52 | |||
53 | protected function _beforeAddProperty($index,&$field){ |
||
56 | |||
57 | protected function _getDefaultValue($name,$value,$index){ |
||
61 | |||
62 | protected function _getValue($property,$index){ |
||
63 | if($property instanceof \ReflectionProperty){ |
||
64 | $property->setAccessible(true); |
||
65 | $value=$property->getValue($this->instance); |
||
66 | View Code Duplication | if(isset($this->values[$index])){ |
|
|
|||
67 | $value= $this->values[$index]($value,$this->instance,$index); |
||
68 | }else{ |
||
69 | $value=$this->_getDefaultValue($property->getName(),$value, $index); |
||
70 | } |
||
71 | }else{ |
||
72 | if(\is_callable($property)) |
||
73 | $value=$property($this->instance); |
||
74 | elseif(\is_array($property)){ |
||
75 | $values=\array_map(function($v) use ($index){return $this->_getValue($v, $index);}, $property); |
||
76 | $value=\implode("", $values); |
||
77 | View Code Duplication | }else{ |
|
78 | if(isset($this->values[$index])){ |
||
79 | $value= $this->values[$index]($property,$this->instance,$index); |
||
80 | }else{ |
||
81 | $value=$property; |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 | if(isset($this->afterCompile[$index])){ |
||
86 | if(\is_callable($this->afterCompile[$index])){ |
||
87 | $this->afterCompile[$index]($value,$this->instance,$index); |
||
88 | } |
||
89 | } |
||
90 | return $value; |
||
91 | } |
||
92 | |||
93 | public function insertField($index,$field){ |
||
94 | array_splice( $this->visibleProperties, $index, 0, $field ); |
||
95 | return $this; |
||
96 | } |
||
97 | |||
98 | public function insertInField($index,$field){ |
||
99 | $vb=$this->visibleProperties; |
||
100 | if(isset($vb[$index])){ |
||
101 | if(\is_array($vb[$index])){ |
||
102 | $this->visibleProperties[$index][]=$field; |
||
103 | }else{ |
||
104 | $this->visibleProperties[$index]=[$vb[$index],$field]; |
||
105 | } |
||
106 | }else{ |
||
107 | return $this->insertField($index, $field); |
||
108 | } |
||
109 | return $this; |
||
110 | } |
||
111 | |||
112 | public function addField($field){ |
||
113 | $this->visibleProperties[]=$field; |
||
114 | return $this; |
||
115 | } |
||
116 | |||
117 | public function count(){ |
||
118 | return \sizeof($this->properties); |
||
119 | } |
||
120 | |||
121 | public function visiblePropertiesCount(){ |
||
122 | return \sizeof($this->visibleProperties); |
||
123 | } |
||
124 | |||
125 | public function getProperty($index){ |
||
128 | |||
129 | public function getFieldName($index){ |
||
130 | $property=$this->getProperty($index); |
||
131 | if($property instanceof \ReflectionProperty){ |
||
132 | $result=$property->getName(); |
||
133 | }elseif(\is_callable($property)){ |
||
134 | $result=$this->visibleProperties[$index]; |
||
135 | }else{ |
||
136 | $result=$property; |
||
137 | } |
||
138 | return $result; |
||
140 | |||
141 | |||
142 | protected function showableProperty(\ReflectionProperty $rProperty){ |
||
145 | |||
146 | public function setInstance($instance) { |
||
162 | |||
163 | private function setInstanceProperty($property){ |
||
185 | |||
186 | protected function getDefaultProperties(){ |
||
197 | |||
198 | public function setVisibleProperties($visibleProperties) { |
||
202 | |||
203 | public function setValueFunction($index,$callback){ |
||
207 | |||
208 | public function setIdentifierFunction($callback){ |
||
212 | |||
213 | public static function setIndex($index) { |
||
216 | |||
217 | public function getProperties() { |
||
220 | |||
221 | public function getCaption($index){ |
||
232 | |||
233 | public function getCaptions(){ |
||
253 | |||
254 | public function setCaption($index,$caption){ |
||
260 | |||
261 | public function setCaptions($captions) { |
||
265 | |||
266 | /** |
||
267 | * Associates a $callback function after the compilation of the field at $index position |
||
268 | * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
||
269 | * @param int $index postion of the compiled field |
||
270 | * @param callable $callback function called after the field compilation |
||
271 | * @return \Ajax\semantic\widgets\datatable\InstanceViewer |
||
272 | */ |
||
273 | public function afterCompile($index,$callback){ |
||
277 | |||
278 | /** |
||
279 | * Defines a callback function to call for modifying captions |
||
280 | * function parameters are $captions: the captions to modify and $instance: the active model instance |
||
281 | * @param callable $captionCallback |
||
282 | * @return \Ajax\semantic\widgets\base\InstanceViewer |
||
283 | */ |
||
284 | public function setCaptionCallback($captionCallback) { |
||
288 | |||
289 | public function setDefaultValueFunction($defaultValueFunction) { |
||
293 | |||
294 | |||
295 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.