This file is indexed.

/usr/share/php/TokenReflection/Resolver.php is in php-tokenreflection 1.4.0-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
 * PHP Token Reflection
 *
 * Version 1.4.0
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this library in the file LICENSE.md.
 *
 * @author Ondřej Nešpor
 * @author Jaroslav Hanslík
 */

namespace TokenReflection;

/**
 * TokenReflection Resolver class.
 */
class Resolver
{
	/**
	 * Placeholder for non-existen constants.
	 *
	 * @var null
	 */
	const CONSTANT_NOT_FOUND = '~~NOT RESOLVED~~';

	/**
	 * Returns a fully qualified name of a class using imported/aliased namespaces.
	 *
	 * @param string $className Input class name
	 * @param array $aliases Namespace import aliases
	 * @param string $namespaceName Context namespace name
	 * @return string
	 */
	final public static function resolveClassFQN($className, array $aliases, $namespaceName = null)
	{
		if ($className{0} == '\\') {
			// FQN
			return ltrim($className, '\\');
		}

		if (false === ($position = strpos($className, '\\'))) {
			// Plain class name
			if (isset($aliases[$className])) {
				return $aliases[$className];
			}
		} else {
			// Namespaced class name
			$alias = substr($className, 0, $position);
			if (isset($aliases[$alias])) {
				return $aliases[$alias] . '\\' . substr($className, $position + 1);
			}
		}

		return null === $namespaceName || '' === $namespaceName || $namespaceName === ReflectionNamespace::NO_NAMESPACE_NAME ? $className : $namespaceName . '\\' . $className;
	}

	/**
	 * Returns a property/parameter/constant/static variable value definition.
	 *
	 * @param array $tokens Tokenized definition
	 * @param \TokenReflection\ReflectionElement $reflection Caller reflection
	 * @return string
	 * @throws \TokenReflection\Exception\RuntimeException If an invalid reflection object was provided.
	 * @throws \TokenReflection\Exception\RuntimeException If an invalid source code was provided.
	 */
	final public static function getValueDefinition(array $tokens, ReflectionElement $reflection)
	{
		if ($reflection instanceof ReflectionConstant || $reflection instanceof ReflectionFunction) {
			$namespace = $reflection->getNamespaceName();
		} elseif ($reflection instanceof ReflectionParameter) {
			$namespace = $reflection->getDeclaringFunction()->getNamespaceName();
		} elseif ($reflection instanceof ReflectionProperty || $reflection instanceof ReflectionMethod) {
			$namespace = $reflection->getDeclaringClass()->getNamespaceName();
		} else {
			throw new Exception\RuntimeException('Invalid reflection object given.', Exception\RuntimeException::INVALID_ARGUMENT, $reflection);
		}

		// Process __LINE__ constants; replace with the line number of the corresponding token
		foreach ($tokens as $index => $token) {
			if (T_LINE === $token[0]) {
				$tokens[$index] = array(
					T_LNUMBER,
					$token[2],
					$token[2]
				);
			}
		}

		$source = self::getSourceCode($tokens);

		$constants = self::findConstants($tokens, $reflection);
		if (!empty($constants)) {
			foreach (array_reverse($constants, true) as $offset => $constant) {
				$value = '';

				try {
					switch ($constant) {
						case '__FILE__':
							$value = $reflection->getFileName();
							break;
						case '__DIR__':
							$value = dirname($reflection->getFileName());
							break;
						case '__FUNCTION__':
							if ($reflection instanceof IReflectionParameter) {
								$value = $reflection->getDeclaringFunctionName();
							} elseif ($reflection instanceof IReflectionFunctionBase) {
								$value = $reflection->getName();
							}
							break;
						case '__CLASS__':
							if ($reflection instanceof IReflectionConstant || $reflection instanceof IReflectionParameter || $reflection instanceof IReflectionProperty || $reflection instanceof IReflectionMethod) {
								$value = $reflection->getDeclaringClassName() ?: '';
							}
							break;
						case '__TRAIT__':
							if ($reflection instanceof IReflectionMethod || $reflection instanceof IReflectionProperty) {
								$value = $reflection->getDeclaringTraitName() ?: '';
							} elseif ($reflection instanceof IReflectionParameter) {
								$method = $reflection->getDeclaringFunction();
								if ($method instanceof IReflectionMethod) {
									$value = $method->getDeclaringTraitName() ?: '';
								}
							}
							break;
						case '__METHOD__':
							if ($reflection instanceof IReflectionParameter) {
								if (null !== $reflection->getDeclaringClassName()) {
									$value = $reflection->getDeclaringClassName() . '::' . $reflection->getDeclaringFunctionName();
								} else {
									$value = $reflection->getDeclaringFunctionName();
								}
							} elseif ($reflection instanceof IReflectionConstant || $reflection instanceof IReflectionProperty) {
								$value = $reflection->getDeclaringClassName() ?: '';
							} elseif ($reflection instanceof IReflectionMethod) {
								$value = $reflection->getDeclaringClassName() . '::' . $reflection->getName();
							} elseif ($reflection instanceof IReflectionFunction) {
								$value = $reflection->getName();
							}
							break;
						case '__NAMESPACE__':
							if (($reflection instanceof IReflectionConstant && null !== $reflection->getDeclaringClassName()) || $reflection instanceof IReflectionProperty) {
								$value = $reflection->getDeclaringClass()->getNamespaceName();
							} elseif ($reflection instanceof IReflectionParameter) {
								if (null !== $reflection->getDeclaringClassName()) {
									$value = $reflection->getDeclaringClass()->getNamespaceName();
								} else {
									$value = $reflection->getDeclaringFunction()->getNamespaceName();
								}
							} elseif ($reflection instanceof IReflectionMethod) {
								$value = $reflection->getDeclaringClass()->getNamespaceName();
							} else {
								$value = $reflection->getNamespaceName();
							}
							break;
						default:
							if (0 === stripos($constant, 'self::') || 0 === stripos($constant, 'parent::')) {
								// Handle self:: and parent:: definitions

								if ($reflection instanceof ReflectionConstant && null === $reflection->getDeclaringClassName()) {
									throw new Exception\RuntimeException('Top level constants cannot use self:: and parent:: references.', Exception\RuntimeException::UNSUPPORTED, $reflection);
								} elseif ($reflection instanceof ReflectionParameter && null === $reflection->getDeclaringClassName()) {
									throw new Exception\RuntimeException('Function parameters cannot use self:: and parent:: references.', Exception\RuntimeException::UNSUPPORTED, $reflection);
								}

								if (0 === stripos($constant, 'self::')) {
									$className = $reflection->getDeclaringClassName();
								} else {
									$declaringClass = $reflection->getDeclaringClass();
									$className = $declaringClass->getParentClassName() ?: self::CONSTANT_NOT_FOUND;
								}

								$constantName = $className . substr($constant, strpos($constant, '::'));
							} else {
								$constantName = self::resolveClassFQN($constant, $reflection->getNamespaceAliases(), $namespace);
								if ($cnt = strspn($constant, '\\')) {
									$constantName = str_repeat('\\', $cnt) . $constantName;
								}
							}

							$constantReflection = $reflection->getBroker()->getConstant($constantName);
							$value = $constantReflection->getValue();
					}
				} catch (Exception\RuntimeException $e) {
					$value = self::CONSTANT_NOT_FOUND;
				}

				$source = substr_replace($source, var_export($value, true), $offset, strlen($constant));
			}
		}

		return self::evaluate(sprintf("return %s;\n", $source));
	}

	/**
	 * Returns a part of the source code defined by given tokens.
	 *
	 * @param array $tokens Tokens array
	 * @return array
	 */
	final public static function getSourceCode(array $tokens)
	{
		if (empty($tokens)) {
			return null;
		}

		$source = '';
		foreach ($tokens as $token) {
			$source .= $token[1];
		}
		return $source;
	}

	/**
	 * Finds constant names in the token definition.
	 *
	 * @param array $tokens Tokenized source code
	 * @param \TokenReflection\ReflectionElement $reflection Caller reflection
	 * @return array
	 */
	final public static function findConstants(array $tokens, ReflectionElement $reflection)
	{
		static $accepted = array(
			T_DOUBLE_COLON => true,
			T_STRING => true,
			T_NS_SEPARATOR => true,
			T_CLASS_C => true,
			T_DIR => true,
			T_FILE => true,
			T_LINE => true,
			T_FUNC_C => true,
			T_METHOD_C => true,
			T_NS_C => true,
			T_TRAIT_C => true
		);
		static $dontResolve = array('true' => true, 'false' => true, 'null' => true);

		// Adding a dummy token to the end
		$tokens[] = array(null);

		$constants = array();
		$constant = '';
		$offset = 0;
		foreach ($tokens as $token) {
			if (isset($accepted[$token[0]])) {
				$constant .= $token[1];
			} elseif ('' !== $constant) {
				if (!isset($dontResolve[strtolower($constant)])) {
					$constants[$offset - strlen($constant)] = $constant;
				}
				$constant = '';
			}

			if (null !== $token[0]) {
				$offset += strlen($token[1]);
			}
		}
		return $constants;
	}

	/**
	 * Evaluates a source code.
	 *
	 * @param string $source Source code
	 * @return mixed
	 */
	final private static function evaluate($source) {
		return eval($source);
	}
}