/usr/share/php/TokenReflection/ReflectionConstant.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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | <?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;
use TokenReflection\Stream\StreamBase as Stream, TokenReflection\Exception;
/**
* Tokenized constant reflection.
*/
class ReflectionConstant extends ReflectionElement implements IReflectionConstant
{
/**
* Name of the declaring class.
*
* @var string
*/
private $declaringClassName;
/**
* Constant namespace name.
*
* @var string
*/
private $namespaceName;
/**
* Constant value.
*
* @var mixed
*/
private $value;
/**
* Constant value definition in tokens.
*
* @var array|string
*/
private $valueDefinition = array();
/**
* Imported namespace/class aliases.
*
* @var array
*/
private $aliases = array();
/**
* Returns the unqualified name (UQN).
*
* @return string
*/
public function getShortName()
{
$name = $this->getName();
if (null !== $this->namespaceName && $this->namespaceName !== ReflectionNamespace::NO_NAMESPACE_NAME) {
$name = substr($name, strlen($this->namespaceName) + 1);
}
return $name;
}
/**
* Returns the name of the declaring class.
*
* @return string|null
*/
public function getDeclaringClassName()
{
return $this->declaringClassName;
}
/**
* Returns a reflection of the declaring class.
*
* @return \TokenReflection\ReflectionClass|null
*/
public function getDeclaringClass()
{
if (null === $this->declaringClassName) {
return null;
}
return $this->getBroker()->getClass($this->declaringClassName);
}
/**
* Returns the namespace name.
*
* @return string
*/
public function getNamespaceName()
{
return null === $this->namespaceName || $this->namespaceName === ReflectionNamespace::NO_NAMESPACE_NAME ? '' : $this->namespaceName;
}
/**
* Returns if the class is defined within a namespace.
*
* @return boolean
*/
public function inNamespace()
{
return '' !== $this->getNamespaceName();
}
/**
* Returns the constant value.
*
* @return mixed
*/
public function getValue()
{
if (is_array($this->valueDefinition)) {
$this->value = Resolver::getValueDefinition($this->valueDefinition, $this);
$this->valueDefinition = Resolver::getSourceCode($this->valueDefinition);
}
return $this->value;
}
/**
* Returns the constant value definition.
*
* @return string
*/
public function getValueDefinition()
{
return is_array($this->valueDefinition) ? Resolver::getSourceCode($this->valueDefinition) : $this->valueDefinition;
}
/**
* Returns the originaly provided value definition.
*
* @return string
*/
public function getOriginalValueDefinition()
{
return $this->valueDefinition;
}
/**
* Returns the string representation of the reflection object.
*
* @return string
*/
public function __toString()
{
return sprintf(
"Constant [ %s %s ] { %s }\n",
strtolower(gettype($this->getValue())),
$this->getName(),
$this->getValue()
);
}
/**
* Exports a reflected object.
*
* @param \TokenReflection\Broker $broker Broker instance
* @param string|object|null $class Class name, class instance or null
* @param string $constant Constant name
* @param boolean $return Return the export instead of outputting it
* @return string|null
* @throws \TokenReflection\Exception\RuntimeException If requested parameter doesn't exist.
*/
public static function export(Broker $broker, $class, $constant, $return = false)
{
$className = is_object($class) ? get_class($class) : $class;
$constantName = $constant;
if (null === $className) {
$constant = $broker->getConstant($constantName);
if (null === $constant) {
throw new Exception\RuntimeException('Constant does not exist.', Exception\RuntimeException::DOES_NOT_EXIST);
}
} else {
$class = $broker->getClass($className);
if ($class instanceof Invalid\ReflectionClass) {
throw new Exception\RuntimeException('Class is invalid.', Exception\RuntimeException::UNSUPPORTED);
} elseif ($class instanceof Dummy\ReflectionClass) {
throw new Exception\RuntimeException('Class does not exist.', Exception\RuntimeException::DOES_NOT_EXIST, $class);
}
$constant = $class->getConstantReflection($constantName);
}
if ($return) {
return $constant->__toString();
}
echo $constant->__toString();
}
/**
* Returns imported namespaces and aliases from the declaring namespace.
*
* @return array
*/
public function getNamespaceAliases()
{
return null === $this->declaringClassName ? $this->aliases : $this->getDeclaringClass()->getNamespaceAliases();
}
/**
* Returns an element pretty (docblock compatible) name.
*
* @return string
*/
public function getPrettyName()
{
return null === $this->declaringClassName ? parent::getPrettyName() : sprintf('%s::%s', $this->declaringClassName, $this->name);
}
/**
* Returns if the constant definition is valid.
*
* @return boolean
*/
public function isValid()
{
return true;
}
/**
* Processes the parent reflection object.
*
* @param \TokenReflection\IReflection $parent Parent reflection object
* @param \TokenReflection\Stream\StreamBase $tokenStream Token substream
* @return \TokenReflection\ReflectionElement
* @throws \TokenReflection\Exception\ParseException If an invalid parent reflection object was provided.
*/
protected function processParent(IReflection $parent, Stream $tokenStream)
{
if ($parent instanceof ReflectionFileNamespace) {
$this->namespaceName = $parent->getName();
$this->aliases = $parent->getNamespaceAliases();
} elseif ($parent instanceof ReflectionClass) {
$this->declaringClassName = $parent->getName();
} else {
throw new Exception\ParseException($this, $tokenStream, sprintf('Invalid parent reflection provided: "%s".', get_class($parent)), Exception\ParseException::INVALID_PARENT);
}
return parent::processParent($parent, $tokenStream);
}
/**
* Find the appropriate docblock.
*
* @param \TokenReflection\Stream\StreamBase $tokenStream Token substream
* @param \TokenReflection\IReflection $parent Parent reflection
* @return \TokenReflection\ReflectionConstant
*/
protected function parseDocComment(Stream $tokenStream, IReflection $parent)
{
$position = $tokenStream->key() - 1;
while ($position > 0 && !$tokenStream->is(T_CONST, $position)) {
$position--;
}
$actual = $tokenStream->key();
parent::parseDocComment($tokenStream->seek($position), $parent);
$tokenStream->seek($actual);
return $this;
}
/**
* Parses reflected element metadata from the token stream.
*
* @param \TokenReflection\Stream\StreamBase $tokenStream Token substream
* @param \TokenReflection\IReflection $parent Parent reflection object
* @return \TokenReflection\ReflectionConstant
*/
protected function parse(Stream $tokenStream, IReflection $parent)
{
if ($tokenStream->is(T_CONST)) {
$tokenStream->skipWhitespaces(true);
}
if (false === $this->docComment->getDocComment()) {
parent::parseDocComment($tokenStream, $parent);
}
return $this
->parseName($tokenStream)
->parseValue($tokenStream, $parent);
}
/**
* Parses the constant name.
*
* @param \TokenReflection\Stream\StreamBase $tokenStream Token substream
* @return \TokenReflection\ReflectionConstant
* @throws \TokenReflection\Exception\ParseReflection If the constant name could not be determined.
*/
protected function parseName(Stream $tokenStream)
{
if (!$tokenStream->is(T_STRING)) {
throw new Exception\ParseException($this, $tokenStream, 'The constant name could not be determined.', Exception\ParseException::LOGICAL_ERROR);
}
if (null === $this->namespaceName || $this->namespaceName === ReflectionNamespace::NO_NAMESPACE_NAME) {
$this->name = $tokenStream->getTokenValue();
} else {
$this->name = $this->namespaceName . '\\' . $tokenStream->getTokenValue();
}
$tokenStream->skipWhitespaces(true);
return $this;
}
/**
* Parses the constant value.
*
* @param \TokenReflection\Stream\StreamBase $tokenStream Token substream
* @param \TokenReflection\IReflection $parent Parent reflection object
* @return \TokenReflection\ReflectionConstant
* @throws \TokenReflection\Exception\ParseException If the constant value could not be determined.
*/
private function parseValue(Stream $tokenStream, IReflection $parent)
{
if (!$tokenStream->is('=')) {
throw new Exception\ParseException($this, $tokenStream, 'Could not find the definition start.', Exception\ParseException::UNEXPECTED_TOKEN);
}
$tokenStream->skipWhitespaces(true);
static $acceptedTokens = array(
'-' => true,
'+' => true,
T_STRING => true,
T_NS_SEPARATOR => true,
T_CONSTANT_ENCAPSED_STRING => true,
T_DNUMBER => true,
T_LNUMBER => true,
T_DOUBLE_COLON => true,
T_CLASS_C => true,
T_DIR => true,
T_FILE => true,
T_FUNC_C => true,
T_LINE => true,
T_METHOD_C => true,
T_NS_C => true,
T_TRAIT_C => true
);
while (null !== ($type = $tokenStream->getType())) {
if (T_START_HEREDOC === $type) {
$this->valueDefinition[] = $tokenStream->current();
while (null !== $type && T_END_HEREDOC !== $type) {
$tokenStream->next();
$this->valueDefinition[] = $tokenStream->current();
$type = $tokenStream->getType();
};
$tokenStream->next();
} elseif (isset($acceptedTokens[$type])) {
$this->valueDefinition[] = $tokenStream->current();
$tokenStream->next();
} elseif ($tokenStream->isWhitespace(true)) {
$tokenStream->skipWhitespaces(true);
} else {
break;
}
}
if (empty($this->valueDefinition)) {
throw new Exception\ParseException($this, $tokenStream, 'Value definition is empty.', Exception\ParseException::LOGICAL_ERROR);
}
$value = $tokenStream->getTokenValue();
if (null === $type || (',' !== $value && ';' !== $value)) {
throw new Exception\ParseException($this, $tokenStream, 'Invalid value definition.', Exception\ParseException::LOGICAL_ERROR);
}
return $this;
}
}
|