Passed
Push — master ( b68a78...d29a76 )
by Jean-Christophe
16:10 queued 03:52
created

Reflexion::getJoinTables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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