Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/DocBlock/StandardTagFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use phpDocumentor\Reflection\DocBlock\Tags\Author;
use phpDocumentor\Reflection\DocBlock\Tags\Covers;
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated;
use phpDocumentor\Reflection\DocBlock\Tags\ExpectedFormat;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\AbstractPHPStanFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\ExtendsFactory;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\Factory;
Expand Down Expand Up @@ -54,6 +55,8 @@
use function call_user_func_array;
use function get_class;
use function is_object;
use function is_string;
use function is_subclass_of;
use function preg_match;
use function sprintf;
use function strpos;
Expand Down Expand Up @@ -258,12 +261,29 @@ private function createTag(string $body, string $name, TypeContext $context): Ta
/** @phpstan-var callable(string): ?Tag $callable */
$tag = call_user_func_array($callable, $arguments);

return $tag ?? InvalidTag::create($body, $name);
return $tag ?? $this->createInvalidTag($handlerClassName, $body, $name);
} catch (InvalidArgumentException $e) {
return InvalidTag::create($body, $name)->withError($e);
return $this->createInvalidTag($handlerClassName, $body, $name)->withError($e);
}
}

/**
* @param class-string<Tag>|Tag|Factory $handlerClassName
*/
private function createInvalidTag($handlerClassName, string $body, string $name): InvalidTag
{
$invalidTag = InvalidTag::create($body, $name);

if (is_string($handlerClassName) && is_subclass_of($handlerClassName, ExpectedFormat::class)) {
$invalidTag = $invalidTag->withFormatHint(
$handlerClassName::getExpectedFormat(),
$handlerClassName::getDocumentationUrl()
);
}

return $invalidTag;
}

/**
* Determines the Fully Qualified Class Name of the Factory or Tag (containing a Factory Method `create`).
*
Expand Down
12 changes: 11 additions & 1 deletion src/DocBlock/Tags/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* Reflection class for an {@}author tag in a Docblock.
*/
final class Author extends BaseTag
final class Author extends BaseTag implements ExpectedFormat
{
/** @var string register that this is the author tag. */
protected string $name = 'author';
Expand Down Expand Up @@ -99,4 +99,14 @@ public static function create(string $body): ?self

return new static($authorName, $email);
}

public static function getExpectedFormat(): string
{
return 'name [<email@example.com>]';
}

public static function getDocumentationUrl(): ?string
{
return 'https://docs.phpdoc.org/3.0/guide/references/phpdoc/tags/author.html';
}
}
33 changes: 33 additions & 0 deletions src/DocBlock/Tags/ExpectedFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/

namespace phpDocumentor\Reflection\DocBlock\Tags;

/**
* Tag handlers may implement this contract to describe what they expect as input. When the handler rejects a body
* and an {@see \phpDocumentor\Reflection\DocBlock\Tags\InvalidTag} is produced, the factory forwards these hints to
* the invalid tag so downstream tooling (for example phpDocumentor's error reporting) can explain the expected
* syntax instead of only showing the raw exception.
*/
interface ExpectedFormat
{
/**
* Returns a short, human-readable description of the expected tag body, e.g. "name [<email>]".
*/
public static function getExpectedFormat(): string;

/**
* Returns a URL pointing to the canonical documentation for the tag, or null when none is available.
*/
public static function getDocumentationUrl(): ?string;
}
46 changes: 45 additions & 1 deletion src/DocBlock/Tags/InvalidTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ final class InvalidTag implements Tag

private ?Throwable $throwable = null;

private ?string $expectedFormat = null;

private ?string $documentationUrl = null;

private function __construct(string $name, string $body)
{
$this->name = $name;
Expand All @@ -51,6 +55,23 @@ public function getException(): ?Throwable
return $this->throwable;
}

/**
* Returns a short description of the format the corresponding tag handler expected, or null when the handler
* did not advertise one via {@see \phpDocumentor\Reflection\DocBlock\Tags\ExpectedFormat}.
*/
public function getExpectedFormat(): ?string
{
return $this->expectedFormat;
}

/**
* Returns a URL pointing to the canonical documentation of the tag, or null when none is available.
*/
public function getDocumentationUrl(): ?string
{
return $this->documentationUrl;
}

public function getName(): string
{
return $this->name;
Expand All @@ -64,12 +85,35 @@ public static function create(string $body, string $name = ''): self
public function withError(Throwable $exception): self
{
$this->flattenExceptionBacktrace($exception);
$tag = new self($this->name, $this->body);
$tag = $this->copy();
$tag->throwable = $exception;

return $tag;
}

/**
* Returns a copy of this invalid tag that also carries hints about the syntax expected by the originating tag
* handler. Both arguments are optional so callers can advertise whichever information they have.
*/
public function withFormatHint(?string $expectedFormat, ?string $documentationUrl = null): self
{
$tag = $this->copy();
$tag->expectedFormat = $expectedFormat;
$tag->documentationUrl = $documentationUrl;

return $tag;
}

private function copy(): self
{
$tag = new self($this->name, $this->body);
$tag->throwable = $this->throwable;
$tag->expectedFormat = $this->expectedFormat;
$tag->documentationUrl = $this->documentationUrl;

return $tag;
}

/**
* Removes all complex types from backtrace
*
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/DocBlock/StandardTagFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter;
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
use phpDocumentor\Reflection\DocBlock\Tags\Implements_;
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
use phpDocumentor\Reflection\DocBlock\Tags\Method;
use phpDocumentor\Reflection\DocBlock\Tags\Mixin;
use phpDocumentor\Reflection\DocBlock\Tags\Param;
Expand Down Expand Up @@ -135,6 +136,52 @@ public function testCreatingASpecificTag(): void
$this->assertSame('author', $tag->getName());
}

/**
* @uses \phpDocumentor\Reflection\DocBlock\StandardTagFactory::addService
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Author
* @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
* @uses \phpDocumentor\Reflection\DocBlock\Tags\InvalidTag
*
* @covers ::__construct
* @covers ::create
*/
public function testInvalidTagReceivesFormatHintFromHandler(): void
{
$context = new Context('');
$tagFactory = StandardTagFactory::createInstance(m::mock(FqsenResolver::class));

$tag = $tagFactory->create('@author Mike <not-an-email>', $context);

$this->assertInstanceOf(InvalidTag::class, $tag);
$this->assertSame('author', $tag->getName());
$this->assertSame(Author::getExpectedFormat(), $tag->getExpectedFormat());
$this->assertSame(Author::getDocumentationUrl(), $tag->getDocumentationUrl());
}

/**
* @uses \phpDocumentor\Reflection\DocBlock\StandardTagFactory::addService
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Generic
* @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
* @uses \phpDocumentor\Reflection\DocBlock\Tags\InvalidTag
*
* @covers ::__construct
* @covers ::create
*/
public function testInvalidTagKeepsNullHintsWhenHandlerDoesNotAdvertiseAny(): void
{
$context = new Context('');
$descriptionFactory = m::mock(DescriptionFactory::class);
$descriptionFactory->shouldReceive('create')->andThrow(new InvalidArgumentException('boom'));
$tagFactory = StandardTagFactory::createInstance(m::mock(FqsenResolver::class));
$tagFactory->addService($descriptionFactory, DescriptionFactory::class);

$tag = $tagFactory->create('@custom anything', $context);

$this->assertInstanceOf(InvalidTag::class, $tag);
$this->assertNull($tag->getExpectedFormat());
$this->assertNull($tag->getDocumentationUrl());
}

/**
* @uses \phpDocumentor\Reflection\DocBlock\StandardTagFactory::addService
* @uses \phpDocumentor\Reflection\DocBlock\Tags\See
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/DocBlock/Tags/InvalidTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,37 @@ public function testCreationWithoutError(): void
self::assertSame('name', $tag->getName());
self::assertSame('@name Body', $tag->render());
self::assertNull($tag->getException());
self::assertNull($tag->getExpectedFormat());
self::assertNull($tag->getDocumentationUrl());
}

/**
* @covers ::withFormatHint
* @covers ::getExpectedFormat
* @covers ::getDocumentationUrl
*/
public function testCreationWithFormatHint(): void
{
$tag = InvalidTag::create('Body', 'name')
->withFormatHint('expected format', 'https://example.com/doc');

self::assertSame('expected format', $tag->getExpectedFormat());
self::assertSame('https://example.com/doc', $tag->getDocumentationUrl());
}

/**
* @covers ::withFormatHint
* @covers ::withError
*/
public function testFormatHintSurvivesWithError(): void
{
$tag = InvalidTag::create('Body', 'name')
->withFormatHint('expected format', 'https://example.com/doc')
->withError(new Exception('boom'));

self::assertSame('expected format', $tag->getExpectedFormat());
self::assertSame('https://example.com/doc', $tag->getDocumentationUrl());
self::assertNotNull($tag->getException());
}

/**
Expand Down