Skip to content

Commit 8079531

Browse files
committed
Factory: reads class constants with visibility & phpDoc
1 parent d422cac commit 8079531

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/PhpGenerator/Factory.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function fromClassReflection(\ReflectionClass $from): ClassType
4141
$class->setExtends($from->getParentClass()->getName());
4242
$class->setImplements(array_diff($class->getImplements(), $from->getParentClass()->getInterfaceNames()));
4343
}
44-
$props = $methods = [];
44+
$props = $methods = $consts = [];
4545
foreach ($from->getProperties() as $prop) {
4646
if ($prop->isDefault() && $prop->getDeclaringClass()->getName() === $from->getName()) {
4747
$props[] = $this->fromPropertyReflection($prop);
@@ -54,7 +54,14 @@ public function fromClassReflection(\ReflectionClass $from): ClassType
5454
}
5555
}
5656
$class->setMethods($methods);
57-
$class->setConstants($from->getConstants());
57+
58+
foreach ($from->getReflectionConstants() as $const) {
59+
if ($const->getDeclaringClass()->name === $from->name) {
60+
$consts[] = $this->fromConstantReflection($const);
61+
}
62+
}
63+
$class->setConstants($consts);
64+
5865
return $class;
5966
}
6067

@@ -127,6 +134,19 @@ public function fromParameterReflection(\ReflectionParameter $from): Parameter
127134
}
128135

129136

137+
public function fromConstantReflection(\ReflectionClassConstant $from): Constant
138+
{
139+
$const = new Constant($from->name);
140+
$const->setValue($from->getValue());
141+
$const->setVisibility($from->isPrivate()
142+
? ClassType::VISIBILITY_PRIVATE
143+
: ($from->isProtected() ? ClassType::VISIBILITY_PROTECTED : ClassType::VISIBILITY_PUBLIC)
144+
);
145+
$const->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
146+
return $const;
147+
}
148+
149+
130150
public function fromPropertyReflection(\ReflectionProperty $from): Property
131151
{
132152
$defaults = $from->getDeclaringClass()->getDefaultProperties();

tests/PhpGenerator/expected/ClassType.from.expect

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Class3
5959

6060
class Class4
6161
{
62-
const THE_CONSTANT = 9;
62+
public const THE_CONSTANT = 9;
6363
}
6464

6565
class Class5
@@ -79,8 +79,9 @@ class Class5
7979
}
8080
}
8181

82-
class Class6
82+
class Class6 extends Class4
8383
{
84-
const THE_PRIVATE_CONSTANT = 9;
85-
const THE_PUBLIC_CONSTANT = 9;
84+
/** const doc */
85+
private const THE_PRIVATE_CONSTANT = 9;
86+
public const THE_PUBLIC_CONSTANT = 9;
8687
}

tests/PhpGenerator/fixtures/classes.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ public function func3(): void
9292
}
9393

9494

95-
class Class6
95+
class Class6 extends Class4
9696
{
97+
/** const doc */
9798
private const THE_PRIVATE_CONSTANT = 9;
9899
public const THE_PUBLIC_CONSTANT = 9;
99100
}

0 commit comments

Comments
 (0)