From e2043499f489b7fc0e73c6ceb93903182cb8915c Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Thu, 23 Jan 2025 17:30:46 +0100 Subject: [PATCH 1/2] Extract property hook bodies properly --- src/PhpGenerator/Extractor.php | 23 +++++++++++++++++++++++ src/PhpGenerator/Factory.php | 9 +++++++++ 2 files changed, 32 insertions(+) diff --git a/src/PhpGenerator/Extractor.php b/src/PhpGenerator/Extractor.php index 97d2f732..cc570a54 100644 --- a/src/PhpGenerator/Extractor.php +++ b/src/PhpGenerator/Extractor.php @@ -79,6 +79,29 @@ public function extractMethodBodies(string $className): array } + /** @return array> */ + 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( diff --git a/src/PhpGenerator/Factory.php b/src/PhpGenerator/Factory.php index 12a5687d..d8d181e5 100644 --- a/src/PhpGenerator/Factory.php +++ b/src/PhpGenerator/Factory.php @@ -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)) { From e5a56dc6f41b09bfe75bbdfb720168d33127ab91 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Thu, 23 Jan 2025 17:44:22 +0100 Subject: [PATCH 2/2] Replaced spaces with tabs as per coding standard --- src/PhpGenerator/Extractor.php | 42 +++++++++++++++++----------------- src/PhpGenerator/Factory.php | 12 +++++----- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/PhpGenerator/Extractor.php b/src/PhpGenerator/Extractor.php index cc570a54..43bd976e 100644 --- a/src/PhpGenerator/Extractor.php +++ b/src/PhpGenerator/Extractor.php @@ -79,27 +79,27 @@ public function extractMethodBodies(string $className): array } - /** @return array> */ - 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; - } + /** @return array> */ + 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 diff --git a/src/PhpGenerator/Factory.php b/src/PhpGenerator/Factory.php index d8d181e5..0e182fc0 100644 --- a/src/PhpGenerator/Factory.php +++ b/src/PhpGenerator/Factory.php @@ -124,12 +124,12 @@ 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); - } + 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); + } } }