Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5de8c7f

Browse files
committed
added support for constants in traits
1 parent c93ab11 commit 5de8c7f

File tree

6 files changed

+27
-9
lines changed

6 files changed

+27
-9
lines changed

‎src/PhpGenerator/Printer.php‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,13 @@ public function printClass(
167167
}
168168

169169
$consts = [];
170-
if ($class instanceof ClassType || $class instanceof InterfaceType || $class instanceof EnumType) {
170+
$methods = [];
171+
if (
172+
$class instanceof ClassType
173+
|| $class instanceof InterfaceType
174+
|| $class instanceof TraitType
175+
|| $class instanceof EnumType
176+
) {
171177
foreach ($class->getConstants() as $const) {
172178
$def = ($const->isFinal() ? 'final ' : '')
173179
. ($const->getVisibility() ? $const->getVisibility() . '' : '')
@@ -178,15 +184,7 @@ public function printClass(
178184
. $def
179185
. $this->dump($const->getValue(), strlen($def)) . ";\n";
180186
}
181-
}
182187

183-
$methods = [];
184-
if (
185-
$class instanceof ClassType
186-
|| $class instanceof InterfaceType
187-
|| $class instanceof EnumType
188-
|| $class instanceof TraitType
189-
) {
190188
foreach ($class->getMethods() as $method) {
191189
$methods[] = $this->printMethod($method, $namespace, $class->isInterface());
192190
}

‎src/PhpGenerator/TraitType.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
final class TraitType extends ClassLike
2222
{
23+
use Traits\ConstantsAware;
2324
use Traits\MethodsAware;
2425
use Traits\PropertiesAware;
2526
use Traits\TraitsAware;
@@ -28,6 +29,7 @@ public function addMember(Method|Property|Constant|TraitUse $member): static
2829
{
2930
$name = $member->getName();
3031
[$type, $n] = match (true) {
32+
$member instanceof Constant => ['consts', $name],
3133
$member instanceof Method => ['methods', strtolower($name)],
3234
$member instanceof Property => ['properties', $name],
3335
$member instanceof TraitUse => ['traits', $name],
@@ -43,6 +45,7 @@ public function addMember(Method|Property|Constant|TraitUse $member): static
4345
public function __clone()
4446
{
4547
$clone = fn($item) => clone $item;
48+
$this->consts = array_map($clone, $this->consts);
4649
$this->methods = array_map($clone, $this->methods);
4750
$this->properties = array_map($clone, $this->properties);
4851
$this->traits = array_map($clone, $this->traits);

‎tests/PhpGenerator/ClassType.from.82.phpt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ require __DIR__ . '/../bootstrap.php';
1313
require __DIR__ . '/fixtures/classes.82.php';
1414

1515
$res[] = ClassType::from(new Abc\Class13);
16+
$res[] = ClassType::from(Abc\Trait13::class);
1617

1718
sameFile(__DIR__ . '/expected/ClassType.from.82.expect', implode("\n", $res));
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
readonly class Class13
22
{
33
}
4+
5+
trait Trait13
6+
{
7+
public const FOO = 123;
8+
}

‎tests/PhpGenerator/expected/Extractor.classes.82.expect‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ namespace Abc;
77
readonly class Class13
88
{
99
}
10+
11+
trait Trait13
12+
{
13+
public const FOO = 123;
14+
}

‎tests/PhpGenerator/fixtures/classes.82.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@
77
readonly class Class13
88
{
99
}
10+
11+
12+
trait Trait13
13+
{
14+
public const FOO = 123;
15+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /