1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14: 
 15: 
 16: namespace TokenReflection\Php;
 17: 
 18: use TokenReflection;
 19: use TokenReflection\Broker, TokenReflection\Exception;
 20: use Reflector, ReflectionFunction as InternalReflectionFunction, ReflectionParameter as InternalReflectionParameter;
 21: 
 22:  23:  24:  25:  26: 
 27: class ReflectionFunction extends InternalReflectionFunction implements IReflection, TokenReflection\IReflectionFunction
 28: {
 29:      30:  31:  32:  33: 
 34:     private $parameters;
 35: 
 36:      37:  38:  39:  40: 
 41:     private $broker;
 42: 
 43:      44:  45:  46:  47:  48: 
 49:     public function __construct($functionName, Broker $broker)
 50:     {
 51:         parent::__construct($functionName);
 52:         $this->broker = $broker;
 53:     }
 54: 
 55:      56:  57:  58:  59: 
 60:     public function getExtension()
 61:     {
 62:         return ReflectionExtension::create(parent::getExtension(), $this->broker);
 63:     }
 64: 
 65:      66:  67:  68:  69:  70: 
 71:     public function hasAnnotation($name)
 72:     {
 73:         return false;
 74:     }
 75: 
 76:      77:  78:  79:  80:  81: 
 82:     public function getAnnotation($name)
 83:     {
 84:         return null;
 85:     }
 86: 
 87:      88:  89:  90:  91: 
 92:     public function getAnnotations()
 93:     {
 94:         return array();
 95:     }
 96: 
 97:      98:  99: 100: 101: 
102:     public function isTokenized()
103:     {
104:         return false;
105:     }
106: 
107:     108: 109: 110: 111: 112: 113: 114: 
115:     public function getParameter($parameter)
116:     {
117:         $parameters = $this->getParameters();
118: 
119:         if (is_numeric($parameter)) {
120:             if (!isset($parameters[$parameter])) {
121:                 throw new Exception\RuntimeException(sprintf('There is no parameter at position "%d".', $parameter), Exception\RuntimeException::DOES_NOT_EXIST, $this);
122:             }
123: 
124:             return $parameters[$parameter];
125:         } else {
126:             foreach ($parameters as $reflection) {
127:                 if ($reflection->getName() === $parameter) {
128:                     return $reflection;
129:                 }
130:             }
131: 
132:             throw new Exception\RuntimeException(sprintf('There is no parameter "%s".', $parameter), Exception\RuntimeException::DOES_NOT_EXIST, $this);
133:         }
134:     }
135: 
136:     137: 138: 139: 140: 
141:     public function getParameters()
142:     {
143:         if (null === $this->parameters) {
144:             $broker = $this->broker;
145:             $parent = $this;
146:             $this->parameters = array_map(function(InternalReflectionParameter $parameter) use ($broker, $parent) {
147:                 return ReflectionParameter::create($parameter, $broker, $parent);
148:             }, parent::getParameters());
149:         }
150: 
151:         return $this->parameters;
152:     }
153: 
154:     155: 156: 157: 158: 
159:     public function getBroker()
160:     {
161:         return $this->broker;
162:     }
163: 
164:     165: 166: 167: 168: 
169:     public function getNamespaceAliases()
170:     {
171:         return array();
172:     }
173: 
174:     175: 176: 177: 178: 179: 
180:     final public function __get($key)
181:     {
182:         return TokenReflection\ReflectionElement::get($this, $key);
183:     }
184: 
185:     186: 187: 188: 189: 190: 
191:     final public function __isset($key)
192:     {
193:         return TokenReflection\ReflectionElement::exists($this, $key);
194:     }
195: 
196:     197: 198: 199: 200: 
201:     public function getClosure()
202:     {
203:         if (PHP_VERSION >= 50400) {
204:             return parent::getClosure();
205:         } else {
206:             $that = $this;
207:             return function() use ($that) {
208:                 return $that->invokeArgs(func_get_args());
209:             };
210:         }
211:     }
212: 
213:     214: 215: 216: 217: 
218:     public function getClosureScopeClass()
219:     {
220:         return PHP_VERSION >= 50400 ? parent::getClosureScopeClass() : null;
221:     }
222: 
223:     224: 225: 226: 227: 
228:     public function getClosureThis()
229:     {
230:         return PHP_VERSION >= 50400 ? parent::getClosureThis() : null;
231:     }
232: 
233:     234: 235: 236: 237: 238: 239: 
240:     public function isValid()
241:     {
242:         return true;
243:     }
244: 
245:     246: 247: 248: 249: 
250:     public function getPrettyName()
251:     {
252:         return $this->getName() . '()';
253:     }
254: 
255:     256: 257: 258: 259: 260: 261: 262: 
263:     public static function create(Reflector $internalReflection, Broker $broker)
264:     {
265:         if (!$internalReflection instanceof InternalReflectionFunction) {
266:             throw new Exception\RuntimeException('Invalid reflection instance provided, ReflectionFunction expected.', Exception\RuntimeException::INVALID_ARGUMENT);
267:         }
268: 
269:         return $broker->getFunction($internalReflection->getName());
270:     }
271: }