Skip to content

Commit

Permalink
Merge pull request #3719 from phpDocumentor/feature/attribute-ui-impr…
Browse files Browse the repository at this point in the history
…ovements

Feat: improvements on display of used attributes per element
  • Loading branch information
jaapio committed May 22, 2024
2 parents 4aa8366 + d2d232e commit 7194a32
Show file tree
Hide file tree
Showing 27 changed files with 116 additions and 52 deletions.
8 changes: 8 additions & 0 deletions config/stages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,44 @@ parameters:
- 'properties'
- 'methods'
- 'usedTraits'
- 'attributes'
'phpDocumentor\Descriptor\InterfaceDescriptor':
- 'tags'
- 'parent'
- 'constants'
- 'methods'
- 'attributes'
'phpDocumentor\Descriptor\TraitDescriptor':
- 'tags'
- 'properties'
- 'methods'
- 'usedTraits'
- 'attributes'
'phpDocumentor\Descriptor\EnumDescriptor':
- 'tags'
- 'interfaces'
- 'cases'
- 'methods'
- 'usedTraits'
- 'attributes'
'phpDocumentor\Descriptor\FunctionDescriptor':
- 'tags'
- 'arguments'
'phpDocumentor\Descriptor\MethodDescriptor':
- 'tags'
- 'arguments'
- 'attributes'
'phpDocumentor\Descriptor\ArgumentDescriptor':
- 'type'
'phpDocumentor\Descriptor\PropertyDescriptor':
- 'tags'
- 'type'
- 'attributes'
'phpDocumentor\Descriptor\ConstantDescriptor':
- 'tags'
- 'type'
- 'attributes'
'phpDocumentor\Descriptor\AttributeDescriptor': ['attribute']
'phpDocumentor\Descriptor\Tag\ParamDescriptor': ['type']
'phpDocumentor\Descriptor\Tag\VarDescriptor': ['type']
'phpDocumentor\Descriptor\Tag\ReturnDescriptor': ['type']
Expand Down
2 changes: 0 additions & 2 deletions data/templates/default/class.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
{{ include('components/class-title.html.twig') }}
{{ include('components/element-found-in.html.twig') }}
{{ include('components/element-header.html.twig') }}

{{ include('components/attributes.html.twig') }}
{{ include('components/constants.html.twig') }}
{{ include('components/properties.html.twig') }}
{{ include('components/methods.html.twig') }}
Expand Down
20 changes: 10 additions & 10 deletions data/templates/default/components/attribute.html.twig
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<article
class="
phpdocumentor-element
-attribute
"
>
<h4 class="phpdocumentor-element__name">
#[{{ attribute.name }}]
</h4>
</article>
<dl class="phpdocumentor-argument-list">
<dt class="phpdocumentor-argument-list__entry">
#[{{ attribute.attribute | route('class:short') }}]
</dt>
{% for argument in attribute.arguments %}
<dd class="phpdocumentor-argument-list__entry">
{% if argument.name %}${{ argument.name }}: {% endif %}{{ argument.value }}
</dd>
{% endfor %}
</dl>
7 changes: 3 additions & 4 deletions data/templates/default/components/attributes.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

{% if attributes is not empty %}
<section class="phpdocumentor-attributes">
<h3 class="phpdocumentor-elements__header" id="attributes">
Attributes
{{ include('components/headerlink.html.twig', {'on': node, 'at': 'attributes'}, with_context = false) }}
</h3>
<h5 class="phpdocumentor-elements__header" id="attributes">
Attributes {{ include('components/headerlink.html.twig', {'on': node, 'at': 'attributes'}, with_context = false) }}
</h5>
{% for attribute in attributes %}
{% include 'components/attribute.html.twig' %}
{% endfor %}
Expand Down
1 change: 1 addition & 0 deletions data/templates/default/components/constant.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
{{ include('components/description.html.twig', {'node': constant}) }}
{{ include('components/description.html.twig', {'node': constant.var[0]}) }}
{{ include ('components/tags.html.twig', {'node': constant}) }}
{{ include('components/attributes.html.twig' , {'node': constant}) }}
</article>
1 change: 1 addition & 0 deletions data/templates/default/components/element-header.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{{ include('components/summary.html.twig') }}
{{ include('components/description.html.twig') }}
{{ include('components/tags.html.twig', {node: node}) }}
{{ include('components/attributes.html.twig') }}

{{ include ('components/table-of-contents.html.twig') }}
1 change: 1 addition & 0 deletions data/templates/default/components/enum-case.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
{{ include('components/description.html.twig', {'node': case}) }}
{{ include('components/description.html.twig', {'node': case.var[0]}) }}
{{ include ('components/tags.html.twig', {'node': case}) }}
{{ include('components/attributes.html.twig' , {'node': case}) }}
</article>
1 change: 1 addition & 0 deletions data/templates/default/components/method.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
{{ include('components/description.html.twig', {'node': method}) }}
{{ include('components/method-arguments.html.twig', {'node': method}) }}
{{ include('components/tags.html.twig', {'node': method }) }}
{{ include('components/attributes.html.twig' , {'node': method}) }}
{{ include('components/method-response.html.twig', {'node': method}) }}
</article>
1 change: 1 addition & 0 deletions data/templates/default/components/property.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
{{ include('components/description.html.twig', {'node': property}) }}
{{ include('components/description.html.twig', {'node': property.var[0]}) }}
{{ include('components/tags.html.twig', {'node': property}) }}
{{ include('components/attributes.html.twig' , {'node': property}) }}
</article>
1 change: 0 additions & 1 deletion data/templates/default/enum.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
{{ include('components/element-found-in.html.twig') }}
{{ include('components/element-header.html.twig') }}

{{ include('components/attributes.html.twig') }}
{{ include('components/enum-cases.html.twig') }}
{{ include('components/methods.html.twig') }}
{{ include('components/source-modal.html.twig') }}
Expand Down
1 change: 0 additions & 1 deletion data/templates/default/interface.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
{{ include('components/element-found-in.html.twig') }}
{{ include('components/element-header.html.twig') }}

{{ include('components/attributes.html.twig') }}
{{ include('components/constants.html.twig') }}
{{ include('components/methods.html.twig') }}
{{ include('components/source-modal.html.twig') }}
Expand Down
1 change: 0 additions & 1 deletion data/templates/default/trait.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
{{ include('components/element-found-in.html.twig') }}
{{ include('components/element-header.html.twig') }}

{{ include('components/attributes.html.twig') }}
{{ include('components/constants.html.twig') }}
{{ include('components/properties.html.twig') }}
{{ include('components/methods.html.twig') }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
#[Stage(
'phpdoc.pipeline.api_documentation.compile',
10000,
'Replace textual FQCNs with object aliases',
description: 'Replace textual FQCNs with object aliases',
)]
final class Linker extends ApiDocumentationPass
{
Expand Down
41 changes: 41 additions & 0 deletions src/phpDocumentor/Descriptor/AttributeDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace phpDocumentor\Descriptor;

use phpDocumentor\Descriptor\Interfaces\AttributeInterface;
use phpDocumentor\Descriptor\Interfaces\ClassInterface;
use phpDocumentor\Descriptor\Traits\HasDescription;
use phpDocumentor\Descriptor\Traits\HasFqsen;
use phpDocumentor\Descriptor\Traits\HasName;
use phpDocumentor\Reflection\Fqsen;
use Stringable;

final class AttributeDescriptor implements Descriptor, Stringable, AttributeInterface
Expand All @@ -16,8 +18,47 @@ final class AttributeDescriptor implements Descriptor, Stringable, AttributeInte
use HasDescription;
use HasFqsen;

/** @var Collection<ValueObjects\CallArgument> */
private Collection $arguments;
private ClassInterface|null $attributeClass = null;

public function __construct()
{
$this->arguments = Collection::fromClassString(ValueObjects\CallArgument::class);
}

public function getAttribute(): ClassInterface|Fqsen|null
{
if ($this->attributeClass !== null) {
return $this->attributeClass;
}

return $this->fqsen;
}

public function setAttribute(ClassInterface|null $attributeClass): void
{
$this->attributeClass = $attributeClass;
}

public function __toString(): string
{
return $this->getName();
}

public function addArgument(ValueObjects\CallArgument $argument): void
{
$this->arguments->add($argument);
}

/** @return Collection<ValueObjects\CallArgument> */
public function getArguments(): Collection
{
return $this->arguments;
}

public function hasArguments(): bool
{
return $this->arguments->count() > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(private readonly ArgumentAssembler $argumentAssemble
*
* @param Function_ $data
*/
public function create(object $data): FunctionInterface
public function buildDescriptor(object $data): FunctionInterface
{
$functionDescriptor = new FunctionDescriptor();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(private readonly ArgumentAssembler $argumentAssemble
*
* @param Method $data
*/
public function create(object $data): MethodInterface
public function buildDescriptor(object $data): MethodInterface
{
$methodDescriptor = new MethodDescriptor();
$methodDescriptor->setNamespace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use phpDocumentor\Descriptor\Builder\AssemblerReducer;
use phpDocumentor\Descriptor\Descriptor;
use phpDocumentor\Descriptor\Interfaces\AttributedInterface;
use phpDocumentor\Descriptor\ValueObjects\CallArgument;
use phpDocumentor\Reflection\Php\AttributeContainer;

final class AttributeReducer implements AssemblerReducer
Expand All @@ -26,6 +27,10 @@ public function create(object $data, Descriptor|null $descriptor = null): Descri
$attributeDescriptor = new AttributeDescriptor();
$attributeDescriptor->setName($attribute->getName());
$attributeDescriptor->setFullyQualifiedStructuralElementName($attribute->getFqsen());
foreach ($attribute->getArguments() as $argument) {
$attributeDescriptor->addArgument(new CallArgument($argument->getValue(), $argument->getName()));
}

$descriptor->addAttribute($attributeDescriptor);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace phpDocumentor\Descriptor\Interfaces;

use phpDocumentor\Descriptor\Collection;
use phpDocumentor\Descriptor\ValueObjects\CallArgument;
use phpDocumentor\Reflection\Fqsen;

interface AttributeInterface
Expand All @@ -19,4 +21,11 @@ public function setFullyQualifiedStructuralElementName(Fqsen $name): void;
* Returns the Fully Qualified Structural Element Name (FQSEN) for this element.
*/
public function getFullyQualifiedStructuralElementName(): Fqsen|null;

public function addArgument(CallArgument $argument): void;

/** @return Collection<CallArgument> */
public function getArguments(): Collection;

public function hasArguments(): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,5 @@ interface AttributedInterface
/** @return Collection<AttributeInterface> */
public function getAttributes(): Collection;

/** @return Collection<AttributeInterface> */
public function getInheritedAttributes(): Collection;

public function addAttribute(AttributeInterface $attribute): void;
}
7 changes: 6 additions & 1 deletion src/phpDocumentor/Descriptor/Interfaces/MethodInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
/**
* Describes the public interface of the description for a method.
*/
interface MethodInterface extends ElementInterface, TypeInterface, InheritsFromElement, ChildInterface
interface MethodInterface extends
ElementInterface,
TypeInterface,
InheritsFromElement,
ChildInterface,
AttributedInterface
{
public function setAbstract(bool $abstract): void;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Descriptor representing a property on a class or trait.
*/
interface PropertyInterface extends ElementInterface, TypeInterface, ChildInterface, IsTyped
interface PropertyInterface extends ElementInterface, TypeInterface, ChildInterface, IsTyped, AttributedInterface
{
/**
* Returns true when this property is intended to be read-only.
Expand Down
3 changes: 1 addition & 2 deletions src/phpDocumentor/Descriptor/MethodDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
*/
class MethodDescriptor extends DescriptorAbstract implements
Interfaces\MethodInterface,
Interfaces\VisibilityInterface,
Interfaces\AttributedInterface
Interfaces\VisibilityInterface
{
use CanBeFinal;
use CanBeAbstract;
Expand Down
1 change: 1 addition & 0 deletions src/phpDocumentor/Descriptor/PropertyDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class PropertyDescriptor extends DescriptorAbstract implements
use Traits\HasVisibility;
use Traits\CanHaveAType;
use Traits\CanHaveADefaultValue;
use Traits\HasAttributes;

/** @var ClassInterface|TraitInterface|null $parent */
protected ElementInterface|null $parent = null;
Expand Down
19 changes: 0 additions & 19 deletions src/phpDocumentor/Descriptor/Traits/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
namespace phpDocumentor\Descriptor\Traits;

use phpDocumentor\Descriptor\Collection;
use phpDocumentor\Descriptor\Interfaces\AttributedInterface;
use phpDocumentor\Descriptor\Interfaces\AttributeInterface;
use phpDocumentor\Descriptor\Interfaces\ChildInterface;

trait HasAttributes
{
Expand All @@ -33,21 +31,4 @@ public function getAttributes(): Collection

return $this->attributes;
}

/** @return Collection<AttributeInterface> */
public function getInheritedAttributes(): Collection
{
if ($this instanceof ChildInterface === false) {
return Collection::fromInterfaceString(AttributeInterface::class);
}

$parent = $this->getParent();
if ($parent instanceof AttributedInterface === false) {
return $this->getAttributes();
}

return $this->getAttributes()->merge(
$parent->getInheritedAttributes(),
);
}
}
22 changes: 22 additions & 0 deletions src/phpDocumentor/Descriptor/ValueObjects/CallArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Descriptor\ValueObjects;

final class CallArgument
{
public function __construct(private readonly string $value, private readonly string|null $name)
{
}

public function getValue(): string
{
return $this->value;
}

public function getName(): string|null
{
return $this->name;
}
}
4 changes: 0 additions & 4 deletions src/phpDocumentor/Transformer/Writer/Twig/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,6 @@ static function (DescriptorAbstract $descriptor): Collection {
$attributes = $attributes->merge($descriptor->getAttributes());
}

if (method_exists($descriptor, 'getInheritedAttributes')) {
$attributes = $attributes->merge($descriptor->getInheritedAttributes());
}

return $attributes;
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function testAddAttribute(): void

public function testGetInheritedAttributesReturnsAttributesFromParent(): void
{
$this->markTestSkipped('Not implemented yet.');
$parent = $this->getParent();
$expected = 0;
if ($parent !== null) {
Expand Down

0 comments on commit 7194a32

Please sign in to comment.