Skip to content

Commit

Permalink
Extract property hook bodies properly (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
xHeaven authored Feb 17, 2025
1 parent 5c30107 commit fff50de
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/PhpGenerator/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,29 @@ public function extractMethodBodies(string $className): array
}


/** @return array<string, array<string, string>> */
public function extractPropertyHookBodies(string $className): array
{
$nodeFinder = new NodeFinder();
$classNode = $nodeFinder->findFirst(
$this->statements,
fn(Node $node) => $node instanceof Node\Stmt\ClassLike && $node->namespacedName->toString() === $className,
);

$res = [];
foreach ($nodeFinder->findInstanceOf($classNode, Node\Stmt\Property::class) as $propertyNode) {
foreach ($propertyNode->props as $propNode) {
$propName = $propNode->name->toString();
foreach ($propertyNode->hooks as $hookNode) {
$hookType = $hookNode->name->toString();
$res[$propName][$hookType] = $this->getReformattedContents([$hookNode->body], 1);
}
}
}
return $res;
}


public function extractFunctionBody(string $name): ?string
{
$functionNode = (new NodeFinder)->findFirst(
Expand Down
9 changes: 9 additions & 0 deletions src/PhpGenerator/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ public function fromClassReflection(
$resolutions = [];
}

if ($withBodies) {
$hookBodies = $this->getExtractor($declaringClass->getFileName())->extractPropertyHookBodies($declaringClass->name);
foreach ($class->getProperties() as $property) {
foreach ($hookBodies[$property->getName()] ?? [] as $hookType => $body) {
$property->getHook($hookType)?->setBody($body, short: true);
}
}
}

$consts = $cases = [];
foreach ($from->getReflectionConstants() as $const) {
if ($class->isEnum() && $from->hasCase($const->name)) {
Expand Down

0 comments on commit fff50de

Please sign in to comment.