8000 Extract property hook bodies properly by xHeaven · Pull Request #172 · nette/php-generator · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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