ReflectionLanguageConstruct.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2018 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. * A fake ReflectionFunction but for language constructs.
  13. */
  14. class ReflectionLanguageConstruct extends \ReflectionFunctionAbstract
  15. {
  16. public $keyword;
  17. /**
  18. * Language construct parameter definitions.
  19. */
  20. private static $languageConstructs = [
  21. 'isset' => [
  22. 'var' => [],
  23. '...' => [
  24. 'isOptional' => true,
  25. 'defaultValue' => null,
  26. ],
  27. ],
  28. 'unset' => [
  29. 'var' => [],
  30. '...' => [
  31. 'isOptional' => true,
  32. 'defaultValue' => null,
  33. ],
  34. ],
  35. 'empty' => [
  36. 'var' => [],
  37. ],
  38. 'echo' => [
  39. 'arg1' => [],
  40. '...' => [
  41. 'isOptional' => true,
  42. 'defaultValue' => null,
  43. ],
  44. ],
  45. 'print' => [
  46. 'arg' => [],
  47. ],
  48. 'die' => [
  49. 'status' => [
  50. 'isOptional' => true,
  51. 'defaultValue' => 0,
  52. ],
  53. ],
  54. 'exit' => [
  55. 'status' => [
  56. 'isOptional' => true,
  57. 'defaultValue' => 0,
  58. ],
  59. ],
  60. ];
  61. /**
  62. * Construct a ReflectionLanguageConstruct object.
  63. *
  64. * @param string $keyword
  65. */
  66. public function __construct($keyword)
  67. {
  68. if (!self::isLanguageConstruct($keyword)) {
  69. throw new \InvalidArgumentException('Unknown language construct: ' . $keyword);
  70. }
  71. $this->keyword = $keyword;
  72. }
  73. /**
  74. * This can't (and shouldn't) do anything :).
  75. *
  76. * @throws \RuntimeException
  77. */
  78. public static function export($name)
  79. {
  80. throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
  81. }
  82. /**
  83. * Get language construct name.
  84. *
  85. * @return string
  86. */
  87. public function getName()
  88. {
  89. return $this->keyword;
  90. }
  91. /**
  92. * None of these return references.
  93. *
  94. * @return bool
  95. */
  96. public function returnsReference()
  97. {
  98. return false;
  99. }
  100. /**
  101. * Get language construct params.
  102. *
  103. * @return array
  104. */
  105. public function getParameters()
  106. {
  107. $params = [];
  108. foreach (self::$languageConstructs[$this->keyword] as $parameter => $opts) {
  109. \array_push($params, new ReflectionLanguageConstructParameter($this->keyword, $parameter, $opts));
  110. }
  111. return $params;
  112. }
  113. /**
  114. * Gets the file name from a language construct.
  115. *
  116. * (Hint: it always returns false)
  117. *
  118. * @return bool false
  119. */
  120. public function getFileName()
  121. {
  122. return false;
  123. }
  124. /**
  125. * To string.
  126. *
  127. * @return string
  128. */
  129. public function __toString()
  130. {
  131. return $this->getName();
  132. }
  133. /**
  134. * Check whether keyword is a (known) language construct.
  135. *
  136. * @param string $keyword
  137. *
  138. * @return bool
  139. */
  140. public static function isLanguageConstruct($keyword)
  141. {
  142. return \array_key_exists($keyword, self::$languageConstructs);
  143. }
  144. }