diff --git a/lib/PhpParser/Node/Stmt/ClassLike.php b/lib/PhpParser/Node/Stmt/ClassLike.php new file mode 100644 index 00000000..e8a873f7 --- /dev/null +++ b/lib/PhpParser/Node/Stmt/ClassLike.php @@ -0,0 +1,23 @@ +stmts as $stmt) { + if ($stmt instanceof ClassMethod) { + $methods[] = $stmt; + } + } + return $methods; + } +} diff --git a/lib/PhpParser/Node/Stmt/Class_.php b/lib/PhpParser/Node/Stmt/Class_.php index 3d34a32c..d92fe460 100644 --- a/lib/PhpParser/Node/Stmt/Class_.php +++ b/lib/PhpParser/Node/Stmt/Class_.php @@ -12,7 +12,7 @@ use PhpParser\Error; * @property Node\Name[] $implements Names of implemented interfaces * @property Node[] $stmts Statements */ -class Class_ extends Node\Stmt +class Class_ extends ClassLike { const MODIFIER_PUBLIC = 1; const MODIFIER_PROTECTED = 2; @@ -75,16 +75,6 @@ class Class_ extends Node\Stmt return (bool) ($this->type & self::MODIFIER_FINAL); } - public function getMethods() { - $methods = array(); - foreach ($this->stmts as $stmt) { - if ($stmt instanceof ClassMethod) { - $methods[] = $stmt; - } - } - return $methods; - } - /** * @internal */ diff --git a/lib/PhpParser/Node/Stmt/Interface_.php b/lib/PhpParser/Node/Stmt/Interface_.php index f3ab0ccb..af7b15a1 100644 --- a/lib/PhpParser/Node/Stmt/Interface_.php +++ b/lib/PhpParser/Node/Stmt/Interface_.php @@ -10,7 +10,7 @@ use PhpParser\Error; * @property Node\Name[] $extends Extended interfaces * @property Node[] $stmts Statements */ -class Interface_ extends Node\Stmt +class Interface_ extends ClassLike { protected static $specialNames = array( 'self' => true, diff --git a/lib/PhpParser/Node/Stmt/Trait_.php b/lib/PhpParser/Node/Stmt/Trait_.php index 0437e8cf..51ab2299 100644 --- a/lib/PhpParser/Node/Stmt/Trait_.php +++ b/lib/PhpParser/Node/Stmt/Trait_.php @@ -8,7 +8,7 @@ use PhpParser\Node; * @property string $name Name * @property Node[] $stmts Statements */ -class Trait_ extends Node\Stmt +class Trait_ extends ClassLike { /** * Constructs a trait node. @@ -26,4 +26,4 @@ class Trait_ extends Node\Stmt $attributes ); } -} \ No newline at end of file +} diff --git a/test/PhpParser/Node/Stmt/ClassTest.php b/test/PhpParser/Node/Stmt/ClassTest.php index c2e30ac6..464b2ae3 100644 --- a/test/PhpParser/Node/Stmt/ClassTest.php +++ b/test/PhpParser/Node/Stmt/ClassTest.php @@ -30,7 +30,7 @@ class ClassTest extends \PHPUnit_Framework_TestCase 'stmts' => array( new TraitUse(array()), $methods[0], - new Const_(array()), + new ClassConst(array()), $methods[1], new Property(0, array()), $methods[2], @@ -39,4 +39,4 @@ class ClassTest extends \PHPUnit_Framework_TestCase $this->assertSame($methods, $class->getMethods()); } -} \ No newline at end of file +} diff --git a/test/PhpParser/Node/Stmt/InterfaceTest.php b/test/PhpParser/Node/Stmt/InterfaceTest.php new file mode 100644 index 00000000..d5746d99 --- /dev/null +++ b/test/PhpParser/Node/Stmt/InterfaceTest.php @@ -0,0 +1,26 @@ + array( + new Node\Stmt\ClassConst(array(new Node\Const_('C1', new Node\Scalar\String('C1')))), + $methods[0], + new Node\Stmt\ClassConst(array(new Node\Const_('C2', new Node\Scalar\String('C2')))), + $methods[1], + new Node\Stmt\ClassConst(array(new Node\Const_('C3', new Node\Scalar\String('C3')))), + ) + )); + + $this->assertSame($methods, $interface->getMethods()); + } +}