diff --git a/src/DocBlock/Tags/Covers.php b/src/DocBlock/Tags/Covers.php index 096d836..9e52e5e 100644 --- a/src/DocBlock/Tags/Covers.php +++ b/src/DocBlock/Tags/Covers.php @@ -49,7 +49,7 @@ final class Covers extends BaseTag implements Factory\StaticMethod ?FqsenResolver $resolver = null, ?TypeContext $context = null ) : self { - Assert::notEmpty($body); + Assert::stringNotEmpty($body); Assert::notNull($descriptionFactory); Assert::notNull($resolver); diff --git a/src/DocBlock/Tags/Example.php b/src/DocBlock/Tags/Example.php index c91f75c..4f95c89 100644 --- a/src/DocBlock/Tags/Example.php +++ b/src/DocBlock/Tags/Example.php @@ -45,9 +45,14 @@ final class Example implements Tag, Factory\StaticMethod /** @var string|null */ private $content; - public function __construct(string $filePath, bool $isURI, int $startingLine, int $lineCount, ?string $content) - { - Assert::notEmpty($filePath); + public function __construct( + string $filePath, + bool $isURI, + int $startingLine, + int $lineCount, + ?string $content + ) { + Assert::stringNotEmpty($filePath); Assert::greaterThanEq($startingLine, 1); Assert::greaterThanEq($lineCount, 0); @@ -64,7 +69,7 @@ final class Example implements Tag, Factory\StaticMethod public function getContent() : string { if ($this->content === null || $this->content === '') { - $filePath = '"' . $this->filePath . '"'; + $filePath = $this->filePath; if ($this->isURI) { $filePath = $this->isUriRelative($this->filePath) ? str_replace('%2F', '/', rawurlencode($this->filePath)) @@ -85,7 +90,7 @@ final class Example implements Tag, Factory\StaticMethod public static function create(string $body) : ?Tag { // File component: File path in quotes or File URI / Source information - if (!preg_match('/^(?:\"([^\"]+)\"|(\S+))(?:\s+(.*))?$/sux', $body, $matches)) { + if (!preg_match('/^\s*(?:(\"[^\"]+\")|(\S+))(?:\s+(.*))?$/sux', $body, $matches)) { return null; } @@ -134,7 +139,7 @@ final class Example implements Tag, Factory\StaticMethod */ public function getFilePath() : string { - return $this->filePath; + return trim($this->filePath, '"'); } /** @@ -143,9 +148,15 @@ final class Example implements Tag, Factory\StaticMethod public function __toString() : string { $filePath = (string) $this->filePath; + $isDefaultLine = $this->startingLine === 1 && $this->lineCount === 0; + $startingLine = !$isDefaultLine ? (string) $this->startingLine : ''; + $lineCount = !$isDefaultLine ? (string) $this->lineCount : ''; $content = (string) $this->content; - return $filePath . ($content !== '' ? ($filePath !== '' ? ' ' : '') . $content : ''); + return $filePath + . ($startingLine !== '' ? ($filePath !== '' ? ' ' : '') . $startingLine : '') + . ($lineCount !== '' ? ($filePath !== '' || $startingLine !== '' ? ' ' : '') . $lineCount : '') + . ($content !== '' ? ($filePath !== '' || $startingLine !== '' || $lineCount !== '' ? ' ' : '') . $content : ''); } /** diff --git a/tests/unit/DocBlock/Tags/ExampleTest.php b/tests/unit/DocBlock/Tags/ExampleTest.php index 1d6d159..3bf7aa0 100644 --- a/tests/unit/DocBlock/Tags/ExampleTest.php +++ b/tests/unit/DocBlock/Tags/ExampleTest.php @@ -93,6 +93,52 @@ class ExampleTest extends TestCase $this->assertEquals(5, $tag->getLineCount()); } + /** + * @covers ::create + * @covers ::__toString + */ + public function testStringRepresentationIsReturned() : void + { + $tag = Example::create('"example1.php" 10 5 test text'); + + $this->assertSame('"example1.php" 10 5 test text', (string) $tag); + + // --- + + $tag = Example::create('file://example1.php'); + + $this->assertSame('file://example1.php', (string) $tag); + + // --- + + $tag = Example::create('0 foo bar'); + + $this->assertSame('0 foo bar', (string) $tag); + + // --- + + $tag = Example::create('$redisCluster->pttl(\'key\');'); + + $this->assertSame('$redisCluster->pttl(\'key\');', (string) $tag); + + // --- + + $tag = Example::create(' "example1.php" 10 5 test text '); + + $this->assertSame('"example1.php" 10 5 test text', (string) $tag); + } + + /** + * @covers ::create + * @covers ::__toString + */ + public function testStringRepresentationIsReturnedWithoutDescription() : void + { + $tag = Example::create(''); + + $this->assertSame('', (string) $tag); + } + /** * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag *