public function testFromReflection() { if (PHP_VERSION_ID < 70000) { $this->markTestSkipped("Test is only valid for PHP >=7"); } $class = new PhpClass(); $class->setName('CG\\Tests\\Generator\\Fixture\\EntityPhp7')->setDocblock('/** * Doc Comment. * * @author Johannes M. Schmitt <*****@*****.**> */')->setProperty(PhpProperty::create('id')->setVisibility('private')->setDefaultValue(0)->setDocblock('/** * @var integer */')); $class->setMethod(PhpMethod::create()->setName('getId')->setDocblock('/** * @return int */')->setVisibility('public')->setReturnType('int')); $class->setMethod(PhpMethod::create()->setName('setId')->setVisibility('public')->setDocBlock('/** * @param int $id * @return EntityPhp7 */')->addParameter(PhpParameter::create()->setName('id')->setType('int')->setDefaultValue(null))->setReturnType('self')); $class->setMethod(PhpMethod::create()->setName('getTime')->setVisibility('public')->setReturnType('DateTime')); $class->setMethod(PhpMethod::create()->setName('getTimeZone')->setVisibility('public')->setReturnType('DateTimeZone')); $class->setMethod(PhpMethod::create()->setName('setTime')->setVisibility('public')->addParameter(PhpParameter::create()->setName('time')->setType('DateTime'))); $class->setMethod(PhpMethod::create()->setName('setTimeZone')->setVisibility('public')->addParameter(PhpParameter::create()->setName('timezone')->setType('DateTimeZone'))); $class->setMethod(PhpMethod::create()->setName('setArray')->setVisibility('public')->setReturnType('array')->addParameter(PhpParameter::create()->setName('array')->setDefaultValue(null)->setPassedByReference(true)->setType('array'))); $class->setMethod(PhpMethod::create()->setName('getFoo')->setReturnType('CG\\Tests\\Generator\\Fixture\\SubFixture\\Foo')); $class->setMethod(PhpMethod::create()->setName('getBar')->setReturnType('CG\\Tests\\Generator\\Fixture\\SubFixture\\Bar')); $class->setMethod(PhpMethod::create()->setName('getBaz')->setReturnType('CG\\Tests\\Generator\\Fixture\\SubFixture\\Baz')); $this->assertEquals($class, PhpClass::fromReflection(new \ReflectionClass('CG\\Tests\\Generator\\Fixture\\EntityPhp7'))); }
public function testVisitClassWithPhp7Features() { if (PHP_VERSION_ID < 70000) { $this->markTestSkipped('Test only valid for PHP >=7.0'); } $ref = new \ReflectionClass('CG\\Tests\\Generator\\Fixture\\EntityPhp7'); $class = PhpClass::fromReflection($ref); $generator = new DefaultGeneratorStrategy(); $content = $generator->generate($class); $this->assertEquals($this->getContent('php7_class.php'), $content); }
public function introspect(\ReflectionExtension $extension) { $classes = $functions = $constants = array(); foreach ($extension->getClasses() as $class) { assert($class instanceof \ReflectionClass); $phpClass = PhpClass::fromReflection($class); $classes[] = $phpClass; } foreach ($extension->getFunctions() as $function) { assert($function instanceof \ReflectionFunction); $phpFunction = PhpFunction::fromReflection($function); $functions[] = $phpFunction; } foreach ($extension->getConstants() as $name => $value) { $phpConstant = new PhpConstant($name); $phpConstant->setValue($value); $constants[] = $phpConstant; } return array('classes' => $classes, 'functions' => $functions, 'constants' => $constants); }
public function testFromReflection() { $class = new PhpClass(); $class->setName('CG\\Tests\\Generator\\Fixture\\Entity')->setAbstract(true)->setDocblock('/** * Doc Comment. * * @author Johannes M. Schmitt <*****@*****.**> */')->setProperty(PhpProperty::create('id')->setVisibility('private')->setDocblock('/** * @var integer */'))->setProperty(PhpProperty::create('enabled')->setVisibility('private')->setDefaultValue(false)); $method = PhpMethod::create()->setName('__construct')->setFinal(true)->addParameter(new PhpParameter('a'))->addParameter(PhpParameter::create()->setName('b')->setType('array')->setPassedByReference(true))->addParameter(PhpParameter::create()->setName('c')->setType('stdClass'))->addParameter(PhpParameter::create()->setName('d')->setDefaultValue('foo'))->setDocblock('/** * Another doc comment. * * @param unknown_type $a * @param array $b * @param \\stdClass $c * @param string $d */'); $class->setMethod($method); $class->setMethod(PhpMethod::create()->setName('foo')->setAbstract(true)->setVisibility('protected')); $class->setMethod(PhpMethod::create()->setName('bar')->setStatic(true)->setVisibility('private')); $this->assertEquals($class, PhpClass::fromReflection(new \ReflectionClass('CG\\Tests\\Generator\\Fixture\\Entity'))); }
/** * Generate aop methods proxies * * @param PhpClass $aopClass * @param PhpClass $parentClass */ private function generateAopMethodProxy(PhpClass $aopClass, PhpClass $parentClass) { foreach ($parentClass->getMethods() as $method) { /* @var $method \CG\Generator\PhpMethod */ try { $aopClass->getMethod($method->getName()); } catch (\InvalidArgumentException $e) { try { $proxyMethod = new ProxyMethod($method); $aopClass->setMethod($proxyMethod); } catch (ProxyMethodException $e) { } } } if ($parentClass->getParentClassName() != null) { $this->generateAopMethodProxy($aopClass, PhpClass::fromReflection(new \ReflectionClass($parentClass->getParentClassName()))); } }