mirror of
https://github.com/danog/PHP-Parser.git
synced 2024-11-26 20:04:48 +01:00
add getProperties() and getConstants() to ClassLike
This commit is contained in:
parent
40e7b67d69
commit
005bb1dba7
@ -14,6 +14,32 @@ abstract class ClassLike extends Node\Stmt
|
||||
/** @var Node\Stmt[] Statements */
|
||||
public $stmts;
|
||||
|
||||
/**
|
||||
* @return ClassConst[]
|
||||
*/
|
||||
public function getConstants() : array {
|
||||
$constants = [];
|
||||
foreach ($this->stmts as $stmt) {
|
||||
if ($stmt instanceof ClassConst) {
|
||||
$constants[] = $stmt;
|
||||
}
|
||||
}
|
||||
return $constants;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Property[]
|
||||
*/
|
||||
public function getProperties() : array {
|
||||
$properties = [];
|
||||
foreach ($this->stmts as $stmt) {
|
||||
if ($stmt instanceof Property) {
|
||||
$properties[] = $stmt;
|
||||
}
|
||||
}
|
||||
return $properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all methods defined directly in this class/interface/trait
|
||||
*
|
||||
|
@ -5,6 +5,12 @@ namespace PhpParser\Builder;
|
||||
use PhpParser\Comment;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PhpParser\Node\Stmt\PropertyProperty;
|
||||
use PhpParser\Node\Stmt\TraitUse;
|
||||
|
||||
class TraitTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
@ -43,4 +49,43 @@ class TraitTest extends \PHPUnit\Framework\TestCase
|
||||
->addStmt(new Stmt\Echo_([]))
|
||||
;
|
||||
}
|
||||
|
||||
public function testGetMethods() {
|
||||
$methods = [
|
||||
new ClassMethod('foo'),
|
||||
new ClassMethod('bar'),
|
||||
new ClassMethod('fooBar'),
|
||||
];
|
||||
$trait = new Stmt\Trait_('Foo', [
|
||||
'stmts' => [
|
||||
new TraitUse([]),
|
||||
$methods[0],
|
||||
new ClassConst([]),
|
||||
$methods[1],
|
||||
new Property(0, []),
|
||||
$methods[2],
|
||||
]
|
||||
]);
|
||||
|
||||
$this->assertSame($methods, $trait->getMethods());
|
||||
}
|
||||
|
||||
public function testGetProperties()
|
||||
{
|
||||
$properties = [
|
||||
new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('foo')]),
|
||||
new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('bar')]),
|
||||
];
|
||||
$trait = new Stmt\Trait_('Foo', [
|
||||
'stmts' => [
|
||||
new TraitUse([]),
|
||||
$properties[0],
|
||||
new ClassConst([]),
|
||||
$properties[1],
|
||||
new ClassMethod('fooBar'),
|
||||
]
|
||||
]);
|
||||
|
||||
$this->assertSame($properties, $trait->getProperties());
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
namespace PhpParser\Node\Stmt;
|
||||
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
|
||||
class ClassTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testIsAbstract() {
|
||||
@ -40,6 +42,42 @@ class ClassTest extends \PHPUnit\Framework\TestCase
|
||||
$this->assertSame($methods, $class->getMethods());
|
||||
}
|
||||
|
||||
public function testGetConstants() {
|
||||
$constants = [
|
||||
new ClassConst([new \PhpParser\Node\Const_('foo', new String_('foo_value'))]),
|
||||
new ClassConst([new \PhpParser\Node\Const_('bar', new String_('bar_value'))]),
|
||||
];
|
||||
$class = new Class_('Foo', [
|
||||
'stmts' => [
|
||||
new TraitUse([]),
|
||||
$constants[0],
|
||||
new ClassMethod('fooBar'),
|
||||
$constants[1],
|
||||
]
|
||||
]);
|
||||
|
||||
$this->assertSame($constants, $class->getConstants());
|
||||
}
|
||||
|
||||
public function testGetProperties()
|
||||
{
|
||||
$properties = [
|
||||
new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('foo')]),
|
||||
new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('bar')]),
|
||||
];
|
||||
$class = new Class_('Foo', [
|
||||
'stmts' => [
|
||||
new TraitUse([]),
|
||||
$properties[0],
|
||||
new ClassConst([]),
|
||||
$properties[1],
|
||||
new ClassMethod('fooBar'),
|
||||
]
|
||||
]);
|
||||
|
||||
$this->assertSame($properties, $class->getProperties());
|
||||
}
|
||||
|
||||
public function testGetMethod() {
|
||||
$methodConstruct = new ClassMethod('__CONSTRUCT');
|
||||
$methodTest = new ClassMethod('test');
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace PhpParser\Node\Stmt;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
|
||||
class InterfaceTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
@ -11,7 +12,7 @@ class InterfaceTest extends \PHPUnit\Framework\TestCase
|
||||
new ClassMethod('foo'),
|
||||
new ClassMethod('bar'),
|
||||
];
|
||||
$interface = new Class_('Foo', [
|
||||
$interface = new Interface_('Foo', [
|
||||
'stmts' => [
|
||||
new Node\Stmt\ClassConst([new Node\Const_('C1', new Node\Scalar\String_('C1'))]),
|
||||
$methods[0],
|
||||
@ -23,4 +24,21 @@ class InterfaceTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
$this->assertSame($methods, $interface->getMethods());
|
||||
}
|
||||
|
||||
public function testGetConstants() {
|
||||
$constants = [
|
||||
new ClassConst([new \PhpParser\Node\Const_('foo', new String_('foo_value'))]),
|
||||
new ClassConst([new \PhpParser\Node\Const_('bar', new String_('bar_value'))]),
|
||||
];
|
||||
$class = new Interface_('Foo', [
|
||||
'stmts' => [
|
||||
new TraitUse([]),
|
||||
$constants[0],
|
||||
new ClassMethod('fooBar'),
|
||||
$constants[1],
|
||||
]
|
||||
]);
|
||||
|
||||
$this->assertSame($constants, $class->getConstants());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user