Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: Try to render docblocks in guides #3577

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,15 @@ services:
phpDocumentor\Guides\Compiler\NodeTransformer\PHPReferenceNodeTransformer:
tags:
- { name: 'phpdoc.guides.compiler.nodeTransformers'}

phpDocumentor\Guides\Compiler\NodeTransformer\DocblockNodeTransformer:
tags:
- { name: 'phpdoc.guides.compiler.nodeTransformers' }

phpDocumentor\Guides\RestructuredText\Directives\Docblock:
tags:
- { name: 'phpdoc.guides.directive'}

phpDocumentor\Guides\NodeRenderer\DocblockRenderer:
tags:
- { name: 'phpdoc.guides.noderenderer.html' }
3 changes: 3 additions & 0 deletions docs/guides/templates/twig.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ route
**type**: ``string``
**default**: ``normal``

.. docblock:: \phpDocumentor\Transformer\Writer\Twig\Extension::route()


Renders a link to the generated page for the node. ``presentation`` can be set to ``'url'`` to render only the URL.

sort_asc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

Check failure on line 1 in src/phpDocumentor/Guides/Compiler/NodeTransformer/DocblockNodeTransformer.php

View workflow job for this annotation

GitHub Actions / Codestyle

Missing declare(strict_types=1).

namespace phpDocumentor\Guides\Compiler\NodeTransformer;

use phpDocumentor\Compiler\DescriptorRepository;
use phpDocumentor\Guides\Compiler\CompilerContext;
use phpDocumentor\Guides\Compiler\NodeTransformer;
use phpDocumentor\Guides\Nodes\DocblockNode;
use phpDocumentor\Guides\Nodes\Node;

class DocblockNodeTransformer implements NodeTransformer

Check failure on line 11 in src/phpDocumentor/Guides/Compiler/NodeTransformer/DocblockNodeTransformer.php

View workflow job for this annotation

GitHub Actions / Phpstan

Class phpDocumentor\Guides\Compiler\NodeTransformer\DocblockNodeTransformer implements generic interface phpDocumentor\Guides\Compiler\NodeTransformer but does not specify its types: T
{
public function __construct(private readonly DescriptorRepository $descriptorRepository)
{
}

public function enterNode(Node $node, CompilerContext $compilerContext): Node
{
return $node;
}

public function leaveNode(Node $node, CompilerContext $compilerContext): Node|null
{
if ($node instanceof DocblockNode === false) {
return $node;
}

$descriptor = $this->descriptorRepository->findDescriptorByFqsen(
$node->getFqsen(),
);

return $node->withDescriptor($descriptor);
}

public function supports(Node $node): bool
{
return $node instanceof DocblockNode;
}

public function getPriority(): int
{
return 3000;
}
}
34 changes: 34 additions & 0 deletions src/phpDocumentor/Guides/NodeRenderer/DocblockRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

Check failure on line 1 in src/phpDocumentor/Guides/NodeRenderer/DocblockRenderer.php

View workflow job for this annotation

GitHub Actions / Codestyle

Missing declare(strict_types=1).

namespace phpDocumentor\Guides\NodeRenderer;

use phpDocumentor\Guides\NodeRenderers\NodeRenderer;
use phpDocumentor\Guides\Nodes\DocblockNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\RenderContext;
use phpDocumentor\Guides\TemplateRenderer;

class DocblockRenderer implements NodeRenderer

Check failure on line 11 in src/phpDocumentor/Guides/NodeRenderer/DocblockRenderer.php

View workflow job for this annotation

GitHub Actions / Phpstan

Class phpDocumentor\Guides\NodeRenderer\DocblockRenderer implements generic interface phpDocumentor\Guides\NodeRenderers\NodeRenderer but does not specify its types: T
{
public function __construct(private readonly TemplateRenderer $renderer)
{
}

public function supports(Node $node): bool
{
return $node instanceof DocblockNode;
}

public function render(Node $node, RenderContext $renderContext): string
{
if ($node->getDescriptor() === null) {

Check failure on line 24 in src/phpDocumentor/Guides/NodeRenderer/DocblockRenderer.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedInterfaceMethod

src/phpDocumentor/Guides/NodeRenderer/DocblockRenderer.php:24:20: UndefinedInterfaceMethod: Method phpDocumentor\Guides\Nodes\Node::getDescriptor does not exist (see https://psalm.dev/181)

Check failure on line 24 in src/phpDocumentor/Guides/NodeRenderer/DocblockRenderer.php

View workflow job for this annotation

GitHub Actions / Phpstan

Call to an undefined method phpDocumentor\Guides\Nodes\Node::getDescriptor().
return '';
}

return $this->renderer->renderTemplate(
$renderContext,
'components/method.html.twig',
['method' => $node->getDescriptor()],

Check failure on line 31 in src/phpDocumentor/Guides/NodeRenderer/DocblockRenderer.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedInterfaceMethod

src/phpDocumentor/Guides/NodeRenderer/DocblockRenderer.php:31:33: UndefinedInterfaceMethod: Method phpDocumentor\Guides\Nodes\Node::getDescriptor does not exist (see https://psalm.dev/181)

Check failure on line 31 in src/phpDocumentor/Guides/NodeRenderer/DocblockRenderer.php

View workflow job for this annotation

GitHub Actions / Phpstan

Call to an undefined method phpDocumentor\Guides\Nodes\Node::getDescriptor().
);
}
}
34 changes: 34 additions & 0 deletions src/phpDocumentor/Guides/Nodes/DocblockNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

Check failure on line 1 in src/phpDocumentor/Guides/Nodes/DocblockNode.php

View workflow job for this annotation

GitHub Actions / Codestyle

Missing declare(strict_types=1).

namespace phpDocumentor\Guides\Nodes;

use phpDocumentor\Descriptor\Descriptor;
use phpDocumentor\Reflection\Fqsen;

final class DocblockNode extends AbstractNode

Check failure on line 8 in src/phpDocumentor/Guides/Nodes/DocblockNode.php

View workflow job for this annotation

GitHub Actions / Phpstan

Class phpDocumentor\Guides\Nodes\DocblockNode extends generic class phpDocumentor\Guides\Nodes\AbstractNode but does not specify its types: TValue
{
private Descriptor|null $descriptor;

public function __construct(private Fqsen $fqsen)
{

}

Check failure on line 15 in src/phpDocumentor/Guides/Nodes/DocblockNode.php

View workflow job for this annotation

GitHub Actions / Codestyle

Function closing brace must go on the next line following the body; found 1 blank lines before brace

public function getFqsen(): Fqsen
{
return $this->fqsen;
}

public function withDescriptor(Descriptor|null $descriptor): self
{
$that = clone $this;
$that->descriptor = $descriptor;

return $that;
}

public function getDescriptor(): Descriptor|null
{
return $this->descriptor;
}
}
39 changes: 39 additions & 0 deletions src/phpDocumentor/Guides/RestructuredText/Directives/Docblock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

Check failure on line 1 in src/phpDocumentor/Guides/RestructuredText/Directives/Docblock.php

View workflow job for this annotation

GitHub Actions / Codestyle

Missing declare(strict_types=1).

namespace phpDocumentor\Guides\RestructuredText\Directives;

use phpDocumentor\Guides\Nodes\DocblockNode;
use phpDocumentor\Guides\Nodes\GenericNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
use phpDocumentor\Reflection\Fqsen;
use Psr\Log\LoggerInterface;

final class Docblock extends BaseDirective
{
public function __construct(private LoggerInterface $logger)
{
}

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

public function processNode(BlockContext $blockContext, Directive $directive,): Node

Check failure on line 24 in src/phpDocumentor/Guides/RestructuredText/Directives/Docblock.php

View workflow job for this annotation

GitHub Actions / Codestyle

Trailing comma after the last parameter in function declaration is disallowed.
{
try {
return new DocblockNode(new Fqsen($directive->getData()));
} catch (\InvalidArgumentException $e) {

Check failure on line 28 in src/phpDocumentor/Guides/RestructuredText/Directives/Docblock.php

View workflow job for this annotation

GitHub Actions / Codestyle

Class \InvalidArgumentException should not be referenced via a fully qualified name, but via a use statement.
$this->logger->warning(
sprintf(

Check failure on line 30 in src/phpDocumentor/Guides/RestructuredText/Directives/Docblock.php

View workflow job for this annotation

GitHub Actions / Codestyle

Function sprintf() should not be referenced via a fallback global name, but via a use statement.
'Invalid docblock directive: %s',
$e->getMessage()

Check failure on line 32 in src/phpDocumentor/Guides/RestructuredText/Directives/Docblock.php

View workflow job for this annotation

GitHub Actions / Codestyle

Multi-line function calls must have a trailing comma after the last parameter.
)

Check failure on line 33 in src/phpDocumentor/Guides/RestructuredText/Directives/Docblock.php

View workflow job for this annotation

GitHub Actions / Codestyle

Multi-line function calls must have a trailing comma after the last parameter.
);
}

return new GenericNode('invalid', $directive->getData());
}
}
26 changes: 21 additions & 5 deletions src/phpDocumentor/Transformer/Writer/Twig/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,11 @@ public function getFilters(): array
),
'route' => new TwigFilter(
'route',
fn (
$value,
string $presentation = LinkRenderer::PRESENTATION_NORMAL,
) => $this->routeRenderer->render($value, $presentation),
['is_safe' => ['all']],
$this->route(...),
[
'needs_context' => true,
'is_safe' => ['all']
],
),
'sort' => new TwigFilter('sort_*', $this->sort(...)),
'sortByVisibility' => new TwigFilter('sortByVisibility', $this->sortByVisibility(...)),
Expand Down Expand Up @@ -507,4 +507,20 @@ static function (Descriptor $a, Descriptor $b) use ($visibilityOrder): int {

return $iterator;
}

/**
* Renders an url for a node or type.
*
* ```twig
* {{ node|route('class') }}
* ```
* @param mixed[] $context
* @param DescriptorAbstract|Fqsen|Reference\Reference|Path|string|iterable<mixed> $element
*
* @return string|list<string>
*/
public function route($context, $element, string $presentation = LinkRenderer::PRESENTATION_NORMAL)
{
return $this->renderRoute($context, $element, $presentation);
}
}