Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 150 | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

Revision 150 Revision 399
Zeile 17... Zeile 17...
17
 */
17
 */
18
final class Reflection
18
final class Reflection
19
{
19
{
20
	use Nette\StaticClass;
20
	use Nette\StaticClass;
Zeile 21... Zeile -...
21
 
-
 
22
	/**
21
 
23
	 * Determines if type is PHP built-in type. Otherwise, it is the class name.
-
 
24
	 */
22
	/** @deprecated use Nette\Utils\Validator::isBuiltinType() */
25
	public static function isBuiltinType(string $type): bool
23
	public static function isBuiltinType(string $type): bool
26
	{
24
	{
27
		return Validators::isBuiltinType($type);
25
		return Validators::isBuiltinType($type);
Zeile 28... Zeile -...
28
	}
-
 
29
 
26
	}
30
 
-
 
31
	/**
27
 
32
	 * Determines if type is special class name self/parent/static.
28
 
33
	 */
29
	/** @deprecated use Nette\Utils\Validator::isClassKeyword() */
34
	public static function isClassKeyword(string $name): bool
30
	public static function isClassKeyword(string $name): bool
Zeile 35... Zeile -...
35
	{
-
 
36
		return Validators::isClassKeyword($name);
-
 
37
	}
-
 
38
 
-
 
39
 
-
 
40
	/**
-
 
41
	 * Returns the type of return value of given function or method and normalizes `self`, `static`, and `parent` to actual class names.
-
 
42
	 * If the function does not have a return type, it returns null.
-
 
43
	 * If the function has union or intersection type, it throws Nette\InvalidStateException.
-
 
44
	 */
-
 
45
	public static function getReturnType(\ReflectionFunctionAbstract $func): ?string
-
 
46
	{
-
 
47
		$type = $func->getReturnType() ?? (PHP_VERSION_ID >= 80100 && $func instanceof \ReflectionMethod ? $func->getTentativeReturnType() : null);
-
 
48
		return self::getType($func, $type);
-
 
49
	}
-
 
50
 
-
 
51
 
-
 
52
	/**
-
 
53
	 * @deprecated
-
 
54
	 */
-
 
55
	public static function getReturnTypes(\ReflectionFunctionAbstract $func): array
-
 
56
	{
-
 
57
		$type = Type::fromReflection($func);
-
 
58
		return $type ? $type->getNames() : [];
-
 
59
	}
-
 
60
 
-
 
61
 
-
 
62
	/**
31
	{
63
	 * Returns the type of given parameter and normalizes `self` and `parent` to the actual class names.
-
 
64
	 * If the parameter does not have a type, it returns null.
-
 
65
	 * If the parameter has union or intersection type, it throws Nette\InvalidStateException.
-
 
66
	 */
-
 
67
	public static function getParameterType(\ReflectionParameter $param): ?string
-
 
68
	{
-
 
69
		return self::getType($param, $param->getType());
-
 
70
	}
-
 
71
 
-
 
72
 
-
 
73
	/**
-
 
74
	 * @deprecated
-
 
75
	 */
-
 
76
	public static function getParameterTypes(\ReflectionParameter $param): array
-
 
77
	{
-
 
78
		$type = Type::fromReflection($param);
-
 
79
		return $type ? $type->getNames() : [];
-
 
80
	}
-
 
81
 
-
 
82
 
-
 
83
	/**
-
 
84
	 * Returns the type of given property and normalizes `self` and `parent` to the actual class names.
-
 
85
	 * If the property does not have a type, it returns null.
-
 
86
	 * If the property has union or intersection type, it throws Nette\InvalidStateException.
-
 
87
	 */
-
 
88
	public static function getPropertyType(\ReflectionProperty $prop): ?string
-
 
89
	{
-
 
90
		return self::getType($prop, PHP_VERSION_ID >= 70400 ? $prop->getType() : null);
-
 
91
	}
-
 
92
 
-
 
93
 
-
 
94
	/**
-
 
95
	 * @deprecated
-
 
96
	 */
-
 
97
	public static function getPropertyTypes(\ReflectionProperty $prop): array
-
 
98
	{
-
 
99
		$type = Type::fromReflection($prop);
-
 
100
		return $type ? $type->getNames() : [];
-
 
101
	}
-
 
102
 
-
 
103
 
-
 
104
	/**
-
 
105
	 * @param  \ReflectionFunction|\ReflectionMethod|\ReflectionParameter|\ReflectionProperty  $reflection
-
 
106
	 */
-
 
107
	private static function getType($reflection, ?\ReflectionType $type): ?string
-
 
108
	{
-
 
109
		if ($type === null) {
-
 
110
			return null;
-
 
111
 
-
 
112
		} elseif ($type instanceof \ReflectionNamedType) {
-
 
113
			return Type::resolve($type->getName(), $reflection);
-
 
114
 
-
 
115
		} elseif ($type instanceof \ReflectionUnionType || $type instanceof \ReflectionIntersectionType) {
-
 
116
			throw new Nette\InvalidStateException('The ' . self::toString($reflection) . ' is not expected to have a union or intersection type.');
-
 
117
 
-
 
118
		} else {
-
 
119
			throw new Nette\InvalidStateException('Unexpected type of ' . self::toString($reflection));
-
 
120
		}
-
 
121
	}
-
 
122
 
-
 
123
 
-
 
124
	/**
32
		return Validators::isClassKeyword($name);
125
	 * Returns the default value of parameter. If it is a constant, it returns its value.
33
	}
126
	 * @return mixed
34
 
127
	 * @throws \ReflectionException  If the parameter does not have a default value or the constant cannot be resolved
35
 
128
	 */
36
	/** @deprecated use native ReflectionParameter::getDefaultValue() */
129
	public static function getParameterDefaultValue(\ReflectionParameter $param)
37
	public static function getParameterDefaultValue(\ReflectionParameter $param): mixed
Zeile 305... Zeile 213...
305
	 * Parses PHP code to [class => [alias => class, ...]]
213
	 * Parses PHP code to [class => [alias => class, ...]]
306
	 */
214
	 */
307
	private static function parseUseStatements(string $code, ?string $forClass = null): array
215
	private static function parseUseStatements(string $code, ?string $forClass = null): array
308
	{
216
	{
309
		try {
217
		try {
310
			$tokens = token_get_all($code, TOKEN_PARSE);
218
			$tokens = \PhpToken::tokenize($code, TOKEN_PARSE);
311
		} catch (\ParseError $e) {
219
		} catch (\ParseError $e) {
312
			trigger_error($e->getMessage(), E_USER_NOTICE);
220
			trigger_error($e->getMessage(), E_USER_NOTICE);
313
			$tokens = [];
221
			$tokens = [];
314
		}
222
		}
Zeile 315... Zeile 223...
315
 
223
 
316
		$namespace = $class = $classLevel = $level = null;
224
		$namespace = $class = $classLevel = $level = null;
Zeile 317... Zeile -...
317
		$res = $uses = [];
-
 
318
 
-
 
319
		$nameTokens = PHP_VERSION_ID < 80000
225
		$res = $uses = [];
Zeile 320... Zeile 226...
320
			? [T_STRING, T_NS_SEPARATOR]
226
 
321
			: [T_STRING, T_NS_SEPARATOR, T_NAME_QUALIFIED, T_NAME_FULLY_QUALIFIED];
227
		$nameTokens = [T_STRING, T_NS_SEPARATOR, T_NAME_QUALIFIED, T_NAME_FULLY_QUALIFIED];
322
 
228
 
323
		while ($token = current($tokens)) {
229
		while ($token = current($tokens)) {
324
			next($tokens);
230
			next($tokens);
325
			switch (is_array($token) ? $token[0] : $token) {
231
			switch ($token->id) {
326
				case T_NAMESPACE:
232
				case T_NAMESPACE:
Zeile 376... Zeile 282...
376
 
282
 
Zeile 377... Zeile 283...
377
					break;
283
					break;
378
 
284
 
379
				case T_CURLY_OPEN:
285
				case T_CURLY_OPEN:
380
				case T_DOLLAR_OPEN_CURLY_BRACES:
286
				case T_DOLLAR_OPEN_CURLY_BRACES:
381
				case '{':
287
				case ord('{'):
Zeile 382... Zeile 288...
382
					$level++;
288
					$level++;
383
					break;
289
					break;
384
 
290
 
385
				case '}':
291
				case ord('}'):
Zeile 386... Zeile 292...
386
					if ($level === $classLevel) {
292
					if ($level === $classLevel) {
Zeile 393... Zeile 299...
393
 
299
 
394
		return $res;
300
		return $res;
Zeile 395... Zeile 301...
395
	}
301
	}
396
 
302
 
397
 
303
 
398
	private static function fetch(array &$tokens, $take): ?string
304
	private static function fetch(array &$tokens, string|int|array $take): ?string
399
	{
-
 
400
		$res = null;
305
	{
401
		while ($token = current($tokens)) {
306
		$res = null;
402
			[$token, $s] = is_array($token) ? $token : [$token, $token];
307
		while ($token = current($tokens)) {
403
			if (in_array($token, (array) $take, true)) {
308
			if ($token->is($take)) {
404
				$res .= $s;
309
				$res .= $token->text;
Zeile 405... Zeile 310...
405
			} elseif (!in_array($token, [T_DOC_COMMENT, T_WHITESPACE, T_COMMENT], true)) {
310
			} elseif (!$token->is([T_DOC_COMMENT, T_WHITESPACE, T_COMMENT])) {
406
				break;
311
				break;