Completed
Push — master ( 361012...7b4150 )
by Jean-Christophe
02:01
created

Reflexion   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 68
Duplicated Lines 29.41 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 21
c 3
b 0
f 1
lcom 1
cbo 1
dl 20
loc 68
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getProperties() 0 8 2
A getProperty() 0 5 1
B getPropertiesAndValues() 0 17 9
A getAnnotationClass() 0 4 1
A getAnnotationMember() 0 6 2
A getMembersWithAnnotation() 10 10 3
A getMembersNameWithAnnotation() 10 10 3

How to fix   Duplicated Code   

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:

1
<?php
2
namespace micro\orm;
3
4
use mindplay\annotations\Annotation;
5
use mindplay\annotations\Annotations;
6
7
/**
8
 * Utilitaires de Reflexion
9
 * @author jc
10
 * @version 1.0.0.1
11
 * @package orm
12
 */
13
class Reflexion{
14
	public static function getProperties($instance){
15
		if(\is_string($instance)){
16
			$instance=new $instance();
17
		}
18
		$reflect = new \ReflectionClass($instance);
19
		$props = $reflect->getProperties();
20
		return $props;
21
	}
22
23
	public static function getProperty($instance,$property){
24
		$reflect = new \ReflectionClass($instance);
25
		$prop = $reflect->getProperty($property);
26
		return $prop;
27
	}
28
29
	public static function getPropertiesAndValues($instance,$props=NULL){
30
		$ret=array();
31
		$className=get_class($instance);
32
		if(is_null($props))
33
			$props=self::getProperties($instance);
34
		foreach ($props as $prop){
35
			$prop->setAccessible(true);
36
			$v=$prop->getValue($instance);
37
			if(OrmUtils::isSerializable($className,$prop->getName())){
38
				if(($v!==null && $v!=="") || (($v===null || $v==="") && OrmUtils::isNullable($className, $prop->getName()))){
39
					$name=OrmUtils::getFieldName($className, $prop->getName());
40
					$ret[$name]=$v;
41
				}
42
			}
43
		}
44
		return $ret;
45
	}
46
47
	public static function getAnnotationClass($class,$annotation){
48
		$annot=Annotations::ofClass($class,$annotation);
49
		return $annot;
50
	}
51
52
	public static function getAnnotationMember($class,$member,$annotation){
53
		$annot=Annotations::ofProperty($class,$member,$annotation);
54
		if(\sizeof($annot)>0)
55
			return $annot[0];
56
		return false;
57
	}
58
59 View Code Duplication
	public static function getMembersWithAnnotation($class,$annotation){
1 ignored issue
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...
60
		$props=self::getProperties($class);
61
		$ret=array();
62
		foreach ($props as $prop){
63
			$annot=self::getAnnotationMember($class, $prop->getName(), $annotation);
64
			if($annot!==false)
65
				$ret[]=$prop;
66
		}
67
		return $ret;
68
	}
69
70 View Code Duplication
	public static function getMembersNameWithAnnotation($class,$annotation){
1 ignored issue
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...
71
		$props=self::getProperties($class);
72
		$ret=array();
73
		foreach ($props as $prop){
74
			$annot=self::getAnnotationMember($class, $prop->getName(), $annotation);
75
			if($annot!==false)
76
				$ret[]=$prop->getName();
77
		}
78
		return $ret;
79
	}
80
}
81