annotate vendor/psy/psysh/src/Psy/Reflection/ReflectionConstant.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of Psy Shell.
Chris@0 5 *
Chris@0 6 * (c) 2012-2017 Justin Hileman
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Psy\Reflection;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Somehow the standard reflection library doesn't include constants.
Chris@0 16 *
Chris@0 17 * ReflectionConstant corrects that omission.
Chris@0 18 */
Chris@0 19 class ReflectionConstant implements \Reflector
Chris@0 20 {
Chris@0 21 private $class;
Chris@0 22 private $name;
Chris@0 23 private $value;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Construct a ReflectionConstant object.
Chris@0 27 *
Chris@0 28 * @param mixed $class
Chris@0 29 * @param string $name
Chris@0 30 */
Chris@0 31 public function __construct($class, $name)
Chris@0 32 {
Chris@0 33 if (!$class instanceof \ReflectionClass) {
Chris@0 34 $class = new \ReflectionClass($class);
Chris@0 35 }
Chris@0 36
Chris@0 37 $this->class = $class;
Chris@0 38 $this->name = $name;
Chris@0 39
Chris@0 40 $constants = $class->getConstants();
Chris@0 41 if (!array_key_exists($name, $constants)) {
Chris@0 42 throw new \InvalidArgumentException('Unknown constant: ' . $name);
Chris@0 43 }
Chris@0 44
Chris@0 45 $this->value = $constants[$name];
Chris@0 46 }
Chris@0 47
Chris@0 48 /**
Chris@0 49 * Gets the declaring class.
Chris@0 50 *
Chris@0 51 * @return string
Chris@0 52 */
Chris@0 53 public function getDeclaringClass()
Chris@0 54 {
Chris@0 55 $parent = $this->class;
Chris@0 56
Chris@0 57 // Since we don't have real reflection constants, we can't see where
Chris@0 58 // it's actually defined. Let's check for a constant that is also
Chris@0 59 // available on the parent class which has exactly the same value.
Chris@0 60 //
Chris@0 61 // While this isn't _technically_ correct, it's prolly close enough.
Chris@0 62 do {
Chris@0 63 $class = $parent;
Chris@0 64 $parent = $class->getParentClass();
Chris@0 65 } while ($parent && $parent->hasConstant($this->name) && $parent->getConstant($this->name) === $this->value);
Chris@0 66
Chris@0 67 return $class;
Chris@0 68 }
Chris@0 69
Chris@0 70 /**
Chris@0 71 * Gets the constant name.
Chris@0 72 *
Chris@0 73 * @return string
Chris@0 74 */
Chris@0 75 public function getName()
Chris@0 76 {
Chris@0 77 return $this->name;
Chris@0 78 }
Chris@0 79
Chris@0 80 /**
Chris@0 81 * Gets the value of the constant.
Chris@0 82 *
Chris@0 83 * @return mixed
Chris@0 84 */
Chris@0 85 public function getValue()
Chris@0 86 {
Chris@0 87 return $this->value;
Chris@0 88 }
Chris@0 89
Chris@0 90 /**
Chris@0 91 * Gets the constant's file name.
Chris@0 92 *
Chris@0 93 * Currently returns null, because if it returns a file name the signature
Chris@0 94 * formatter will barf.
Chris@0 95 */
Chris@0 96 public function getFileName()
Chris@0 97 {
Chris@0 98 return;
Chris@0 99 // return $this->class->getFileName();
Chris@0 100 }
Chris@0 101
Chris@0 102 /**
Chris@0 103 * Get the code start line.
Chris@0 104 *
Chris@0 105 * @throws \RuntimeException
Chris@0 106 */
Chris@0 107 public function getStartLine()
Chris@0 108 {
Chris@0 109 throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
Chris@0 110 }
Chris@0 111
Chris@0 112 /**
Chris@0 113 * Get the code end line.
Chris@0 114 *
Chris@0 115 * @throws \RuntimeException
Chris@0 116 */
Chris@0 117 public function getEndLine()
Chris@0 118 {
Chris@0 119 return $this->getStartLine();
Chris@0 120 }
Chris@0 121
Chris@0 122 /**
Chris@0 123 * Get the constant's docblock.
Chris@0 124 *
Chris@0 125 * @return false
Chris@0 126 */
Chris@0 127 public function getDocComment()
Chris@0 128 {
Chris@0 129 return false;
Chris@0 130 }
Chris@0 131
Chris@0 132 /**
Chris@0 133 * Export the constant? I don't think this is possible.
Chris@0 134 *
Chris@0 135 * @throws \RuntimeException
Chris@0 136 */
Chris@0 137 public static function export()
Chris@0 138 {
Chris@0 139 throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
Chris@0 140 }
Chris@0 141
Chris@0 142 /**
Chris@0 143 * To string.
Chris@0 144 *
Chris@0 145 * @return string
Chris@0 146 */
Chris@0 147 public function __toString()
Chris@0 148 {
Chris@0 149 return $this->getName();
Chris@0 150 }
Chris@0 151 }