Skip to content

Commit a25f71f

Browse files
committed
Fix getDeclaredFields()
1 parent 569c739 commit a25f71f

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ XP Framework Core ChangeLog
55

66
### Bugfixes
77

8+
* Fixed `lang.XPClass::getDeclaredFields()` throwing an exception - @thekid
89
* Fixed issue #276: PHP 8.1: offset* / getIterator signature. Made codebase
910
compatible with PHP 8.1 (*while keeping PHP 7 support!*) by adding the
1011
`#[ReturnTypeWillChange]` in relevant places.

src/main/php/lang/XPClass.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,9 @@ public function getFields() {
295295
*/
296296
public function getDeclaredFields() {
297297
$list= [];
298-
$reflect= $this->reflect()->name;
298+
$reflect= $this->reflect();
299299
foreach ($reflect->getProperties() as $p) {
300-
if ('__id' === $p->name || $p->class !== $reflect) continue;
300+
if ('__id' === $p->name || $p->class !== $reflect->name) continue;
301301
$list[]= new Field($this->_class, $p);
302302
}
303303
return $list;

src/test/php/net/xp_framework/unittest/reflection/FieldBasicsTest.class.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,38 @@ public function string_representation($declaration, $expected) {
6868
public function trait_field_type() {
6969
$this->assertEquals('int', $this->type()->getField('NOT_INSTANCE')->getTypeName());
7070
}
71+
72+
#[Test]
73+
public function all_fields() {
74+
$fixture= $this->type('{ public $a= "Test", $b; }', [
75+
'use' => []
76+
]);
77+
$this->assertEquals(
78+
['a', 'b'],
79+
array_map(function($f) { return $f->getName(); }, $fixture->getFields())
80+
);
81+
}
82+
83+
#[Test]
84+
public function all_fields_include_base_class() {
85+
$fixture= $this->type('{ public $declared= "Test"; public function getDate() { return null; }}', [
86+
'use' => [],
87+
'extends' => [AbstractTestClass::class]
88+
]);
89+
$this->assertEquals(
90+
['declared', 'inherited'],
91+
array_map(function($f) { return $f->getName(); }, $fixture->getFields())
92+
);
93+
}
94+
95+
#[Test]
96+
public function declared_fields() {
97+
$fixture= $this->type('{ public $a= "Test", $b; }', [
98+
'use' => []
99+
]);
100+
$this->assertEquals(
101+
['a', 'b'],
102+
array_map(function($f) { return $f->getName(); }, $fixture->getDeclaredFields())
103+
);
104+
}
71105
}

0 commit comments

Comments
 (0)