Completed
Push — master ( 4e6026...e0b9fa )
by Jean-Christophe
01:30
created

Reflexion   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 168
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 45
lcom 1
cbo 1
dl 24
loc 168
rs 8.3673
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A getProperties() 0 8 2
A getMethods() 0 5 1
A getKeyFields() 0 3 1
A getMemberValue() 0 5 1
A setMemberValue() 0 9 2
A getProperty() 0 7 2
B getPropertiesAndValues() 4 17 5
A getAnnotationClass() 0 4 1
A getAnnotationMember() 0 6 2
A getAnnotationsMethod() 0 6 2
A getMembersAnnotationWithAnnotation() 10 10 3
A getMembersWithAnnotation() 0 10 3
A getMembersNameWithAnnotation() 10 10 3
A isNullable() 0 7 2
A getDbType() 0 7 2
B isSerializable() 0 6 5
A getFieldName() 0 8 3
A getTableName() 0 12 3
A getMethodParameters() 0 7 2

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 Reflexion 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 Reflexion, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Ubiquity\orm\parser;
4
5
use mindplay\annotations\Annotation;
6
use mindplay\annotations\Annotations;
7
use Ubiquity\orm\OrmUtils;
8
9
/**
10
 * Utilitaires de Reflexion
11
 * @author jc
12
 * @version 1.0.0.2
13
 */
14
class Reflexion {
15
16
	public static function getProperties($instance) {
17
		if (\is_string($instance)) {
18
			$instance=new $instance();
19
		}
20
		$reflect=new \ReflectionClass($instance);
21
		$props=$reflect->getProperties();
22
		return $props;
23
	}
24
25
	public static function getMethods($instance, $filter=null) {
26
		$reflect=new \ReflectionClass($instance);
27
		$methods=$reflect->getMethods($filter);
28
		return $methods;
29
	}
30
31
	public static function getKeyFields($instance) {
32
		return Reflexion::getMembersNameWithAnnotation(get_class($instance), "@id");
33
	}
34
35
	public static function getMemberValue($instance, $member) {
36
		$prop=self::getProperty($instance, $member);
37
		$prop->setAccessible(true);
38
		return $prop->getValue($instance);
39
	}
40
41
	public static function setMemberValue($instance, $member,$value) {
42
		$prop=self::getProperty($instance, $member);
43
		if($prop){
44
			$prop->setAccessible(true);
45
			$prop->setValue($instance,$value);
46
			return true;
47
		}
48
		return false;
49
	}
50
51
	public static function getProperty($instance, $member) {
52
		$reflect=new \ReflectionClass($instance);
53
		$prop=false;
54
		if($reflect->hasProperty($member))
55
			$prop=$reflect->getProperty($member);
56
		return $prop;
57
	}
58
59
	public static function getPropertiesAndValues($instance, $props=NULL) {
60
		$ret=array ();
61
		$className=get_class($instance);
62
		if (is_null($props))
63
			$props=self::getProperties($instance);
64
		foreach ( $props as $prop ) {
65
			$prop->setAccessible(true);
66
			$v=$prop->getValue($instance);
67
			if (OrmUtils::isSerializable($className, $prop->getName())) {
68 View Code Duplication
				if (OrmUtils::isNotNullOrNullAccepted($v, $className, $prop->getName())) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
69
					$name=OrmUtils::getFieldName($className, $prop->getName());
70
					$ret[$name]=$v;
71
				}
72
			}
73
		}
74
		return $ret;
75
	}
76
77
	public static function getAnnotationClass($class, $annotation) {
78
		$annot=Annotations::ofClass($class, $annotation);
79
		return $annot;
80
	}
81
82
	public static function getAnnotationMember($class, $member, $annotation) {
83
		$annot=Annotations::ofProperty($class, $member, $annotation);
84
		if (\sizeof($annot) > 0)
85
			return $annot[0];
86
		return false;
87
	}
88
89
	public static function getAnnotationsMethod($class, $method, $annotation) {
90
		$annots=Annotations::ofMethod($class, $method, $annotation);
91
		if (\sizeof($annots) > 0)
92
			return $annots;
93
		return false;
94
	}
95
96 View Code Duplication
	public static function getMembersAnnotationWithAnnotation($class, $annotation) {
97
		$props=self::getProperties($class);
98
		$ret=array ();
99
		foreach ( $props as $prop ) {
100
			$annot=self::getAnnotationMember($class, $prop->getName(), $annotation);
101
			if ($annot !== false)
102
				$ret[$prop->getName()]=$annot;
103
		}
104
		return $ret;
105
	}
106
107
	public static function getMembersWithAnnotation($class, $annotation) {
108
		$props=self::getProperties($class);
109
		$ret=array ();
110
		foreach ( $props as $prop ) {
111
			$annot=self::getAnnotationMember($class, $prop->getName(), $annotation);
112
			if ($annot !== false)
113
				$ret[]=$prop;
114
		}
115
		return $ret;
116
	}
117
118 View Code Duplication
	public static function getMembersNameWithAnnotation($class, $annotation) {
119
		$props=self::getProperties($class);
120
		$ret=array ();
121
		foreach ( $props as $prop ) {
122
			$annot=self::getAnnotationMember($class, $prop->getName(), $annotation);
123
			if ($annot !== false)
124
				$ret[]=$prop->getName();
125
		}
126
		return $ret;
127
	}
128
129
	public static function isNullable($class, $member) {
130
		$ret=self::getAnnotationMember($class, $member, "@column");
131
		if (!$ret)
132
			return false;
133
		else
134
			return $ret->nullable;
135
	}
136
137
	public static function getDbType($class, $member) {
138
		$ret=self::getAnnotationMember($class, $member, "@column");
139
		if (!$ret)
140
			return false;
141
		else
142
			return $ret->dbType;
143
	}
144
145
	public static function isSerializable($class, $member) {
146
		if (self::getAnnotationMember($class, $member, "@transient") !== false || self::getAnnotationMember($class, $member, "@manyToOne") !== false || self::getAnnotationMember($class, $member, "@manyToMany") !== false || self::getAnnotationMember($class, $member, "@oneToMany") !== false)
147
			return false;
148
		else
149
			return true;
150
	}
151
152
	public static function getFieldName($class, $member) {
153
		$ret=self::getAnnotationMember($class, $member, "@column");
154
		if ($ret === false || !isset($ret->name))
155
			$ret=$member;
156
		else
157
			$ret=$ret->name;
158
		return $ret;
159
	}
160
161
	public static function getTableName($class) {
162
		$ret=Reflexion::getAnnotationClass($class, "@table");
163
		if (\sizeof($ret) === 0) {
164
			$posSlash=strrpos($class, '\\');
165
			if ($posSlash !== false)
166
				$class=substr($class, $posSlash + 1);
167
			$ret=$class;
168
		} else {
169
			$ret=$ret[0]->name;
170
		}
171
		return $ret;
172
	}
173
174
	public static function getMethodParameters(\ReflectionMethod $method) {
175
		$result=array ();
176
		foreach ( $method->getParameters() as $param ) {
177
			$result[]=$param->name;
178
		}
179
		return $result;
180
	}
181
}
182