Skip to content

Commit 836a689

Browse files
authored
Merge pull request #44 from xp-framework/feature/final-properties
Implement support for final properties
2 parents 03cec33 + c5daa42 commit 836a689

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/main/php/lang/meta/MetaInformation.class.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,23 @@ public function propertyComment($reflect) {
192192
}
193193
}
194194

195+
/**
196+
* Returns modifiers for a given property, including non-declared
197+
*
198+
* @param \ReflectionProperty $reflect
199+
* @return int
200+
*/
201+
public function propertyModifiers($reflect) {
202+
$name= $reflect->getDeclaringClass()->name;
203+
$c= \xp::$cn[$name] ?? strtr($name, '\\', '.');
204+
if ($meta= \xp::$meta[$c][0][$reflect->getName()][DETAIL_ARGUMENTS] ?? null) {
205+
return $reflect->getModifiers() | (int)$meta[0];
206+
} else {
207+
$tags= $this->tags($reflect);
208+
return $reflect->getModifiers() | (isset($tags['final']) ? MODIFIER_FINAL : 0);
209+
}
210+
}
211+
195212
/**
196213
* Returns annotation map (type => arguments) for a given method
197214
*

src/main/php/lang/reflection/Property.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function modifiers($hook= null) {
5353
];
5454

5555
// Readonly implies protected(set)
56-
$bits= $this->reflect->getModifiers();
56+
$bits= Reflection::meta()->propertyModifiers($this->reflect);
5757
$bits & Modifiers::IS_READONLY && $bits|= Modifiers::IS_PROTECTED_SET;
5858

5959
switch ($hook) {

src/test/php/lang/reflection/unittest/PropertiesTest.class.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ public function modifiers() {
3434
);
3535
}
3636

37+
#[Test]
38+
public function modifiers_tagged_final() {
39+
Assert::equals(
40+
new Modifiers(['public', 'final']),
41+
$this->declare('{ /** @final */ public $fixture; }')->property('fixture')->modifiers()
42+
);
43+
}
44+
45+
#[Test, Runtime(php: '>=8.4')]
46+
public function modifiers_declared_final() {
47+
Assert::equals(
48+
new Modifiers(['public', 'final']),
49+
$this->declare('{ public final $fixture; }')->property('fixture')->modifiers()
50+
);
51+
}
52+
3753
#[Test, Values(['public', 'protected', 'private'])]
3854
public function get_modifiers($modifier) {
3955
Assert::equals(

0 commit comments

Comments
 (0)