Skip to content

Commit

Permalink
[FEATURE] Introduce php:staticmethod directive
Browse files Browse the repository at this point in the history
  • Loading branch information
linawolf committed Nov 25, 2023
1 parent cc00776 commit 14b2554
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 2 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ parameters:
ignoreErrors:
-
message: "#^Function phpDocumentor\\\\Guides\\\\DependencyInjection\\\\template not found\\.$#"
count: 8
count: 9
path: src/DependencyInjection/GuidesPhpDomainExtension.php

-
Expand Down
2 changes: 2 additions & 0 deletions resources/config/php-domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use T3Docs\GuidesPhpDomain\Directives\Php\ClassDirective;
use T3Docs\GuidesPhpDomain\Directives\Php\ConstDirective;
use T3Docs\GuidesPhpDomain\Directives\Php\StaticMethodDirective;
use T3Docs\GuidesPhpDomain\Directives\Php\GlobalDirective;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

Expand Down Expand Up @@ -37,6 +38,7 @@
->set(InterfaceDirective::class)
->set(MethodDirective::class)
->set(NamespaceDirective::class)
->set(StaticMethodDirective::class)
->set(FullyQualifiedNameService::class)
->set(MethodNameService::class)
->set(NamespaceRepository::class)
Expand Down
5 changes: 4 additions & 1 deletion resources/template/html/body/directive/php/method.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<dl class="php method">
<dt class="sig sig-object php" id="{{ node.id }}">
{{ renderNode(node.methodName) }}
{%- for modifier in node.modifiers -%}
{{- renderNode(modifier) }}{{ ' ' -}}
{%- endfor -%}
{{- renderNode(node.methodName) -}}
</dt>
<dd>
{{ renderNode(node.value) }}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span class="sig-name modifier"><span class="pre">{{ node.type }}</span></span>
2 changes: 2 additions & 0 deletions src/DependencyInjection/GuidesPhpDomainExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use T3Docs\GuidesPhpDomain\Nodes\PhpConstNode;
use T3Docs\GuidesPhpDomain\Nodes\PhpGlobalNode;
use T3Docs\GuidesPhpDomain\Nodes\PhpMethodNode;
use T3Docs\GuidesPhpDomain\Nodes\PhpModifierNode;
use T3Docs\GuidesPhpDomain\Nodes\PhpNamespaceNode;

use function dirname;
Expand Down Expand Up @@ -46,6 +47,7 @@ public function prepend(ContainerBuilder $container): void
template(PhpGlobalNode::class, 'body/directive/php/global.html.twig'),
template(PhpNamespaceNode::class, 'body/directive/php/namespace.html.twig'),
template(PhpMethodNode::class, 'body/directive/php/method.html.twig'),
template(PhpModifierNode::class, 'body/directive/php/modifier.html.twig'),
template(MemberNameNode::class, 'body/directive/php/memberName.html.twig'),
template(MethodNameNode::class, 'body/directive/php/methodName.html.twig'),
],
Expand Down
51 changes: 51 additions & 0 deletions src/Directives/Php/StaticMethodDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace T3Docs\GuidesPhpDomain\Directives\Php;

use phpDocumentor\Guides\Nodes\CollectionNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\ReferenceResolvers\AnchorReducer;
use phpDocumentor\Guides\RestructuredText\Directives\SubDirective;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\Rule;
use phpDocumentor\Guides\RestructuredText\TextRoles\GenericLinkProvider;
use T3Docs\GuidesPhpDomain\Nodes\PhpMethodNode;
use T3Docs\GuidesPhpDomain\Nodes\PhpModifierNode;
use T3Docs\GuidesPhpDomain\PhpDomain\MethodNameService;

final class StaticMethodDirective extends SubDirective
{
public function __construct(
Rule $startingRule,
GenericLinkProvider $genericLinkProvider,
private readonly MethodNameService $methodNameService,
private readonly AnchorReducer $anchorReducer,
) {
parent::__construct($startingRule);
$genericLinkProvider->addGenericLink($this->getName(), $this->getName());
}

public function getName(): string
{
return 'php:staticmethod';
}

protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
$name = $this->methodNameService->getMethodName(trim($directive->getData()));
$id = $this->anchorReducer->reduceAnchor($name->toString());

return new PhpMethodNode(
$id,
$name,
$collectionNode->getChildren(),
[new PhpModifierNode(PhpModifierNode::STATIC)]
);
}
}
10 changes: 10 additions & 0 deletions src/Nodes/PhpMethodNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ final class PhpMethodNode extends PhpMemberNode
private const TYPE = 'method';
/**
* @param Node[] $value
* @param PhpModifierNode[] $modifiers
*/
public function __construct(
string $id,
private readonly MethodNameNode $methodName,
array $value = [],
private readonly array $modifiers = [],
) {
parent::__construct($id, self::TYPE, $methodName->toString(), $value);
}
Expand All @@ -24,4 +26,12 @@ public function getMethodName(): MethodNameNode
{
return $this->methodName;
}

/**
* @return PhpModifierNode[]
*/
public function getModifiers(): array
{
return $this->modifiers;
}
}
27 changes: 27 additions & 0 deletions src/Nodes/PhpModifierNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace T3Docs\GuidesPhpDomain\Nodes;

use phpDocumentor\Guides\Nodes\AbstractNode;
use phpDocumentor\Guides\Nodes\Node;

/**
* Stores data on PHP modifiers for classes, methods, attributes, etc
* @extends AbstractNode<string>
*/
final class PhpModifierNode extends AbstractNode
{
public const STATIC = 'static';
public function __construct(
private readonly string $type,
) {
$this->value = $type;
}

public function getType(): string
{
return $this->type;
}
}
32 changes: 32 additions & 0 deletions tests/integration/class-with-staticmethod/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!-- content start -->
<div class="section" id="php-class-with-static-method">
<h1>PHP Class with static method</h1>

<dl class="php class">
<dt class="sig sig-object php"
id="typo3-cms-core-test">
<em class="property"><span class="pre">class</span> </em>
<span class="sig-prename descclassname"><span class="pre">\TYPO3\CMS\Core\</span></span>
<span class="sig-name descname"><span class="pre">Test</span></span>

</dt>
<dd>
<p>Lorem Ipsum Dolor!</p><dl class="php method">
<dt class="sig sig-object php" id="dosomething"><span class="sig-name modifier"><span class="pre">static</span></span>

<span class="sig-name descname"><span class="pre">doSomething</span></span>
<span class="sig-paren">(</span>
<em class="sig-param"><span class="pre">string $whatever</span></em><span class="sig-paren">)</span>
<em class="sig-returns"><span class="pre">: string</span></em>
</dt>
<dd>
<p>Do something</p>
</dd>
</dl>

</dd>
</dl>

</div>

<!-- content end -->
11 changes: 11 additions & 0 deletions tests/integration/class-with-staticmethod/input/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
============================
PHP Class with static method
============================

.. php:class:: TYPO3\CMS\Core\Test
Lorem Ipsum Dolor!

.. php:staticmethod:: doSomething(string $whatever): string
Do something
20 changes: 20 additions & 0 deletions tests/integration/static-method/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- content start -->
<div class="section" id="php-static-method">
<h1>PHP static method</h1>

<dl class="php method">
<dt class="sig sig-object php" id="dosomething"><span class="sig-name modifier"><span class="pre">static</span></span>

<span class="sig-name descname"><span class="pre">doSomething</span></span>
<span class="sig-paren">(</span>
<em class="sig-param"><span class="pre">string $whatever</span></em><span class="sig-paren">)</span>
<em class="sig-returns"><span class="pre">: string</span></em>
</dt>
<dd>
<p>Do something</p>
</dd>
</dl>

</div>

<!-- content end -->
7 changes: 7 additions & 0 deletions tests/integration/static-method/input/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
=================
PHP static method
=================

.. php:staticmethod:: doSomething(string $whatever): string
Do something

0 comments on commit 14b2554

Please sign in to comment.