Skip to content

Commit 0959a81

Browse files
committed
TraitType: traits cannot have constants
1 parent 23110dd commit 0959a81

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

src/PhpGenerator/Factory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ public function fromClassReflection(
137137
}
138138
}
139139

140-
$class->setConstants($consts);
140+
if ($consts) {
141+
$class->setConstants($consts);
142+
}
141143
if ($cases) {
142144
$class->setCases($cases);
143145
}

src/PhpGenerator/Printer.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,10 @@ public function printClass(ClassLike $class, ?PhpNamespace $namespace = null): s
161161
}
162162

163163
$consts = [];
164-
$methods = [];
165164
if (
166165
$class instanceof ClassType
167166
|| $class instanceof InterfaceType
168167
|| $class instanceof EnumType
169-
|| $class instanceof TraitType
170168
) {
171169
foreach ($class->getConstants() as $const) {
172170
$def = ($const->isFinal() ? 'final ' : '')
@@ -178,7 +176,15 @@ public function printClass(ClassLike $class, ?PhpNamespace $namespace = null): s
178176
. $def
179177
. $this->dump($const->getValue(), strlen($def)) . ";\n";
180178
}
179+
}
181180

181+
$methods = [];
182+
if (
183+
$class instanceof ClassType
184+
|| $class instanceof InterfaceType
185+
|| $class instanceof EnumType
186+
|| $class instanceof TraitType
187+
) {
182188
foreach ($class->getMethods() as $method) {
183189
$methods[] = $this->printMethod($method, $namespace, $class->isInterface());
184190
}

src/PhpGenerator/TraitType.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@
2020
*/
2121
final class TraitType extends ClassLike
2222
{
23-
use Traits\ConstantsAware;
2423
use Traits\MethodsAware;
2524
use Traits\PropertiesAware;
2625
use Traits\TraitsAware;
2726

27+
2828
public function addMember(Method|Property|Constant|TraitUse $member): static
2929
{
3030
$name = $member->getName();
3131
[$type, $n] = match (true) {
32-
$member instanceof Constant => ['consts', $name],
3332
$member instanceof Method => ['methods', strtolower($name)],
3433
$member instanceof Property => ['properties', $name],
3534
$member instanceof TraitUse => ['traits', $name],
@@ -45,7 +44,6 @@ public function addMember(Method|Property|Constant|TraitUse $member): static
4544
public function __clone()
4645
{
4746
$clone = fn($item) => clone $item;
48-
$this->consts = array_map($clone, $this->consts);
4947
$this->methods = array_map($clone, $this->methods);
5048
$this->properties = array_map($clone, $this->properties);
5149
$this->traits = array_map($clone, $this->traits);

0 commit comments

Comments
 (0)