ReflectionConstant_.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2020 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy\Reflection;
  11. /**
  12. * Somehow the standard reflection library doesn't include constants.
  13. *
  14. * ReflectionConstant_ corrects that omission.
  15. *
  16. * Note: For backwards compatibility reasons, this class is named
  17. * ReflectionConstant_ rather than ReflectionConstant. It will be renamed in
  18. * v0.10.0.
  19. */
  20. class ReflectionConstant_ implements \Reflector
  21. {
  22. public $name;
  23. private $value;
  24. private static $magicConstants = [
  25. '__LINE__',
  26. '__FILE__',
  27. '__DIR__',
  28. '__FUNCTION__',
  29. '__CLASS__',
  30. '__TRAIT__',
  31. '__METHOD__',
  32. '__NAMESPACE__',
  33. '__COMPILER_HALT_OFFSET__',
  34. ];
  35. /**
  36. * Construct a ReflectionConstant_ object.
  37. *
  38. * @param string $name
  39. */
  40. public function __construct($name)
  41. {
  42. $this->name = $name;
  43. if (!\defined($name) && !self::isMagicConstant($name)) {
  44. throw new \InvalidArgumentException('Unknown constant: '.$name);
  45. }
  46. if (!self::isMagicConstant($name)) {
  47. $this->value = @\constant($name);
  48. }
  49. }
  50. /**
  51. * Exports a reflection.
  52. *
  53. * @param string $name
  54. * @param bool $return pass true to return the export, as opposed to emitting it
  55. *
  56. * @return string|null
  57. */
  58. public static function export($name, $return = false)
  59. {
  60. $refl = new self($name);
  61. $value = $refl->getValue();
  62. $str = \sprintf('Constant [ %s %s ] { %s }', \gettype($value), $refl->getName(), $value);
  63. if ($return) {
  64. return $str;
  65. }
  66. echo $str."\n";
  67. }
  68. public static function isMagicConstant($name)
  69. {
  70. return \in_array($name, self::$magicConstants);
  71. }
  72. /**
  73. * Get the constant's docblock.
  74. *
  75. * @return false
  76. */
  77. public function getDocComment()
  78. {
  79. return false;
  80. }
  81. /**
  82. * Gets the constant name.
  83. *
  84. * @return string
  85. */
  86. public function getName()
  87. {
  88. return $this->name;
  89. }
  90. /**
  91. * Gets the namespace name.
  92. *
  93. * Returns '' when the constant is not namespaced.
  94. *
  95. * @return string
  96. */
  97. public function getNamespaceName()
  98. {
  99. if (!$this->inNamespace()) {
  100. return '';
  101. }
  102. return \preg_replace('/\\\\[^\\\\]+$/', '', $this->name);
  103. }
  104. /**
  105. * Gets the value of the constant.
  106. *
  107. * @return mixed
  108. */
  109. public function getValue()
  110. {
  111. return $this->value;
  112. }
  113. /**
  114. * Checks if this constant is defined in a namespace.
  115. *
  116. * @return bool
  117. */
  118. public function inNamespace()
  119. {
  120. return \strpos($this->name, '\\') !== false;
  121. }
  122. /**
  123. * To string.
  124. *
  125. * @return string
  126. */
  127. public function __toString()
  128. {
  129. return $this->getName();
  130. }
  131. /**
  132. * Gets the constant's file name.
  133. *
  134. * Currently returns null, because if it returns a file name the signature
  135. * formatter will barf.
  136. */
  137. public function getFileName()
  138. {
  139. return;
  140. // return $this->class->getFileName();
  141. }
  142. /**
  143. * Get the code start line.
  144. *
  145. * @throws \RuntimeException
  146. */
  147. public function getStartLine()
  148. {
  149. throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
  150. }
  151. /**
  152. * Get the code end line.
  153. *
  154. * @throws \RuntimeException
  155. */
  156. public function getEndLine()
  157. {
  158. return $this->getStartLine();
  159. }
  160. }