From 977bd2f74f0279da6d719d7a15c9e71bd1647842 Mon Sep 17 00:00:00 2001 From: Lina Wolf <48202465+linawolf@users.noreply.github.com> Date: Sat, 9 Dec 2023 15:41:46 +0100 Subject: [PATCH] [FEATURE] Introduce typed PHP constants (#24) * [BUGFIX] Keyword for PHP constant is "const" We should also output it like that * [FEATURE] Introduce typed PHP constants Depends on #23 --- .../html/body/directive/php/const.html.twig | 1 + src/Directives/Php/ConstDirective.php | 6 +++ src/Nodes/PhpConstNode.php | 6 +++ .../expected/index.html | 52 +++++++++++++++++++ .../class-with-typed-const/input/index.rst | 30 +++++++++++ 5 files changed, 95 insertions(+) create mode 100644 tests/integration/class-with-typed-const/expected/index.html create mode 100644 tests/integration/class-with-typed-const/input/index.rst diff --git a/resources/template/html/body/directive/php/const.html.twig b/resources/template/html/body/directive/php/const.html.twig index 7867347..b8d591c 100644 --- a/resources/template/html/body/directive/php/const.html.twig +++ b/resources/template/html/body/directive/php/const.html.twig @@ -3,6 +3,7 @@ {% for modifier in node.modifiers -%} {{- renderNode(modifier) }}{{ ' ' -}} {%- endfor -%} + {% if node.phpType -%}{{ node.phpType }} {% endif -%} const {{ renderNode(node.memberName) }} diff --git a/src/Directives/Php/ConstDirective.php b/src/Directives/Php/ConstDirective.php index 0bb1938..6796b91 100644 --- a/src/Directives/Php/ConstDirective.php +++ b/src/Directives/Php/ConstDirective.php @@ -61,11 +61,17 @@ protected function processSub( } } + $type = null; + if ($directive->hasOption('type')) { + $type = $directive->getOption('type')->toString(); + } + return new PhpConstNode( $id, $name, $collectionNode->getChildren(), $modifiers, + $type, ); } } diff --git a/src/Nodes/PhpConstNode.php b/src/Nodes/PhpConstNode.php index feed000..34238f5 100644 --- a/src/Nodes/PhpConstNode.php +++ b/src/Nodes/PhpConstNode.php @@ -19,6 +19,7 @@ public function __construct( private readonly MemberNameNode $memberName, array $value = [], private readonly array $modifiers = [], + private readonly string|null $phpType = null, ) { parent::__construct($id, self::TYPE, $memberName->toString(), $value); } @@ -35,4 +36,9 @@ public function getModifiers(): array { return $this->modifiers; } + + public function getPhpType(): ?string + { + return $this->phpType; + } } diff --git a/tests/integration/class-with-typed-const/expected/index.html b/tests/integration/class-with-typed-const/expected/index.html new file mode 100644 index 0000000..2239d35 --- /dev/null +++ b/tests/integration/class-with-typed-const/expected/index.html @@ -0,0 +1,52 @@ + +
+

PHP class with constants

+ +
+
+ class + \SecretSociety\ +EnigmaticClass + +
+
+

Unveiling the mysteries of constants!

+
+ public + double const + PI + +
+

The eternal symbol representing the ratio of a circle's circumference +to its diameter. Also used as the secret handshake among mathematicians.

+
+
+
+ protected + int const + SECRET_NUMBER + +
+

This constant holds the secret number known only to members of the +EnigmaticClass. Rumor has it, it's the combination to the ultimate +treasure chest.

+
+
+
+ private + string const + ETERNAL_FLAME + +
+

The eternal flame, kept alight by the power of mystical constants. Its +warmth is known only to those who dare to delve into the deepest realms +of code and cryptography.

+
+ +
+
+ +
+ + diff --git a/tests/integration/class-with-typed-const/input/index.rst b/tests/integration/class-with-typed-const/input/index.rst new file mode 100644 index 0000000..860d053 --- /dev/null +++ b/tests/integration/class-with-typed-const/input/index.rst @@ -0,0 +1,30 @@ +======================== +PHP class with constants +======================== + +.. php:class:: SecretSociety\EnigmaticClass + + Unveiling the mysteries of constants! + + .. php:const:: PI + :public: + :type: double + + The eternal symbol representing the ratio of a circle's circumference + to its diameter. Also used as the secret handshake among mathematicians. + + .. php:const:: SECRET_NUMBER + :protected: + :type: int + + This constant holds the secret number known only to members of the + EnigmaticClass. Rumor has it, it's the combination to the ultimate + treasure chest. + + .. php:const:: ETERNAL_FLAME + :private: + :type: string + + The eternal flame, kept alight by the power of mystical constants. Its + warmth is known only to those who dare to delve into the deepest realms + of code and cryptography.