Completed
Push — master ( 3555a5...e61355 )
by Jean-Christophe
03:33
created

InstanceViewer   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 189
Duplicated Lines 4.76 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 42
lcom 1
cbo 1
dl 9
loc 189
rs 8.295
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getCaption() 0 3 1
A getCaptions() 0 16 4
A getValues() 9 9 2
A getCkValue() 0 7 2
A getValue() 0 4 1
B _getValue() 0 18 5
A insertField() 0 4 1
A insertInField() 0 13 3
A addField() 0 4 1
A count() 0 3 1
A showableProperty() 0 3 1
D setInstance() 0 33 9
A getDefaultProperties() 0 11 3
A setCaptions() 0 4 1
A setVisibleProperties() 0 4 1
A setValueFunction() 0 4 1
A setCkValueFunction() 0 4 1
A setIndex() 0 3 1
A getProperties() 0 3 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

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 Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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
2
namespace Ajax\semantic\widgets\datatable;
3
use Ajax\service\JString;
4
5
class InstanceViewer {
6
	private $instance;
7
	private $reflect;
8
	private $properties;
9
	private $captions;
10
	private $visibleProperties;
11
	private $values;
12
	private static $index=0;
13
14
	public function __construct($instance=NULL,$captions=NULL){
15
		$this->values=[];
16
		if(isset($instance))
17
			$this->setInstance($instance);
18
		$this->setCaptions($captions);
19
	}
20
21
	public function getCaption($index){
22
		return $this->properties[$index]->getName();
23
	}
24
25
	public function getCaptions(){
26
		if(isset($this->captions)){
27
			$result= $this->captions;
28
			for($i=\sizeof($result);$i<$this->count();$i++){
29
				$result[]="";
30
			}
31
			return $result;
32
		}
33
		$captions=[];
34
		$index=0;
35
		$count=$this->count();
36
		while($index<$count){
37
			$captions[]=$this->getCaption($index++);
38
		}
39
		return $captions;
40
	}
41
42 View Code Duplication
	public function getValues(){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
43
		$values=[];
44
		$index=0;
45
		$count=$this->count();
46
		while($index<$count){
47
			$values[]=$this->getValue($index++);
48
		}
49
		return $values;
50
	}
51
52
	public function getCkValue(){
53
		$value=self::$index;
54
		if(isset($this->values["ck"]))
55
			$value=$this->values["ck"](self::$index,$this->instance);
56
		self::$index++;
57
		return $value;
58
	}
59
60
	public function getValue($index){
61
		$property=$this->properties[$index];
62
		return $this->_getValue($property, $index);
63
	}
64
65
	private function _getValue($property,$index){
66
		if($property instanceof \ReflectionProperty){
67
			$property->setAccessible(true);
68
			$value=$property->getValue($this->instance);
69
			if(isset($this->values[$index])){
70
				$value= $this->values[$index]($value);
71
			}
72
		}else{
73
			if(\is_callable($property))
74
				$value=$property($this->instance);
75
			elseif(\is_array($property)){
76
				$values=\array_map(function($v) use ($index){return $this->_getValue($v, $index);}, $property);
77
				$value=\implode("", $values);
78
			}else
79
				$value=$property;
80
		}
81
		return $value;
82
	}
83
84
	public function insertField($index,$field){
85
		array_splice( $this->visibleProperties, $index, 0, $field );
86
		return $this;
87
	}
88
89
	public function insertInField($index,$field){
90
		$vb=$this->visibleProperties;
91
		if(isset($vb[$index])){
92
			if(\is_array($vb[$index])){
93
				$this->visibleProperties[$index][]=$field;
94
			}else{
95
				$this->visibleProperties[$index]=[$vb[$index],$field];
96
			}
97
		}else{
98
			return $this->insertField($index, $field);
99
		}
100
		return $this;
101
	}
102
103
	public function addField($field){
104
		$this->visibleProperties[]=$field;
105
		return $this;
106
	}
107
108
	public function count(){
109
		return \sizeof($this->properties);
110
	}
111
112
	private function showableProperty(\ReflectionProperty $rProperty){
113
		return JString::startswith($rProperty->getName(),"_")===false;
114
	}
115
116
	public function setInstance($instance) {
117
		if(\is_string($instance)){
118
			$instance=new $instance();
119
		}
120
		$this->instance=$instance;
121
		$this->properties=[];
122
		$this->reflect=new \ReflectionClass($instance);
123
		if(\sizeof($this->visibleProperties)===0){
124
			$this->properties=$this->getDefaultProperties();
125
		}else{
126
			foreach ($this->visibleProperties as $property){
127
				if(\is_callable($property)){
128
					$this->properties[]=$property;
129
				}elseif(\is_string($property)){
130
					try{
131
						$rProperty=$this->reflect->getProperty($property);
132
						$this->properties[]=$rProperty;
133
					}catch(\Exception $e){
134
						$this->properties[]=$property;
135
					}
136
				}elseif(\is_int($property)){
137
					$props=$this->getDefaultProperties();
138
					if(isset($props[$property]))
139
						$this->properties[]=$props[$property];
140
					else
141
						$this->properties[]=$property;
142
				}else{
143
					$this->properties[]=$property;
144
				}
145
			}
146
		}
147
		return $this;
148
	}
149
150
	private function getDefaultProperties(){
151
		$result=[];
152
		$properties=$this->reflect->getProperties();
153
		foreach ($properties as $property){
154
			$showable=$this->showableProperty($property);
155
			if($showable!==false){
156
				$result[]=$property;
157
			}
158
		}
159
		return $result;
160
	}
161
162
	public function setCaptions($captions) {
163
		$this->captions=$captions;
164
		return $this;
165
	}
166
167
	public function setVisibleProperties($visibleProperties) {
168
		$this->visibleProperties=$visibleProperties;
169
		return $this;
170
	}
171
172
	public function setValueFunction($index,$callback){
173
		$this->values[$index]=$callback;
174
		return $this;
175
	}
176
177
	public function setCkValueFunction($callback){
178
		$this->values["ck"]=$callback;
179
		return $this;
180
	}
181
182
	public static function setIndex($index) {
183
		self::$index=$index;
184
	}
185
186
	public function getProperties() {
187
		return $this->properties;
188
	}
189
190
191
192
193
}