/** * Creates a PHP class from reflection * * @param \ReflectionClass $ref * @return PhpClass */ public static function fromReflection(\ReflectionClass $ref) { $class = new static(); $class->setQualifiedName($ref->name)->setAbstract($ref->isAbstract())->setFinal($ref->isFinal())->setUseStatements(ReflectionUtils::getUseStatements($ref)); if ($ref->getDocComment()) { $docblock = new Docblock($ref); $class->setDocblock($docblock); $class->setDescription($docblock->getShortDescription()); $class->setLongDescription($docblock->getLongDescription()); } // methods foreach ($ref->getMethods() as $method) { $class->setMethod(static::createMethod($method)); } // properties foreach ($ref->getProperties() as $property) { $class->setProperty(static::createProperty($property)); } // traits foreach ($ref->getTraits() as $trait) { $class->addTrait(PhpTrait::fromReflection($trait)); } // constants // TODO: https://github.com/gossi/php-code-generator/issues/19 $class->setConstants($ref->getConstants()); return $class; }
/** * Creates a PHP interface from reflection * * @param \ReflectionClass $ref * @return PhpInterface */ public static function fromReflection(\ReflectionClass $ref) { $interface = new static(); $interface->setQualifiedName($ref->name)->setConstants($ref->getConstants())->setUseStatements(ReflectionUtils::getUseStatements($ref)); $docblock = new Docblock($ref); $interface->setDocblock($docblock); $interface->setDescription($docblock->getShortDescription()); $interface->setLongDescription($docblock->getLongDescription()); foreach ($ref->getMethods() as $method) { $method = static::createMethod($method); $method->setAbstract(false); $interface->setMethod($method); } return $interface; }
public function testUseStatements() { $class = new PhpClass(); $this->assertEquals(array(), $class->getUseStatements()); $this->assertSame($class, $class->setUseStatements(array('foo' => 'bar'))); $this->assertEquals(array('foo' => 'bar'), $class->getUseStatements()); $this->assertSame($class, $class->addUseStatement('Foo\\Bar')); $this->assertEquals(array('foo' => 'bar', 'Bar' => 'Foo\\Bar'), $class->getUseStatements()); $this->assertSame($class, $class->addUseStatement('Foo\\Bar', 'Baz')); $this->assertEquals(array('foo' => 'bar', 'Bar' => 'Foo\\Bar', 'Baz' => 'Foo\\Bar'), $class->getUseStatements()); $this->assertTrue($class->hasUseStatement('bar')); $class->removeUseStatement('bar'); $this->assertFalse($class->hasUseStatement('bar')); // from reflection require_once __DIR__ . '/../fixture/DummyTrait.php'; $statements = ReflectionUtils::getUseStatements(new \ReflectionClass('gossi\\codegen\\tests\\fixture\\DummyTrait')); $this->assertEquals(['gossi\\codegen\\tests\\fixture\\VeryDummyTrait'], $statements); require_once __DIR__ . '/../fixture/ClassWithTraits.php'; $statements = ReflectionUtils::getUseStatements(new \ReflectionClass('gossi\\codegen\\tests\\fixture\\ClassWithTraits')); $this->assertEquals(['DT' => 'gossi\\codegen\\tests\\fixture\\DummyTrait'], $statements); }
public static function fromReflection(\ReflectionClass $ref) { $trait = new static(); $trait->setQualifiedName($ref->name); $trait->setUseStatements(ReflectionUtils::getUseStatements($ref)); $docblock = new Docblock($ref); $trait->setDocblock($docblock); $trait->setDescription($docblock->getShortDescription()); $trait->setLongDescription($docblock->getLongDescription()); // traits foreach ($ref->getTraits() as $reflectionTrait) { $trait->addTrait(PhpTrait::fromReflection($reflectionTrait)); } // properties foreach ($ref->getProperties() as $property) { $trait->setProperty(static::createProperty($property)); } // methods foreach ($ref->getMethods() as $method) { $trait->setMethod(static::createMethod($method)); } return $trait; }