Skip to content

Commit 5ce7b6f

Browse files
committed
Initial support for trait usage, fixing #14
1 parent 4d7bc4c commit 5ce7b6f

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/main/php/lang/ast/Emitter.class.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,10 @@ protected function emitTrait($node) {
375375
$this->out->write('}');
376376
}
377377

378+
protected function emitUse($node) {
379+
$this->out->write('use '.$node->value.';');
380+
}
381+
378382
protected function emitConst($node) {
379383
$this->out->write(implode(' ', $node->value[1]).' const '.$node->value[0].'=');
380384
$this->emit($node->value[2]);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php namespace lang\ast\unittest\emit;
2+
3+
trait Loading {
4+
5+
public function loaded() { }
6+
7+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php namespace lang\ast\unittest\emit;
2+
3+
use lang\XPClass;
4+
5+
/**
6+
* Traits
7+
*
8+
* @see https://wiki.php.net/rfc/horizontalreuse
9+
*/
10+
class TraitsTest extends EmittingTest {
11+
12+
#[@test]
13+
public function trait_is_included() {
14+
$t= $this->type('class <T> { use \lang\ast\unittest\emit\Loading; }');
15+
$this->assertEquals([new XPClass(Loading::class)], $t->getTraits());
16+
}
17+
18+
#[@test]
19+
public function trait_method_is_part_of_type() {
20+
$t= $this->type('class <T> { use \lang\ast\unittest\emit\Loading; }');
21+
$this->assertTrue($t->hasMethod('loaded'));
22+
}
23+
}

0 commit comments

Comments
 (0)