Skip to content

Commit fff50de

Browse files
authored
Extract property hook bodies properly (#172)
1 parent 5c30107 commit fff50de

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/PhpGenerator/Extractor.php

+23
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,29 @@ public function extractMethodBodies(string $className): array
7979
}
8080

8181

82+
/** @return array<string, array<string, string>> */
83+
public function extractPropertyHookBodies(string $className): array
84+
{
85+
$nodeFinder = new NodeFinder();
86+
$classNode = $nodeFinder->findFirst(
87+
$this->statements,
88+
fn(Node $node) => $node instanceof Node\Stmt\ClassLike && $node->namespacedName->toString() === $className,
89+
);
90+
91+
$res = [];
92+
foreach ($nodeFinder->findInstanceOf($classNode, Node\Stmt\Property::class) as $propertyNode) {
93+
foreach ($propertyNode->props as $propNode) {
94+
$propName = $propNode->name->toString();
95+
foreach ($propertyNode->hooks as $hookNode) {
96+
$hookType = $hookNode->name->toString();
97+
$res[$propName][$hookType] = $this->getReformattedContents([$hookNode->body], 1);
98+
}
99+
}
100+
}
101+
return $res;
102+
}
103+
104+
82105
public function extractFunctionBody(string $name): ?string
83106
{
84107
$functionNode = (new NodeFinder)->findFirst(

src/PhpGenerator/Factory.php

+9
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ public function fromClassReflection(
124124
$resolutions = [];
125125
}
126126

127+
if ($withBodies) {
128+
$hookBodies = $this->getExtractor($declaringClass->getFileName())->extractPropertyHookBodies($declaringClass->name);
129+
foreach ($class->getProperties() as $property) {
130+
foreach ($hookBodies[$property->getName()] ?? [] as $hookType => $body) {
131+
$property->getHook($hookType)?->setBody($body, short: true);
132+
}
133+
}
134+
}
135+
127136
$consts = $cases = [];
128137
foreach ($from->getReflectionConstants() as $const) {
129138
if ($class->isEnum() && $from->hasCase($const->name)) {

0 commit comments

Comments
 (0)