Skip to content

Commit 8370789

Browse files
committed
Verify parameter default is accessible via reflection
See xp-framework/core#259
1 parent 96d45da commit 8370789

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/main/php/lang/ast/emit/PHP.class.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,9 @@ protected function emitMethod($result, $method) {
554554

555555
$promoted= '';
556556
foreach ($method->signature->parameters as $param) {
557+
$meta[DETAIL_TARGET_ANNO][$param->name]= $param->annotations;
558+
$meta[DETAIL_ARGUMENTS][]= $param->type ? $param->type->name() : 'var';
559+
557560
if (isset($param->promote)) {
558561
$promoted.= $param->promote.' $'.$param->name.';';
559562
$result->locals[1]['$this->'.$param->name]= new Code(($param->reference ? '&$' : '$').$param->name);
@@ -565,8 +568,10 @@ protected function emitMethod($result, $method) {
565568
DETAIL_ARGUMENTS => []
566569
];
567570
}
568-
$meta[DETAIL_TARGET_ANNO][$param->name]= $param->annotations;
569-
$meta[DETAIL_ARGUMENTS][]= $param->type ? $param->type->name() : 'var';
571+
572+
if (isset($param->default) && !$this->isConstant($param->default)) {
573+
$meta[DETAIL_TARGET_ANNO][$param->name]['default']= [$param->default];
574+
}
570575
}
571576

572577
if (null === $method->body) {

src/test/php/lang/ast/unittest/emit/InitializeWithExpressionsTest.class.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ public function run($h= new Handle(0)) {
149149
Assert::equals(new Handle(1), $r);
150150
}
151151

152+
#[Test]
153+
public function parameter_default_reflective_access() {
154+
$r= $this->run('use lang\ast\unittest\emit\Handle; class <T> {
155+
public function run($h= new Handle(0)) {
156+
return typeof($this)->getMethod("run")->getParameter(0)->getDefaultValue();
157+
}
158+
}');
159+
Assert::equals(new Handle(0), $r);
160+
}
161+
152162
#[Test]
153163
public function property_reference_as_parameter_default() {
154164
$r= $this->run('use lang\ast\unittest\emit\Handle; class <T> {

0 commit comments

Comments
 (0)