file-fetcher
|
An example of a custom tag called my-tag
with an optional description.
A Custom Tag is a class that can consist of two parts:
create
that is a static factory for this class.The static factory create
is used to convert a tag line (without the tag name) into an instance of the same tag object with the right constructor parameters set. This method has a dynamic list of parameters so that you can inject various dependencies, see the method's DocBlock for more information.
An object of this class, and its methods and properties, represent a single instance of that tag in your documentation in the form of a Value Object whose properties should not be changed after instantiation (it should be immutable).
Important: Tag classes that act as Factories using the
create
method should implement the TagFactory interface.
Public Member Functions | |
__construct (Description $description=null) | |
__toString () | |
![]() | |
getName () | |
getDescription () | |
render (?Formatter $formatter=null) | |
Static Public Member Functions | |
static | create (string $body, DescriptionFactory $descriptionFactory=null, Context $context=null) |
![]() | |
static | create (string $body) |
![]() | |
static | create (string $body) |
Protected Attributes | |
$name = 'my-tag' | |
![]() | |
$name = '' | |
$description | |
MyTag::__construct | ( | Description | $description = null | ) |
The constructor for this Tag; this should contain all properties for this object.
Description | $description | An example of how to add a Description to the tag; the Description is often an optional variable so passing null is allowed in this instance (though you can also construct an empty description object). |
MyTag::__toString | ( | ) |
Returns a rendition of the original tag line.
This method is used to reconstitute a DocBlock into its original form by the {
Implements phpDocumentor\Reflection\DocBlock\Tag.
|
static |
A static Factory that creates a new instance of the current Tag.
In this example the MyTag tag can be created by passing a description text as $body. Because we have added a $descriptionFactory that is type-hinted as DescriptionFactory we can now construct a new Description object and pass that to the constructor.
You could directly instantiate a Description object here but that won't be parsed for inline tags and Types won't be resolved. The DescriptionFactory will take care of those actions.
The create
method's interface states that this method only features a single parameter ($body
) but the {
Important: all properties following the
$body
should default tonull
, otherwise PHP will error because it no longer matches the interface. This is why you often see the default tags check that an optional argument is not null nonetheless.
string | $body | |
DescriptionFactory | $descriptionFactory | |
Context | null | $context | The Context is used to resolve Types and FQSENs, although optional it is highly recommended to pass it. If you omit it then it is assumed that the DocBlock is in the global namespace and has no use statements. |
create
method.
|
protected |