From 77a6e5c1f80efe6611f67c0bc720e8c999ebddc8 Mon Sep 17 00:00:00 2001 From: Lina Wolf <48202465+linawolf@users.noreply.github.com> Date: Mon, 25 Mar 2024 05:50:39 +0100 Subject: [PATCH] [FEATURE] Make Fully Qualified name available in members node. (#41) * [FEATURE] Make Fully Qualified name available in members node. This is used to suggest a rst reference snippet in the render-guides. I can then offer the following code in the suggest wizzard: :php:method:`somemanual:\LibraryName\LibraryClassFinal::firstmethod` Instead of something like this: :php:method:`somemanual:libraryname-libraryclassfinal-firstmethod` * [TASK] Update integration test baseline Whitespace handling changed in the guides, therefore our integration tests need to be updated for whitespace. --- src/Directives/Php/ClassDirective.php | 7 ++++++- src/Directives/Php/ComponentTrait.php | 20 ++++++++++++++++++++ src/Directives/Php/EnumDirective.php | 6 +++++- src/Directives/Php/ExceptionDirective.php | 6 +++++- src/Directives/Php/InterfaceDirective.php | 7 +++++-- src/Directives/Php/TraitDirective.php | 7 +++++-- src/Nodes/PhpMemberNode.php | 21 ++++++++++++++++++++- 7 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 src/Directives/Php/ComponentTrait.php diff --git a/src/Directives/Php/ClassDirective.php b/src/Directives/Php/ClassDirective.php index f3fa93b..f701b63 100644 --- a/src/Directives/Php/ClassDirective.php +++ b/src/Directives/Php/ClassDirective.php @@ -19,6 +19,7 @@ final class ClassDirective extends SubDirective { + use ComponentTrait; /** * @var string[] */ @@ -54,7 +55,7 @@ protected function processSub( $this->logger->warning('A PHP class cannot be abstract and final at the same time.', $blockContext->getLoggerInformation()); } - return new PhpClassNode( + $node = new PhpClassNode( $id, $fqn, $collectionNode->getChildren(), @@ -62,5 +63,9 @@ protected function processSub( [], $modifiers, ); + + $this->setParentsForMembers($collectionNode, $node); + + return $node; } } diff --git a/src/Directives/Php/ComponentTrait.php b/src/Directives/Php/ComponentTrait.php new file mode 100644 index 0000000..49e4ad2 --- /dev/null +++ b/src/Directives/Php/ComponentTrait.php @@ -0,0 +1,20 @@ +getChildren() as $child) { + if (!$child instanceof PhpMemberNode) { + continue; + } + $child->setParentComponent($node); + } + } +} diff --git a/src/Directives/Php/EnumDirective.php b/src/Directives/Php/EnumDirective.php index c3aa14a..ec16744 100644 --- a/src/Directives/Php/EnumDirective.php +++ b/src/Directives/Php/EnumDirective.php @@ -18,6 +18,7 @@ final class EnumDirective extends SubDirective { + use ComponentTrait; public function __construct( Rule $startingRule, GenericLinkProvider $genericLinkProvider, @@ -59,7 +60,7 @@ protected function processSub( $type = $directive->getOption('type')->toString(); } - return new PhpEnumNode( + $node = new PhpEnumNode( $id, $fqn, $collectionNode->getChildren(), @@ -68,5 +69,8 @@ protected function processSub( [], $type, ); + + $this->setParentsForMembers($collectionNode, $node); + return $node; } } diff --git a/src/Directives/Php/ExceptionDirective.php b/src/Directives/Php/ExceptionDirective.php index 8ba8b73..bb082b8 100644 --- a/src/Directives/Php/ExceptionDirective.php +++ b/src/Directives/Php/ExceptionDirective.php @@ -19,6 +19,7 @@ final class ExceptionDirective extends SubDirective { + use ComponentTrait; /** * @var string[] */ @@ -54,7 +55,7 @@ protected function processSub( $this->logger->warning('A PHP class cannot be abstract and final at the same time.', $blockContext->getLoggerInformation()); } - return new PhpExceptionNode( + $node = new PhpExceptionNode( $id, $fqn, $collectionNode->getChildren(), @@ -62,5 +63,8 @@ protected function processSub( [], $modifiers, ); + + $this->setParentsForMembers($collectionNode, $node); + return $node; } } diff --git a/src/Directives/Php/InterfaceDirective.php b/src/Directives/Php/InterfaceDirective.php index 4df8fee..780f8c1 100644 --- a/src/Directives/Php/InterfaceDirective.php +++ b/src/Directives/Php/InterfaceDirective.php @@ -17,6 +17,7 @@ final class InterfaceDirective extends SubDirective { + use ComponentTrait; public function __construct( Rule $startingRule, GenericLinkProvider $genericLinkProvider, @@ -41,13 +42,15 @@ protected function processSub( $fqn = $this->fullyQualifiedNameService->getFullyQualifiedName($name, true); $id = $this->anchorNormalizer->reduceAnchor($fqn->toString()); - - return new PhpInterfaceNode( + $node = new PhpInterfaceNode( $id, $fqn, $collectionNode->getChildren(), null, [], ); + + $this->setParentsForMembers($collectionNode, $node); + return $node; } } diff --git a/src/Directives/Php/TraitDirective.php b/src/Directives/Php/TraitDirective.php index 1ab2ed2..4bcd60b 100644 --- a/src/Directives/Php/TraitDirective.php +++ b/src/Directives/Php/TraitDirective.php @@ -17,6 +17,7 @@ final class TraitDirective extends SubDirective { + use ComponentTrait; public function __construct( Rule $startingRule, GenericLinkProvider $genericLinkProvider, @@ -40,13 +41,15 @@ protected function processSub( $name = trim($directive->getData()); $fqn = $this->fullyQualifiedNameService->getFullyQualifiedName($name, true); $id = $this->anchorNormalizer->reduceAnchor($fqn->toString()); - - return new PhpTraitNode( + $node = new PhpTraitNode( $id, $fqn, $collectionNode->getChildren(), null, [], ); + + $this->setParentsForMembers($collectionNode, $node); + return $node; } } diff --git a/src/Nodes/PhpMemberNode.php b/src/Nodes/PhpMemberNode.php index 5a82b98..9b0b9c9 100644 --- a/src/Nodes/PhpMemberNode.php +++ b/src/Nodes/PhpMemberNode.php @@ -20,6 +20,7 @@ public function __construct( private readonly string $type, private readonly string $name, array $value = [], + private ?PhpComponentNode $parentComponent = null, ) { parent::__construct($value); } @@ -42,7 +43,7 @@ public function getLinkType(): string } public function getLinkText(): string { - return $this->name; + return $this->getFullyQualifiedName(); } public function getType(): string @@ -54,4 +55,22 @@ public function getId(): string { return $this->id; } + + public function getParentComponent(): ?PhpComponentNode + { + return $this->parentComponent; + } + + public function setParentComponent(?PhpComponentNode $parentComponent): void + { + $this->parentComponent = $parentComponent; + } + + public function getFullyQualifiedName(): string + { + if ($this->parentComponent == null) { + return $this->getName(); + } + return $this->parentComponent->getName()->toString() . '::' . $this->getName(); + } }