Skip to content

Commit ed4c6b7

Browse files
committed
Write example on escaping values in a DocBlock
1 parent 216cf00 commit ed4c6b7

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/../../vendor/autoload.php');
4+
5+
use phpDocumentor\Reflection\DocBlockFactory;
6+
7+
$docComment = <<<DOCCOMMENT
8+
/**
9+
* This is an example of a summary.
10+
*
11+
* You can escape the @-sign by surrounding it with braces, for example: {@}. And escape a closing brace within an
12+
* inline tag by adding an opening brace in front of it like this: {}.
13+
*
14+
* Here are example texts where you can see how they could be used in a real life situation:
15+
*
16+
* This is a text with an {@internal inline tag where a closing brace ({}) is shown}.
17+
* Or an {@internal inline tag with a literal {{@}link{} in it}.
18+
*
19+
* Do note that an {@internal inline tag that has an opening brace ({) does not break out}.
20+
*/
21+
DOCCOMMENT;
22+
23+
$factory = DocBlockFactory::createInstance();
24+
$docblock = $factory->create($docComment);
25+
26+
// Escaping is automatic so this happens in the DescriptionFactory.
27+
$description = $docblock->getDescription();
28+
29+
// This is the rendition that we will receive of the Description.
30+
$receivedDocComment = <<<DOCCOMMENT
31+
/**
32+
* This is an example of a summary.
33+
*
34+
* You can escape the @-sign by surrounding it with braces, for example: {@}. And escape a closing brace within an
35+
* inline tag by adding an opening brace in front of it like this: {}.
36+
*
37+
* Here are example texts where you can see how they could be used in a real life situation:
38+
*
39+
* This is a text with an {@internal inline tag where a closing brace ({}) is shown}.
40+
* Or an {@internal inline tag with a literal {{@}link{} in it}.
41+
*
42+
* Do note that an {@internal inline tag that has an opening brace ({) does not break out}.
43+
*/
44+
DOCCOMMENT;
45+
46+
// Render it using the default PassthroughFormatter
47+
$foundDescription = $description->render();

tests/integration/InterpretingDocBlocksTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,32 @@ public function testInterpretingTags()
6666
$this->assertSame('\\' . StandardTagFactory::class, (string)$seeTag->getReference());
6767
$this->assertSame('', (string)$seeTag->getDescription());
6868
}
69+
70+
public function testDescriptionsCanEscapeAtSignsAndClosingBraces()
71+
{
72+
/**
73+
* @var string $docComment
74+
* @var DocBlock $docblock
75+
* @var Description $description
76+
* @var string $receivedDocComment
77+
* @var string $foundDescription
78+
*/
79+
80+
include(__DIR__ . '/../../examples/playing-with-descriptions/02-escaping.php');
81+
$this->assertSame(<<<'DESCRIPTION'
82+
You can escape the @-sign by surrounding it with braces, for example: @. And escape a closing brace within an
83+
inline tag by adding an opening brace in front of it like this: }.
84+
85+
Here are example texts where you can see how they could be used in a real life situation:
86+
87+
This is a text with an {@internal inline tag where a closing brace (}) is shown}.
88+
Or an {@internal inline tag with a literal {@link} in it}.
89+
90+
Do note that an {@internal inline tag that has an opening brace ({) does not break out}.
91+
DESCRIPTION
92+
,
93+
$foundDescription
94+
)
95+
;
96+
}
6997
}

tests/integration/ReconstitutingADocBlockTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
/**
2121
* @coversNothing
2222
*/
23-
class InterpretingDocBlocksTest extends \PHPUnit_Framework_TestCase
23+
class ReconstitutingADocBlockTest extends \PHPUnit_Framework_TestCase
2424
{
25-
public function testInterpretingASimpleDocBlock()
25+
public function testReconstituteADocBlock()
2626
{
2727
/**
2828
* @var string $docComment

tests/unit/DocBlock/DescriptionFactoryTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ public function testIfSuperfluousStartingSpacesAreRemoved()
121121
description that you commonly
122122
see with tags.
123123
124-
It does have a code sample
124+
It does have a multiline code sample
125+
that should align, no matter what
125126
126127
All spaces superfluous spaces on the
127128
second and later lines should be
@@ -134,7 +135,8 @@ public function testIfSuperfluousStartingSpacesAreRemoved()
134135
description that you commonly
135136
see with tags.
136137
137-
It does have a code sample
138+
It does have a multiline code sample
139+
that should align, no matter what
138140
139141
All spaces superfluous spaces on the
140142
second and later lines should be

0 commit comments

Comments
 (0)