public final function parse(IFunctionReflection $reflection) { $innerReflection = $reflection->getInnerReflection(); if (!$innerReflection->isUserDefined()) { throw new InvalidFunctionException('Cannot parse function %s: function is not user defined', $innerReflection->getName()); } $filePath = $reflection->getLocation()->getFilePath(); if (!is_readable($filePath)) { throw new InvalidFunctionException('Cannot parse function %s: \'%s\' is not a valid accessible file', $innerReflection->getName(), $filePath); } try { return $this->parseFunction($reflection, $filePath); } catch (ASTException $astException) { throw InvalidFunctionException::invalidFunctionMessage($astException->getMessage(), $innerReflection); } }
/** * @param callable $function * * @return \ReflectionFunctionAbstract * @throws InvalidFunctionException */ public static final function fromCallable(callable $function) { // If is array it could be an instance or static method: // ['class', 'method'] or [$instance, 'method']; if (is_array($function)) { return new \ReflectionMethod($function[0], $function[1]); } elseif ($function instanceof \Closure) { $reflection = new \ReflectionFunction($function); // If the name is {closure} it as an actual closure if ($reflection->getShortName() === '{closure}') { return $reflection; } // Bail out, no (sane) way of determining the actual function // represented by the closure throw InvalidFunctionException::invalidFunctionMessage('The function has been wrapped in closure ' . '(most likely via ReflectionFunction::getClosure or \\ReflectionMethod::getClosure) ' . 'and this is not supported', $reflection); } elseif (is_object($function)) { return new \ReflectionMethod($function, '__invoke'); } else { $name = null; is_callable($function, false, $name); return new \ReflectionFunction($name); } }