From 006acba06624279f35c652cd1da11cefbdd4bd14 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 31 Aug 2019 00:36:19 +0200 Subject: [PATCH] add getTraitUses() method to ClassLike --- lib/PhpParser/Node/Stmt/ClassLike.php | 13 +++++++++++++ test/PhpParser/Node/Stmt/ClassTest.php | 16 ++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/PhpParser/Node/Stmt/ClassLike.php b/lib/PhpParser/Node/Stmt/ClassLike.php index 4ae2d3b..b1cd326 100644 --- a/lib/PhpParser/Node/Stmt/ClassLike.php +++ b/lib/PhpParser/Node/Stmt/ClassLike.php @@ -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[] */ diff --git a/test/PhpParser/Node/Stmt/ClassTest.php b/test/PhpParser/Node/Stmt/ClassTest.php index 202a1d5..accf813 100644 --- a/test/PhpParser/Node/Stmt/ClassTest.php +++ b/test/PhpParser/Node/Stmt/ClassTest.php @@ -22,6 +22,22 @@ class ClassTest extends \PHPUnit\Framework\TestCase $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'),