public function testStaticCreation() { require_once __DIR__ . '/../Fixture/Functions.php'; $reflection = ReflectionFunction::createFromName('BetterReflectionTest\\Fixture\\myFunction'); $this->assertSame('myFunction', $reflection->getShortName()); }
/** * Create the parameter from the given spec. Possible $spec parameters are: * * - [$instance, 'method'] * - ['Foo', 'bar'] * - ['foo'] * - [function () {}] * * @param string[]|string|\Closure $spec * @param string $parameterName * @return ReflectionParameter * @throws \Exception * @throws \InvalidArgumentException */ public static function createFromSpec($spec, $parameterName) { if (is_array($spec) && count($spec) === 2) { if (is_object($spec[0])) { return self::createFromClassInstanceAndMethod($spec[0], $spec[1], $parameterName); } return self::createFromClassNameAndMethod($spec[0], $spec[1], $parameterName); } if (is_string($spec)) { return ReflectionFunction::createFromName($spec)->getParameter($parameterName); } if ($spec instanceof \Closure) { throw new \Exception('Creating by closure is not supported yet'); } throw new \InvalidArgumentException('Could not create reflection from the spec given'); }
/** * @param string $functionName * @param string $expectedStringValue * @dataProvider functionStringRepresentations */ public function testStringCast($functionName, $expectedStringValue) { require_once __DIR__ . '/../Fixture/Functions.php'; $functionInfo = ReflectionFunction::createFromName($functionName); $this->assertStringMatchesFormat($expectedStringValue, (string) $functionInfo); }