Add Class->getMethods() function

This commit is contained in:
nikic 2012-07-07 16:24:07 +02:00
parent 4137d7a7a8
commit eb5991227d
3 changed files with 33 additions and 0 deletions

View File

@ -1,6 +1,9 @@
Version 0.9.2-dev
-----------------
* Add `Class->getMethods()` function, which returns all methods contained in the `stmts` array of the class node. This
does not take inherited methods into account.
* Add `isPublic()`, `isProtected()`, `isPrivate()`. `isAbstract()`, `isFinal()` and `isStatic()` accessors to the
`ClassMethod`, `Property` and `Class` nodes. (`Property` and `Class` obviously only have the accessors relevant to
them.)

View File

@ -68,6 +68,16 @@ class PHPParser_Node_Stmt_Class extends PHPParser_Node_Stmt
return (bool) ($this->type & self::MODIFIER_FINAL);
}
public function getMethods() {
$methods = array();
foreach ($this->stmts as $stmt) {
if ($stmt instanceof PHPParser_Node_Stmt_ClassMethod) {
$methods[] = $stmt;
}
}
return $methods;
}
public static function verifyModifier($a, $b) {
if ($a & 7 && $b & 7) {
throw new PHPParser_Error('Multiple access type modifiers are not allowed');

View File

@ -17,4 +17,24 @@ class PHPParser_Tests_Node_Stmt_ClassTest extends PHPUnit_Framework_TestCase
$class = new PHPParser_Node_Stmt_Class('Foo');
$this->assertFalse($class->isFinal());
}
public function testGetMethods() {
$methods = array(
new PHPParser_Node_Stmt_ClassMethod('foo'),
new PHPParser_Node_Stmt_ClassMethod('bar'),
new PHPParser_Node_Stmt_ClassMethod('fooBar'),
);
$class = new PHPParser_Node_Stmt_Class('Foo', array(
'stmts' => array(
new PHPParser_Node_Stmt_TraitUse(array()),
$methods[0],
new PHPParser_Node_Stmt_Const(array()),
$methods[1],
new PHPParser_Node_Stmt_Property(0, array()),
$methods[2],
)
));
$this->assertEquals($methods, $class->getMethods());
}
}