/usr/share/php/TokenReflection/ReflectionProperty.php is in php-tokenreflection 1.4.0-2build1.
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 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 | <?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\Exception, TokenReflection\Stream\StreamBase as Stream;
use ReflectionProperty as InternalReflectionProperty, ReflectionClass as InternalReflectionClass;
/**
* Tokenized class property reflection.
*/
class ReflectionProperty extends ReflectionElement implements IReflectionProperty
{
/**
* Access level of this property has changed from the original implementation.
*
* @see http://svn.php.net/viewvc/php/php-src/branches/PHP_5_3/Zend/zend_compile.h?revision=306939&view=markup#l134
* ZEND_ACC_CHANGED
*
* @var integer
*/
const ACCESS_LEVEL_CHANGED = 0x800;
/**
* Name of the declaring class.
*
* @var string
*/
private $declaringClassName;
/**
* Property modifiers.
*
* @var integer
*/
private $modifiers = 0;
/**
* Determines if modifiers are complete.
*
* @var boolean
*/
private $modifiersComplete = false;
/**
* Property default value.
*
* @var mixed
*/
private $defaultValue;
/**
* Property default value definition (part of the source code).
*
* @var array|string
*/
private $defaultValueDefinition = array();
/**
* Determined if the property value is accessible.
*
* @var boolean
*/
private $accessible = false;
/**
* Declaring trait name.
*
* @var string
*/
private $declaringTraitName;
/**
* Returns a reflection of the declaring class.
*
* @return \TokenReflection\ReflectionClass
*/
public function getDeclaringClass()
{
return $this->getBroker()->getClass($this->declaringClassName);
}
/**
* Returns the name of the declaring class.
*
* @return string
*/
public function getDeclaringClassName()
{
return $this->declaringClassName;
}
/**
* Returns the property default value.
*
* @return mixed
*/
public function getDefaultValue()
{
if (is_array($this->defaultValueDefinition)) {
$this->defaultValue = Resolver::getValueDefinition($this->defaultValueDefinition, $this);
$this->defaultValueDefinition = Resolver::getSourceCode($this->defaultValueDefinition);
}
return $this->defaultValue;
}
/**
* Returns the part of the source code defining the property default value.
*
* @return string
*/
public function getDefaultValueDefinition()
{
return is_array($this->defaultValueDefinition) ? Resolver::getSourceCode($this->defaultValueDefinition) : $this->defaultValueDefinition;
}
/**
* Returns the property value for a particular class instance.
*
* @param object $object
* @return mixed
* @throws \TokenReflection\Exception\RuntimeException If it is not possible to return the property value.
*/
public function getValue($object)
{
$declaringClass = $this->getDeclaringClass();
if (!$declaringClass->isInstance($object)) {
throw new Exception\RuntimeException('The given class is not an instance or subclass of the current class.', Exception\RuntimeException::INVALID_ARGUMENT, $this);
}
if ($this->isPublic()) {
return $object->{$this->name};
} elseif ($this->isAccessible()) {
$refClass = new InternalReflectionClass($object);
$refProperty = $refClass->getProperty($this->name);
$refProperty->setAccessible(true);
$value = $refProperty->getValue($object);
$refProperty->setAccessible(false);
return $value;
}
throw new Exception\RuntimeException('Only public and accessible properties can return their values.', Exception\RuntimeException::NOT_ACCESSBILE, $this);
}
/**
* Returns if the property was created at compile time.
*
* All properties in the source code are.
*
* @return boolean
*/
public function isDefault()
{
return true;
}
/**
* Returns property modifiers.
*
* @return integer
*/
public function getModifiers()
{
if (false === $this->modifiersComplete) {
$declaringClass = $this->getDeclaringClass();
$declaringClassParent = $declaringClass->getParentClass();
if ($declaringClassParent && $declaringClassParent->hasProperty($this->name)) {
$property = $declaringClassParent->getProperty($this->name);
if (($this->isPublic() && !$property->isPublic()) || ($this->isProtected() && $property->isPrivate())) {
$this->modifiers |= self::ACCESS_LEVEL_CHANGED;
}
}
$this->modifiersComplete = ($this->modifiers & self::ACCESS_LEVEL_CHANGED) || $declaringClass->isComplete();
}
return $this->modifiers;
}
/**
* Returns if the property is private.
*
* @return boolean
*/
public function isPrivate()
{
return (bool) ($this->modifiers & InternalReflectionProperty::IS_PRIVATE);
}
/**
* Returns if the property is protected.
*
* @return boolean
*/
public function isProtected()
{
return (bool) ($this->modifiers & InternalReflectionProperty::IS_PROTECTED);
}
/**
* Returns if the property is public.
*
* @return boolean
*/
public function isPublic()
{
return (bool) ($this->modifiers & InternalReflectionProperty::IS_PUBLIC);
}
/**
* Returns if the poperty is static.
*
* @return boolean
*/
public function isStatic()
{
return (bool) ($this->modifiers & InternalReflectionProperty::IS_STATIC);
}
/**
* Returns the string representation of the reflection object.
*
* @return string
*/
public function __toString()
{
return sprintf(
"Property [ %s%s%s%s%s\$%s ]\n",
$this->isStatic() ? '' : '<default> ',
$this->isPublic() ? 'public ' : '',
$this->isPrivate() ? 'private ' : '',
$this->isProtected() ? 'protected ' : '',
$this->isStatic() ? 'static ' : '',
$this->getName()
);
}
/**
* Exports a reflected object.
*
* @param \TokenReflection\Broker $broker Broker instance
* @param string|object $class Class name or class instance
* @param string $property Property 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, $property, $return = false)
{
$className = is_object($class) ? get_class($class) : $class;
$propertyName = $property;
$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(sprintf('Class %s does not exist.', $className), Exception\RuntimeException::DOES_NOT_EXIST);
}
$property = $class->getProperty($propertyName);
if ($return) {
return $property->__toString();
}
echo $property->__toString();
}
/**
* Returns if the property is set accessible.
*
* @return boolean
*/
public function isAccessible()
{
return $this->accessible;
}
/**
* Sets a property to be accessible or not.
*
* @param boolean $accessible If the property should be accessible.
*/
public function setAccessible($accessible)
{
$this->accessible = (bool) $accessible;
}
/**
* Sets the property default value.
*
* @param mixed $value
*/
public function setDefaultValue($value)
{
$this->defaultValue = $value;
$this->defaultValueDefinition = var_export($value, true);
}
/**
* Sets value of a property for a particular class instance.
*
* @param object $object Class instance
* @param mixed $value Poperty value
* @throws \TokenReflection\Exception\RuntimeException If it is not possible to set the property value.
*/
public function setValue($object, $value)
{
$declaringClass = $this->getDeclaringClass();
if (!$declaringClass->isInstance($object)) {
throw new Exception\RuntimeException('Instance of or subclass expected.', Exception\RuntimeException::INVALID_ARGUMENT, $this);
}
if ($this->isPublic()) {
$object->{$this->name} = $value;
} elseif ($this->isAccessible()) {
$refClass = new InternalReflectionClass($object);
$refProperty = $refClass->getProperty($this->name);
$refProperty->setAccessible(true);
$refProperty->setValue($object, $value);
$refProperty->setAccessible(false);
if ($this->isStatic()) {
$this->setDefaultValue($value);
}
} else {
throw new Exception\RuntimeException('Only public and accessible properties can be set.', Exception\RuntimeException::NOT_ACCESSBILE, $this);
}
}
/**
* Returns imported namespaces and aliases from the declaring namespace.
*
* @return array
*/
public function getNamespaceAliases()
{
return $this->getDeclaringClass()->getNamespaceAliases();
}
/**
* Creates a property alias for the given class.
*
* @param \TokenReflection\ReflectionClass $parent New parent class
* @return \TokenReflection\ReflectionProperty
*/
public function alias(ReflectionClass $parent)
{
$property = clone $this;
$property->declaringClassName = $parent->getName();
return $property;
}
/**
* Returns the defining trait.
*
* @return \TokenReflection\IReflectionClass|null
*/
public function getDeclaringTrait()
{
return null === $this->declaringTraitName ? null : $this->getBroker()->getClass($this->declaringTraitName);
}
/**
* Returns the declaring trait name.
*
* @return string|null
*/
public function getDeclaringTraitName()
{
return $this->declaringTraitName;
}
/**
* Returns an element pretty (docblock compatible) name.
*
* @return string
*/
public function getPrettyName()
{
return sprintf('%s::$%s', $this->declaringClassName ?: $this->declaringTraitName, $this->name);
}
/**
* 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\Parse If an invalid parent reflection object was provided.
*/
protected function processParent(IReflection $parent, Stream $tokenStream)
{
if (!$parent instanceof ReflectionClass) {
throw new Exception\ParseException($this, $tokenStream, 'The parent object has to be an instance of TokenReflection\ReflectionClass.', Exception\ParseException::INVALID_PARENT);
}
$this->declaringClassName = $parent->getName();
if ($parent->isTrait()) {
$this->declaringTraitName = $parent->getName();
}
return parent::processParent($parent, $tokenStream);
}
/**
* Parses reflected element metadata from the token stream.
*
* @param \TokenReflection\Stream\StreamBase $tokenStream Token substream
* @param \TokenReflection\IReflection $parent Parent reflection object
* @return \TokenReflection\ReflectionProperty
*/
protected function parse(Stream $tokenStream, IReflection $parent)
{
$this->parseModifiers($tokenStream, $parent);
if (false === $this->docComment->getDocComment()) {
$this->parseDocComment($tokenStream, $parent);
}
return $this->parseName($tokenStream)
->parseDefaultValue($tokenStream);
}
/**
* Parses class modifiers (abstract, final) and class type (class, interface).
*
* @param \TokenReflection\Stream\StreamBase $tokenStream Token substream
* @param \TokenReflection\ReflectionClass $class Defining class
* @return \TokenReflection\ReflectionClass
* @throws \TokenReflection\Exception\ParseException If the modifiers value cannot be determined.
*/
private function parseModifiers(Stream $tokenStream, ReflectionClass $class)
{
while (true) {
switch ($tokenStream->getType()) {
case T_PUBLIC:
case T_VAR:
$this->modifiers |= InternalReflectionProperty::IS_PUBLIC;
break;
case T_PROTECTED:
$this->modifiers |= InternalReflectionProperty::IS_PROTECTED;
break;
case T_PRIVATE:
$this->modifiers |= InternalReflectionProperty::IS_PRIVATE;
break;
case T_STATIC:
$this->modifiers |= InternalReflectionProperty::IS_STATIC;
break;
default:
break 2;
}
$tokenStream->skipWhitespaces(true);
}
if (InternalReflectionProperty::IS_STATIC === $this->modifiers) {
$this->modifiers |= InternalReflectionProperty::IS_PUBLIC;
} elseif (0 === $this->modifiers) {
$parentProperties = $class->getOwnProperties();
if (empty($parentProperties)) {
throw new Exception\ParseException($this, $tokenStream, 'No access level defined and no previous defining class property present.', Exception\ParseException::LOGICAL_ERROR);
}
$sibling = array_pop($parentProperties);
if ($sibling->isPublic()) {
$this->modifiers = InternalReflectionProperty::IS_PUBLIC;
} elseif ($sibling->isPrivate()) {
$this->modifiers = InternalReflectionProperty::IS_PRIVATE;
} elseif ($sibling->isProtected()) {
$this->modifiers = InternalReflectionProperty::IS_PROTECTED;
} else {
throw new Exception\ParseException($this, $tokenStream, sprintf('Property sibling "%s" has no access level defined.', $sibling->getName()), Exception\Parse::PARSE_ELEMENT_ERROR);
}
if ($sibling->isStatic()) {
$this->modifiers |= InternalReflectionProperty::IS_STATIC;
}
}
return $this;
}
/**
* Parses the property name.
*
* @param \TokenReflection\Stream\StreamBase $tokenStream Token substream
* @return \TokenReflection\ReflectionProperty
* @throws \TokenReflection\Exception\ParseException If the property name could not be determined.
*/
protected function parseName(Stream $tokenStream)
{
if (!$tokenStream->is(T_VARIABLE)) {
throw new Exception\ParseException($this, $tokenStream, 'The property name could not be determined.', Exception\ParseException::LOGICAL_ERROR);
}
$this->name = substr($tokenStream->getTokenValue(), 1);
$tokenStream->skipWhitespaces(true);
return $this;
}
/**
* Parses the propety default value.
*
* @param \TokenReflection\Stream\StreamBase $tokenStream Token substream
* @return \TokenReflection\ReflectionProperty
* @throws \TokenReflection\Exception\ParseException If the property default value could not be determined.
*/
private function parseDefaultValue(Stream $tokenStream)
{
$type = $tokenStream->getType();
if (';' === $type || ',' === $type) {
// No default value
return $this;
}
if ('=' === $type) {
$tokenStream->skipWhitespaces(true);
}
$level = 0;
while (null !== ($type = $tokenStream->getType())) {
switch ($type) {
case ',':
if (0 !== $level) {
break;
}
case ';':
break 2;
case ')':
case ']':
case '}':
$level--;
break;
case '(':
case '{':
case '[':
$level++;
break;
default:
break;
}
$this->defaultValueDefinition[] = $tokenStream->current();
$tokenStream->next();
}
if (',' !== $type && ';' !== $type) {
throw new Exception\ParseException($this, $tokenStream, 'The property default value is not terminated properly. Expected "," or ";".', Exception\ParseException::UNEXPECTED_TOKEN);
}
return $this;
}
}
|