Skip to content

Commit

Permalink
[FEATURE] Introduce typed PHP constants (#24)
Browse files Browse the repository at this point in the history
* [BUGFIX] Keyword for PHP constant is "const"

We should also output it like that

* [FEATURE] Introduce typed PHP constants

Depends on #23
  • Loading branch information
linawolf authored Dec 9, 2023
1 parent 09ffa33 commit 977bd2f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions resources/template/html/body/directive/php/const.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% for modifier in node.modifiers -%}
{{- renderNode(modifier) }}{{ ' ' -}}
{%- endfor -%}
{% if node.phpType -%}<span class="pre">{{ node.phpType }}</span> {% endif -%}
<em class="property"><span class="pre">const</span></em>
{{ renderNode(node.memberName) }}
</dt>
Expand Down
6 changes: 6 additions & 0 deletions src/Directives/Php/ConstDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
6 changes: 6 additions & 0 deletions src/Nodes/PhpConstNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -35,4 +36,9 @@ public function getModifiers(): array
{
return $this->modifiers;
}

public function getPhpType(): ?string
{
return $this->phpType;
}
}
52 changes: 52 additions & 0 deletions tests/integration/class-with-typed-const/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!-- content start -->
<div class="section" id="php-class-with-constants">
<h1>PHP class with constants</h1>

<dl class="php class">
<dt class="sig sig-object php"
id="secretsociety-enigmaticclass">
<em class="property"><span class="pre">class</span> </em>
<span class="sig-prename descclassname"><span class="pre">\SecretSociety\</span></span>
<span class="sig-name descname"><span class="pre">EnigmaticClass</span></span>

</dt>
<dd>
<p>Unveiling the mysteries of constants!</p><dl class="php const">
<dt class="sig sig-object php" id="pi">
<span class="sig-name modifier"><span class="pre">public</span></span>
<span class="pre">double</span> <em class="property"><span class="pre">const</span></em>
<span class="sig-name descname"><span class="pre">PI</span></span>

</dt>
<dd><p>The eternal symbol representing the ratio of a circle&#039;s circumference
to its diameter. Also used as the secret handshake among mathematicians.</p></dd>
</dl>
<dl class="php const">
<dt class="sig sig-object php" id="secret-number">
<span class="sig-name modifier"><span class="pre">protected</span></span>
<span class="pre">int</span> <em class="property"><span class="pre">const</span></em>
<span class="sig-name descname"><span class="pre">SECRET_NUMBER</span></span>

</dt>
<dd><p>This constant holds the secret number known only to members of the
EnigmaticClass. Rumor has it, it&#039;s the combination to the ultimate
treasure chest.</p></dd>
</dl>
<dl class="php const">
<dt class="sig sig-object php" id="eternal-flame">
<span class="sig-name modifier"><span class="pre">private</span></span>
<span class="pre">string</span> <em class="property"><span class="pre">const</span></em>
<span class="sig-name descname"><span class="pre">ETERNAL_FLAME</span></span>

</dt>
<dd><p>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.</p></dd>
</dl>

</dd>
</dl>

</div>

<!-- content end -->
30 changes: 30 additions & 0 deletions tests/integration/class-with-typed-const/input/index.rst
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 977bd2f

Please sign in to comment.