Passed
Push — master ( a4d3b7...39c189 )
by Jean-Christophe
10:46
created

Reflexion::getTableName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 11
ccs 8
cts 9
cp 0.8889
rs 9.9666
cc 3
nc 3
nop 1
crap 3.0123
1
<?php
2
3
namespace Ubiquity\orm\parser;
4
5
use mindplay\annotations\Annotations;
6
use Ubiquity\orm\OrmUtils;
7
8
/**
9
 * Reflection utilities
10
 * in dev environment only
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.2
14
 */
15
class Reflexion {
16
	use ReflexionFieldsTrait;
1 ignored issue
show
introduced by
The trait Ubiquity\orm\parser\ReflexionFieldsTrait requires some properties which are not provided by Ubiquity\orm\parser\Reflexion: $dbType, $name, $nullable
Loading history...
17
	protected static $classProperties = [ ];
18
19 17
	public static function getMethods($instance, $filter = null) {
20 17
		$reflect = new \ReflectionClass ( $instance );
21 17
		$methods = $reflect->getMethods ( $filter );
22 17
		return $methods;
23
	}
24
25 19
	public static function getKeyFields($instance) {
26 19
		return self::getMembersNameWithAnnotation ( get_class ( $instance ), "@id" );
27
	}
28
29 19
	public static function getMemberValue($instance, $member) {
30 19
		$prop = self::getProperty ( $instance, $member );
31 19
		$prop->setAccessible ( true );
32 19
		return $prop->getValue ( $instance );
33
	}
34
35 42
	public static function getPropValue($instance, $prop) {
36 42
		return $prop->getValue ( $instance );
37
	}
38
39 1
	public static function setMemberValue($instance, $member, $value) {
40 1
		$prop = self::getProperty ( $instance, $member );
41 1
		if ($prop) {
42
			$prop->setAccessible ( true );
43
			$prop->setValue ( $instance, $value );
44
			return true;
45
		}
46 1
		return false;
47
	}
48
49 12
	public static function getPropertiesAndValues($instance, $props = NULL) {
50 12
		$ret = array ();
51 12
		$className = \get_class ( $instance );
52 12
		if (isset ( self::$classProperties [$className] )) {
53 8
			foreach ( self::$classProperties [$className] as $prop ) {
54 8
				$ret [$prop->getName ()] = $prop->getValue ( $instance );
55
			}
56 8
			return $ret;
57
		}
58 5
		if (\is_null ( $props ))
59 5
			$props = self::getProperties ( $instance );
60 5
		foreach ( $props as $prop ) {
61 5
			$prop->setAccessible ( true );
62 5
			$v = $prop->getValue ( $instance );
63 5
			if (OrmUtils::isSerializable ( $className, $prop->getName () )) {
64 5
				if (OrmUtils::isNotNullOrNullAccepted ( $v, $className, $prop->getName () )) {
65 5
					$name = OrmUtils::getFieldName ( $className, $prop->getName () );
66 5
					$ret [$name] = $v;
67 5
					self::$classProperties [$className] [] = $prop;
68
				}
69
			}
70
		}
71 5
		return $ret;
72
	}
73
74 23
	public static function getAnnotationClass($class, $annotation) {
75 23
		$annot = Annotations::ofClass ( $class, $annotation );
76 23
		return $annot;
77
	}
78
79 20
	public static function getAnnotationMember($class, $member, $annotation) {
80 20
		$annot = Annotations::ofProperty ( $class, $member, $annotation );
81 20
		return current ( $annot );
82
	}
83
84 9
	public static function getAnnotationsMember($class, $member, $annotation) {
85 9
		return Annotations::ofProperty ( $class, $member, $annotation );
86
	}
87
88 17
	public static function getAnnotationsMethod($class, $method, $annotation) {
89 17
		if (is_array ( $annotation )) {
90 17
			$result = [ ];
91 17
			foreach ( $annotation as $annot ) {
92 17
				$annots = Annotations::ofMethod ( $class, $method, $annot );
93 17
				if (sizeof ( $annots ) > 0) {
94 17
					$result = array_merge ( $result, $annots );
95
				}
96
			}
97 17
			return $result;
98
		}
99 6
		$annots = Annotations::ofMethod ( $class, $method, $annotation );
100 6
		if (\sizeof ( $annots ) > 0)
101 6
			return $annots;
102 6
		return false;
103
	}
104
105 19
	public static function getMembersAnnotationWithAnnotation($class, $annotation) {
106
		return self::getMembersWithAnnotation_ ( $class, $annotation, function (&$ret, $prop, $annot) {
107 19
			$ret [$prop->getName ()] = $annot;
108 19
		} );
109
	}
110
111
	public static function getMembersWithAnnotation($class, $annotation) {
112
		return self::getMembersWithAnnotation_ ( $class, $annotation, function (&$ret, $prop) {
113
			$ret [] = $prop;
114
		} );
115
	}
116
117 19
	public static function getMembersNameWithAnnotation($class, $annotation) {
118
		return self::getMembersWithAnnotation_ ( $class, $annotation, function (&$ret, $prop) {
119 19
			$ret [] = $prop->getName ();
120 19
		} );
121
	}
122
123 19
	protected static function getMembersWithAnnotation_($class, $annotation, $callback) {
124 19
		$props = self::getProperties ( $class );
125 19
		$ret = array ();
126 19
		foreach ( $props as $prop ) {
127 19
			$annot = self::getAnnotationMember ( $class, $prop->getName (), $annotation );
128 19
			if ($annot !== false)
129 19
				$callback ( $ret, $prop, $annot );
130
		}
131 19
		return $ret;
132
	}
133
134 19
	public static function getTableName($class) {
135 19
		$ret = Reflexion::getAnnotationClass ( $class, "@table" );
136 19
		if (\sizeof ( $ret ) === 0) {
137 19
			$posSlash = strrpos ( $class, '\\' );
138 19
			if ($posSlash !== false)
139 19
				$class = substr ( $class, $posSlash + 1 );
140 19
			$ret = $class;
141
		} else {
142
			$ret = $ret [0]->name;
143
		}
144 19
		return $ret;
145
	}
146
147 17
	public static function getMethodParameters(\ReflectionFunctionAbstract $method) {
148 17
		$result = array ();
149 17
		foreach ( $method->getParameters () as $param ) {
150 17
			$result [] = $param->name;
151
		}
152 17
		return $result;
153
	}
154
155 2
	public static function getJoinTables($class) {
156 2
		$result = [ ];
157 2
		$annots = self::getMembersAnnotationWithAnnotation ( $class, "@joinTable" );
158 2
		foreach ( $annots as $annot ) {
159 2
			$result [] = $annot->name;
160
		}
161 2
		return $result;
162
	}
163
164 2
	public static function getAllJoinTables($models) {
165 2
		$result = [ ];
166 2
		foreach ( $models as $model ) {
167 2
			$result = array_merge ( $result, self::getJoinTables ( $model ) );
168
		}
169 2
		return $result;
170
	}
171
}
172