diff --git a/src/PhpGenerator/Printer.php b/src/PhpGenerator/Printer.php index 9847fbaf..dbd56ad8 100644 --- a/src/PhpGenerator/Printer.php +++ b/src/PhpGenerator/Printer.php @@ -167,7 +167,13 @@ public function printClass( } $consts = []; - if ($class instanceof ClassType || $class instanceof InterfaceType || $class instanceof EnumType) { + $methods = []; + if ( + $class instanceof ClassType + || $class instanceof InterfaceType + || $class instanceof TraitType + || $class instanceof EnumType + ) { foreach ($class->getConstants() as $const) { $def = ($const->isFinal() ? 'final ' : '') . ($const->getVisibility() ? $const->getVisibility() . ' ' : '') @@ -178,15 +184,7 @@ public function printClass( . $def . $this->dump($const->getValue(), strlen($def)) . ";\n"; } - } - $methods = []; - if ( - $class instanceof ClassType - || $class instanceof InterfaceType - || $class instanceof EnumType - || $class instanceof TraitType - ) { foreach ($class->getMethods() as $method) { $methods[] = $this->printMethod($method, $namespace, $class->isInterface()); } diff --git a/src/PhpGenerator/TraitType.php b/src/PhpGenerator/TraitType.php index 4dbf42df..57621ad4 100644 --- a/src/PhpGenerator/TraitType.php +++ b/src/PhpGenerator/TraitType.php @@ -20,6 +20,7 @@ */ final class TraitType extends ClassLike { + use Traits\ConstantsAware; use Traits\MethodsAware; use Traits\PropertiesAware; use Traits\TraitsAware; @@ -28,6 +29,7 @@ public function addMember(Method|Property|Constant|TraitUse $member): static { $name = $member->getName(); [$type, $n] = match (true) { + $member instanceof Constant => ['consts', $name], $member instanceof Method => ['methods', strtolower($name)], $member instanceof Property => ['properties', $name], $member instanceof TraitUse => ['traits', $name], @@ -43,6 +45,7 @@ public function addMember(Method|Property|Constant|TraitUse $member): static public function __clone() { $clone = fn($item) => clone $item; + $this->consts = array_map($clone, $this->consts); $this->methods = array_map($clone, $this->methods); $this->properties = array_map($clone, $this->properties); $this->traits = array_map($clone, $this->traits); diff --git a/tests/PhpGenerator/ClassType.from.82.phpt b/tests/PhpGenerator/ClassType.from.82.phpt index 5acddad0..db001ac7 100644 --- a/tests/PhpGenerator/ClassType.from.82.phpt +++ b/tests/PhpGenerator/ClassType.from.82.phpt @@ -13,5 +13,6 @@ require __DIR__ . '/../bootstrap.php'; require __DIR__ . '/fixtures/classes.82.php'; $res[] = ClassType::from(new Abc\Class13); +$res[] = ClassType::from(Abc\Trait13::class); sameFile(__DIR__ . '/expected/ClassType.from.82.expect', implode("\n", $res)); diff --git a/tests/PhpGenerator/expected/ClassType.from.82.expect b/tests/PhpGenerator/expected/ClassType.from.82.expect index 39bb4484..4c03d053 100644 --- a/tests/PhpGenerator/expected/ClassType.from.82.expect +++ b/tests/PhpGenerator/expected/ClassType.from.82.expect @@ -1,3 +1,8 @@ readonly class Class13 { } + +trait Trait13 +{ + public const FOO = 123; +} diff --git a/tests/PhpGenerator/expected/Extractor.classes.82.expect b/tests/PhpGenerator/expected/Extractor.classes.82.expect index ea812871..c7de3d7e 100644 --- a/tests/PhpGenerator/expected/Extractor.classes.82.expect +++ b/tests/PhpGenerator/expected/Extractor.classes.82.expect @@ -7,3 +7,8 @@ namespace Abc; readonly class Class13 { } + +trait Trait13 +{ + public const FOO = 123; +} diff --git a/tests/PhpGenerator/fixtures/classes.82.php b/tests/PhpGenerator/fixtures/classes.82.php index ea812871..95e594ff 100644 --- a/tests/PhpGenerator/fixtures/classes.82.php +++ b/tests/PhpGenerator/fixtures/classes.82.php @@ -7,3 +7,9 @@ readonly class Class13 { } + + +trait Trait13 +{ + public const FOO = 123; +}