Skip to content

Commit

Permalink
add getTraitUses() method to ClassLike
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored and nikic committed Aug 31, 2019
1 parent 005bb1d commit 006acba
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/PhpParser/Node/Stmt/ClassLike.php
Expand Up @@ -14,6 +14,19 @@ abstract class ClassLike extends Node\Stmt
/** @var Node\Stmt[] Statements */
public $stmts;

/**
* @return TraitUse[]
*/
public function getTraitUses() : array {
$traitUses = [];
foreach ($this->stmts as $stmt) {
if ($stmt instanceof TraitUse) {
$traitUses[] = $stmt;
}
}
return $traitUses;
}

/**
* @return ClassConst[]
*/
Expand Down
16 changes: 16 additions & 0 deletions test/PhpParser/Node/Stmt/ClassTest.php
Expand Up @@ -22,6 +22,22 @@ public function testIsFinal() {
$this->assertFalse($class->isFinal());
}

public function testGetTraitUses() {
$traitUses = [
new TraitUse([new Trait_('foo')]),
new TraitUse([new Trait_('bar')]),
];
$class = new Class_('Foo', [
'stmts' => [
$traitUses[0],
new ClassMethod('fooBar'),
$traitUses[1],
]
]);

$this->assertSame($traitUses, $class->getTraitUses());
}

public function testGetMethods() {
$methods = [
new ClassMethod('foo'),
Expand Down

0 comments on commit 006acba

Please sign in to comment.