Skip to content

Commit

Permalink
add getProperties() and getConstants() to ClassLike
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored and nikic committed Aug 30, 2019
1 parent 40e7b67 commit 005bb1d
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 4 deletions.
26 changes: 26 additions & 0 deletions lib/PhpParser/Node/Stmt/ClassLike.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/Class_.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static function verifyModifier($a, $b) {
throw new Error('Cannot use the final modifier on an abstract class member');
}
}

public function getType() : string {
return 'Stmt_Class';
}
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/Interface_.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct($name, array $subNodes = [], array $attributes = [])
public function getSubNodeNames() : array {
return ['name', 'extends', 'stmts'];
}

public function getType() : string {
return 'Stmt_Interface';
}
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/Trait_.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct($name, array $subNodes = [], array $attributes = [])
public function getSubNodeNames() : array {
return ['name', 'stmts'];
}

public function getType() : string {
return 'Stmt_Trait';
}
Expand Down
45 changes: 45 additions & 0 deletions test/PhpParser/Builder/TraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
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
{
Expand Down Expand Up @@ -43,4 +49,43 @@ public function testInvalidStmtError() {
->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());
}
}
38 changes: 38 additions & 0 deletions test/PhpParser/Node/Stmt/ClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpParser\Node\Stmt;

use PhpParser\Node\Scalar\String_;

class ClassTest extends \PHPUnit\Framework\TestCase
{
public function testIsAbstract() {
Expand Down Expand Up @@ -40,6 +42,42 @@ public function testGetMethods() {
$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');
Expand Down
20 changes: 19 additions & 1 deletion test/PhpParser/Node/Stmt/InterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpParser\Node\Stmt;

use PhpParser\Node;
use PhpParser\Node\Scalar\String_;

class InterfaceTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -11,7 +12,7 @@ public function testGetMethods() {
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],
Expand All @@ -23,4 +24,21 @@ public function testGetMethods() {

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

0 comments on commit 005bb1d

Please sign in to comment.