Move getMethods() to new ClassLike node

Class_, Interface_ and Trait_ extend the ClassLike node, which
provides the getMethods() method.
This commit is contained in:
Tom Rochette 2015-01-31 22:59:38 +01:00 committed by Nikita Popov
parent 6930feac43
commit 1366e833a1
6 changed files with 55 additions and 16 deletions

View File

@ -0,0 +1,23 @@
<?php
namespace PhpParser\Node\Stmt;
use PhpParser\Node;
/**
* Class, interface or trait.
*
* @property string $name Name
* @property Node[] $stmts Statements
*/
abstract class ClassLike extends Node\Stmt {
public function getMethods() {
$methods = array();
foreach ($this->stmts as $stmt) {
if ($stmt instanceof ClassMethod) {
$methods[] = $stmt;
}
}
return $methods;
}
}

View File

@ -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
*/

View File

@ -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,

View File

@ -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
);
}
}
}

View File

@ -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());
}
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace PhpParser\Node\Stmt;
use PhpParser\Node;
class InterfaceTest extends \PHPUnit_Framework_TestCase
{
public function testGetMethods() {
$methods = array(
new ClassMethod('foo'),
new ClassMethod('bar'),
);
$interface = new Class_('Foo', array(
'stmts' => 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());
}
}