/** * 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; }
public static function fromReflection(\ReflectionMethod $ref) { $method = new static($ref->name); $method->setFinal($ref->isFinal())->setAbstract($ref->isAbstract())->setStatic($ref->isStatic())->setVisibility($ref->isPublic() ? self::VISIBILITY_PUBLIC : ($ref->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PRIVATE))->setReferenceReturned($ref->returnsReference())->setBody(ReflectionUtils::getFunctionBody($ref)); $docblock = new Docblock($ref); $method->setDocblock($docblock); $method->setDescription($docblock->getShortDescription()); $method->setLongDescription($docblock->getLongDescription()); foreach ($ref->getParameters() as $param) { $method->addParameter(static::createParameter($param)); } return $method; }
public static function fromReflection(\ReflectionFunction $ref) { $function = PhpFunction::create($ref->name)->setReferenceReturned($ref->returnsReference())->setBody(ReflectionUtils::getFunctionBody($ref)); $docblock = new Docblock($ref); $function->setDocblock($docblock); $function->setDescription($docblock->getShortDescription()); $function->setLongDescription($docblock->getLongDescription()); foreach ($ref->getParameters() as $refParam) { assert($refParam instanceof \ReflectionParameter); // hmm - assert here? $param = PhpParameter::fromReflection($refParam); $function->addParameter($param); } return $function; }
/** * 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; }
public function testGetUnindentedDocComment() { $writer = new Writer(); $comment = $writer->writeln('/**')->indent()->writeln(' * Foo.')->write(' */')->getContent(); $this->assertEquals("/**\n * Foo.\n */", ReflectionUtils::getUnindentedDocComment($comment)); }