Author: Thomas Gossmann
Inheritance: extends AbstractPhpStruct, implements gossi\codegen\model\GenerateableInterface, implements gossi\codegen\model\TraitsInterface, implements gossi\codegen\model\PropertiesInterface, use trait gossi\codegen\model\parts\PropertiesPart, use trait gossi\codegen\model\parts\TraitsPart
 /**
  * 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;
 }
 /**
  * Removes a trait. 
  * 
  * If the trait is passed as PhpTrait object, 
  * the trait is also removed from use statements.
  *
  * @param PhpTrait|string $trait trait or qualified name
  * @return $this
  */
 public function removeTrait($trait)
 {
     if ($trait instanceof PhpTrait) {
         $name = $trait->getName();
     } else {
         $name = $trait;
     }
     $index = array_search($name, $this->traits);
     if ($index) {
         unset($this->traits[$name]);
         if ($trait instanceof PhpTrait) {
             $qname = $trait->getQualifiedName();
             $this->removeUseStatement($qname);
         }
     }
     return $this;
 }
 public function testSignature()
 {
     $expected = 'trait MyTrait {' . "\n" . '}';
     $trait = PhpTrait::create('MyTrait');
     $codegen = new CodeGenerator(['generateDocblock' => false, 'generateEmptyDocblock' => false]);
     $code = $codegen->generate($trait);
     $this->assertEquals($expected, $code);
 }
 public function testSignature()
 {
     $expected = 'trait MyTrait {' . "\n" . '}';
     $trait = PhpTrait::create('MyTrait');
     $generator = new ModelGenerator();
     $code = $generator->generate($trait);
     $this->assertEquals($expected, $code);
 }
 public function testCreate()
 {
     $class = PhpClass::create();
     $this->assertTrue($class instanceof PhpClass);
     $interface = PhpInterface::create();
     $this->assertTrue($interface instanceof PhpInterface);
     $trait = PhpTrait::create();
     $this->assertTrue($trait instanceof PhpTrait);
 }
 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 testEmptyTrait()
 {
     $trait = new PhpTrait();
     $trait->generateDocblock();
     $this->assertTrue($trait->getDocblock()->isEmpty());
 }
 public function testFromFile()
 {
     $expected = Fixtures::createDummyTrait();
     $actual = PhpTrait::fromFile(__DIR__ . '/../fixtures/DummyTrait.php');
     $this->assertEquals($expected, $actual);
 }
 private function buildSignature(PhpTrait $model)
 {
     $this->writer->write('trait ');
     $this->writer->write($model->getName());
 }
Exemple #10
0
 /**
  * Creates DummyTrait
  * 
  * @return PhpTrait
  */
 public static function createDummyTrait()
 {
     $trait = PhpTrait::create('DummyTrait')->setNamespace('gossi\\codegen\\tests\\fixtures')->setDescription('Dummy docblock')->setMethod(PhpMethod::create('foo')->setVisibility('public'))->setProperty(PhpProperty::create('iAmHidden')->setVisibility('private'))->addTrait('VeryDummyTrait');
     $trait->generateDocblock();
     return $trait;
 }