diff --git a/composer.json b/composer.json index 1084a615..270c52bd 100755 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ "php" : ">=7.0.0" }, "require-dev" : { + "xp-framework/reflection": "dev-feature/typed_class_constants as 2.11.0", "xp-framework/test": "^1.0" }, "bin": ["bin/xp.xp-framework.compiler.compile", "bin/xp.xp-framework.compiler.ast"], diff --git a/src/main/php/lang/ast/emit/OmitConstModifiers.class.php b/src/main/php/lang/ast/emit/OmitConstModifiers.class.php index 465590bc..cab1aacd 100755 --- a/src/main/php/lang/ast/emit/OmitConstModifiers.class.php +++ b/src/main/php/lang/ast/emit/OmitConstModifiers.class.php @@ -9,6 +9,14 @@ trait OmitConstModifiers { protected function emitConst($result, $const) { + $result->codegen->scope[0]->meta[self::CONSTANT][$const->name]= [ + DETAIL_RETURNS => $const->type ? $const->type->name() : 'var', + DETAIL_ANNOTATIONS => $const->annotations, + DETAIL_COMMENT => $const->comment, + DETAIL_TARGET_ANNO => [], + DETAIL_ARGUMENTS => [] + ]; + $result->out->write('const '.$const->name.'='); $this->emitOne($result, $const->expression); $result->out->write(';'); diff --git a/src/main/php/lang/ast/emit/PHP.class.php b/src/main/php/lang/ast/emit/PHP.class.php index 6842b785..49ffc37f 100755 --- a/src/main/php/lang/ast/emit/PHP.class.php +++ b/src/main/php/lang/ast/emit/PHP.class.php @@ -21,6 +21,7 @@ abstract class PHP extends Emitter { const PROPERTY = 0; const METHOD = 1; + const CONSTANT = 2; protected $literals= []; @@ -562,6 +563,14 @@ protected function emitUse($result, $use) { } protected function emitConst($result, $const) { + $result->codegen->scope[0]->meta[self::CONSTANT][$const->name]= [ + DETAIL_RETURNS => $const->type ? $const->type->name() : 'var', + DETAIL_ANNOTATIONS => $const->annotations, + DETAIL_COMMENT => $const->comment, + DETAIL_TARGET_ANNO => [], + DETAIL_ARGUMENTS => [] + ]; + $const->comment && $this->emitOne($result, $const->comment); $const->annotations && $this->emitOne($result, $const->annotations); $result->at($const->declared)->out->write(implode(' ', $const->modifiers).' const '.$const->name.'='); diff --git a/src/test/php/lang/ast/unittest/emit/MembersTest.class.php b/src/test/php/lang/ast/unittest/emit/MembersTest.class.php index 0bdc00e3..b81e446f 100755 --- a/src/test/php/lang/ast/unittest/emit/MembersTest.class.php +++ b/src/test/php/lang/ast/unittest/emit/MembersTest.class.php @@ -1,6 +1,6 @@ run('class { + $t= Reflection::type($this->type('class { private const string MEMBER = "Test"; + }')); + $const= $t->constant('MEMBER'); - public function run() { - return self::MEMBER; - } - }'); - - Assert::equals('Test', $r); + Assert::equals('Test', $const->value()); + Assert::equals(Primitive::$STRING, $const->constraint()->type()); } #[Test, Values(['$this->$member', '$this->{$member}', '$this->{strtoupper($member)}'])] diff --git a/src/test/php/lang/ast/unittest/emit/OmitConstModifiersTest.class.php b/src/test/php/lang/ast/unittest/emit/OmitConstModifiersTest.class.php index 7c6fedba..393c6ac1 100755 --- a/src/test/php/lang/ast/unittest/emit/OmitConstModifiersTest.class.php +++ b/src/test/php/lang/ast/unittest/emit/OmitConstModifiersTest.class.php @@ -1,11 +1,12 @@ type= new ClassDeclaration([], new IsValue('\\T'), null, [], [], null, null, 1); + } + #[Test] public function omits_type() { - Assert::equals( - 'const TEST="test";', - $this->emit(new Constant([], 'TEST', new IsLiteral('string'), new Literal('"test"'))) - ); + $const= new Constant([], 'TEST', new IsLiteral('string'), new Literal('"test"')); + Assert::equals('const TEST="test";', $this->emit($const, [$this->type])); } #[Test] public function omits_modifier() { - Assert::equals( - 'const TEST="test";', - $this->emit(new Constant(['private'], 'TEST', new IsLiteral('string'), new Literal('"test"'))) - ); + $const= new Constant(['private'], 'TEST', new IsLiteral('string'), new Literal('"test"')); + Assert::equals('const TEST="test";', $this->emit($const, [$this->type])); } } \ No newline at end of file