comparison vendor/psy/psysh/src/Psy/Reflection/ReflectionLanguageConstruct.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /*
4 * This file is part of Psy Shell.
5 *
6 * (c) 2012-2017 Justin Hileman
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Psy\Reflection;
13
14 /**
15 * A fake ReflectionFunction but for language constructs.
16 */
17 class ReflectionLanguageConstruct extends \ReflectionFunctionAbstract
18 {
19 public $keyword;
20
21 /**
22 * Language construct parameter definitions.
23 */
24 private static $languageConstructs = array(
25 'isset' => array(
26 'var' => array(),
27 '...' => array(
28 'isOptional' => true,
29 'defaultValue' => null,
30 ),
31 ),
32
33 'unset' => array(
34 'var' => array(),
35 '...' => array(
36 'isOptional' => true,
37 'defaultValue' => null,
38 ),
39 ),
40
41 'empty' => array(
42 'var' => array(),
43 ),
44
45 'echo' => array(
46 'arg1' => array(),
47 '...' => array(
48 'isOptional' => true,
49 'defaultValue' => null,
50 ),
51 ),
52
53 'print' => array(
54 'arg' => array(),
55 ),
56
57 'die' => array(
58 'status' => array(
59 'isOptional' => true,
60 'defaultValue' => 0,
61 ),
62 ),
63
64 'exit' => array(
65 'status' => array(
66 'isOptional' => true,
67 'defaultValue' => 0,
68 ),
69 ),
70 );
71
72 /**
73 * Construct a ReflectionLanguageConstruct object.
74 *
75 * @param string $name
76 */
77 public function __construct($keyword)
78 {
79 if (self::isLanguageConstruct($keyword)) {
80 throw new \InvalidArgumentException('Unknown language construct: ' . $keyword);
81 }
82
83 $this->keyword = $keyword;
84 }
85
86 /**
87 * This can't (and shouldn't) do anything :).
88 *
89 * @throws \RuntimeException
90 */
91 public static function export($name)
92 {
93 throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
94 }
95
96 /**
97 * Get language construct name.
98 *
99 * @return string
100 */
101 public function getName()
102 {
103 return $this->keyword;
104 }
105
106 /**
107 * None of these return references.
108 *
109 * @return bool
110 */
111 public function returnsReference()
112 {
113 return false;
114 }
115
116 /**
117 * Get language construct params.
118 *
119 * @return
120 */
121 public function getParameters()
122 {
123 $params = array();
124 foreach (self::$languageConstructs[$this->keyword] as $parameter => $opts) {
125 array_push($params, new ReflectionLanguageConstructParameter($this->keyword, $parameter, $opts));
126 }
127
128 return $params;
129 }
130
131 /**
132 * To string.
133 *
134 * @return string
135 */
136 public function __toString()
137 {
138 return $this->getName();
139 }
140
141 /**
142 * Check whether keyword is a (known) language construct.
143 *
144 * @param $keyword
145 *
146 * @return bool
147 */
148 public static function isLanguageConstruct($keyword)
149 {
150 return array_key_exists($keyword, self::$languageConstructs);
151 }
152 }