mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-27 04:24:43 +01:00
Move getMethods() to new ClassLike node
Class_, Interface_ and Trait_ extend the ClassLike node, which provides the getMethods() method.
This commit is contained in:
parent
6930feac43
commit
1366e833a1
23
lib/PhpParser/Node/Stmt/ClassLike.php
Normal file
23
lib/PhpParser/Node/Stmt/ClassLike.php
Normal 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;
|
||||
}
|
||||
}
|
@ -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
|
||||
*/
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
26
test/PhpParser/Node/Stmt/InterfaceTest.php
Normal file
26
test/PhpParser/Node/Stmt/InterfaceTest.php
Normal 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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user