mirror of
https://github.com/danog/PHP-Parser.git
synced 2024-11-26 20:04:48 +01:00
Add getProperty
to ClassLike
This commit is contained in:
parent
f56d90d4f8
commit
64f4d5b619
@ -53,6 +53,26 @@ abstract class ClassLike extends Node\Stmt
|
||||
return $properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets property with the given name defined directly in this class/interface/trait.
|
||||
*
|
||||
* @param string $name Name of the property
|
||||
*
|
||||
* @return Property|null Property node or null if the property does not exist
|
||||
*/
|
||||
public function getProperty(string $name) {
|
||||
foreach ($this->stmts as $stmt) {
|
||||
if ($stmt instanceof Property) {
|
||||
foreach ($stmt->props as $prop) {
|
||||
if ($prop instanceof PropertyProperty && $name === $prop->name->toString()) {
|
||||
return $stmt;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all methods defined directly in this class/interface/trait
|
||||
*
|
||||
|
@ -94,6 +94,31 @@ class ClassTest extends \PHPUnit\Framework\TestCase
|
||||
$this->assertSame($properties, $class->getProperties());
|
||||
}
|
||||
|
||||
public function testGetProperty() {
|
||||
$properties = [
|
||||
$fooProp = new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('foo1')]),
|
||||
$barProp = new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('BAR1')]),
|
||||
$fooBarProp = new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('foo2'), new PropertyProperty('bar2')]),
|
||||
];
|
||||
$class = new Class_('Foo', [
|
||||
'stmts' => [
|
||||
new TraitUse([]),
|
||||
$properties[0],
|
||||
new ClassConst([]),
|
||||
$properties[1],
|
||||
new ClassMethod('fooBar'),
|
||||
$properties[2],
|
||||
]
|
||||
]);
|
||||
|
||||
$this->assertSame($fooProp, $class->getProperty('foo1'));
|
||||
$this->assertSame($barProp, $class->getProperty('BAR1'));
|
||||
$this->assertSame($fooBarProp, $class->getProperty('foo2'));
|
||||
$this->assertSame($fooBarProp, $class->getProperty('bar2'));
|
||||
$this->assertNull($class->getProperty('bar1'));
|
||||
$this->assertNull($class->getProperty('nonExisting'));
|
||||
}
|
||||
|
||||
public function testGetMethod() {
|
||||
$methodConstruct = new ClassMethod('__CONSTRUCT');
|
||||
$methodTest = new ClassMethod('test');
|
||||
|
Loading…
Reference in New Issue
Block a user