mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-27 04:24:43 +01:00
Merge branch '1.x'
This commit is contained in:
commit
e7a2abb03b
@ -9,6 +9,7 @@ use PhpParser\Node\Stmt;
|
||||
class Trait_ extends Declaration
|
||||
{
|
||||
protected $name;
|
||||
protected $properties = array();
|
||||
protected $methods = array();
|
||||
|
||||
/**
|
||||
@ -29,12 +30,15 @@ class Trait_ extends Declaration
|
||||
*/
|
||||
public function addStmt($stmt) {
|
||||
$stmt = $this->normalizeNode($stmt);
|
||||
if (!$stmt instanceof Stmt\ClassMethod) {
|
||||
|
||||
if ($stmt instanceof Stmt\Property) {
|
||||
$this->properties[] = $stmt;
|
||||
} else if ($stmt instanceof Stmt\ClassMethod) {
|
||||
$this->methods[] = $stmt;
|
||||
} else {
|
||||
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
|
||||
}
|
||||
|
||||
$this->methods[] = $stmt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -44,6 +48,8 @@ class Trait_ extends Declaration
|
||||
* @return Stmt\Trait_ The built interface node
|
||||
*/
|
||||
public function getNode() {
|
||||
return new Stmt\Trait_($this->name, $this->methods, $this->attributes);
|
||||
return new Stmt\Trait_(
|
||||
$this->name, array_merge($this->properties, $this->methods), $this->attributes
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -17,13 +17,17 @@ class TraitTest extends \PHPUnit_Framework_TestCase
|
||||
$method1 = new Stmt\ClassMethod('test1');
|
||||
$method2 = new Stmt\ClassMethod('test2');
|
||||
$method3 = new Stmt\ClassMethod('test3');
|
||||
$prop = new Stmt\Property(Stmt\Class_::MODIFIER_PUBLIC, array(
|
||||
new Stmt\PropertyProperty('test')
|
||||
));
|
||||
$trait = $this->createTraitBuilder('TestTrait')
|
||||
->setDocComment('/** Nice trait */')
|
||||
->addStmt($method1)
|
||||
->addStmts(array($method2, $method3))
|
||||
->addStmt($prop)
|
||||
->getNode();
|
||||
$this->assertEquals(new Stmt\Trait_('TestTrait', array(
|
||||
$method1, $method2, $method3
|
||||
$prop, $method1, $method2, $method3
|
||||
), array(
|
||||
'comments' => array(
|
||||
new Comment\Doc('/** Nice trait */')
|
||||
|
Loading…
Reference in New Issue
Block a user