Skip to content

Commit

Permalink
added support for constants in traits
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 2, 2022
1 parent 5a40432 commit 17ce530
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,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() . ' ' : '')
Expand All @@ -179,15 +185,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());
}
Expand Down
3 changes: 3 additions & 0 deletions src/PhpGenerator/TraitType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
final class TraitType extends ClassLike
{
use Traits\ConstantsAware;
use Traits\MethodsAware;
use Traits\PropertiesAware;
use Traits\TraitsAware;
Expand All @@ -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],
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tests/PhpGenerator/ClassType.from.82.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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));
5 changes: 5 additions & 0 deletions tests/PhpGenerator/expected/ClassType.from.82.expect
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
readonly class Class13
{
}

trait Trait13
{
public const FOO = 123;
}
5 changes: 5 additions & 0 deletions tests/PhpGenerator/expected/Extractor.classes.82.expect
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ namespace Abc;
readonly class Class13
{
}

trait Trait13
{
public const FOO = 123;
}
6 changes: 6 additions & 0 deletions tests/PhpGenerator/fixtures/classes.82.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@
readonly class Class13
{
}


trait Trait13
{
public const FOO = 123;
}

0 comments on commit 17ce530

Please sign in to comment.