Passed
Push — master ( 5bce84...50f645 )
by Jean-Christophe
16:11
created

Reflexion::getMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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