From 1366e833a16c4267883187da6da7b317f0582dd7 Mon Sep 17 00:00:00 2001 From: Tom Rochette Date: Sat, 31 Jan 2015 22:59:38 +0100 Subject: [PATCH] Move getMethods() to new ClassLike node Class_, Interface_ and Trait_ extend the ClassLike node, which provides the getMethods() method. --- lib/PhpParser/Node/Stmt/ClassLike.php | 23 +++++++++++++++++++ lib/PhpParser/Node/Stmt/Class_.php | 12 +--------- lib/PhpParser/Node/Stmt/Interface_.php | 2 +- lib/PhpParser/Node/Stmt/Trait_.php | 4 ++-- test/PhpParser/Node/Stmt/ClassTest.php | 4 ++-- test/PhpParser/Node/Stmt/InterfaceTest.php | 26 ++++++++++++++++++++++ 6 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 lib/PhpParser/Node/Stmt/ClassLike.php create mode 100644 test/PhpParser/Node/Stmt/InterfaceTest.php diff --git a/lib/PhpParser/Node/Stmt/ClassLike.php b/lib/PhpParser/Node/Stmt/ClassLike.php new file mode 100644 index 0000000..e8a873f --- /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 3d34a32..d92fe46 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 f3ab0cc..af7b15a 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 0437e8c..51ab229 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 c2e30ac..464b2ae 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 0000000..d5746d9 --- /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()); + } +}