Skip to content

Commit 6f0fc03

Browse files
committed
Added setters at Location and Description;
Added DocBlock::appendTag(); Minor reorganization at the tag setters;
1 parent b095253 commit 6f0fc03

File tree

12 files changed

+236
-81
lines changed

12 files changed

+236
-81
lines changed

src/phpDocumentor/Reflection/DocBlock.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace phpDocumentor\Reflection;
1414

15+
use phpDocumentor\Reflection\DocBlock\Tag;
1516
use phpDocumentor\Reflection\DocBlock\Context;
1617
use phpDocumentor\Reflection\DocBlock\Location;
1718

@@ -28,13 +29,13 @@ class DocBlock implements \Reflector
2829
protected $short_description = '';
2930

3031
/**
31-
* @var \phpDocumentor\Reflection\DocBlock\LongDescription The actual
32+
* @var DocBlock\Description The actual
3233
* description for this docblock.
3334
*/
3435
protected $long_description = null;
3536

3637
/**
37-
* @var \phpDocumentor\Reflection\DocBlock\Tags[] An array containing all
38+
* @var Tag[] An array containing all
3839
* the tags in this docblock; except inline.
3940
*/
4041
protected $tags = array();
@@ -221,7 +222,7 @@ protected function parseTags($tags)
221222

222223
// create proper Tag objects
223224
foreach ($result as $key => $tag_line) {
224-
$result[$key] = DocBlock\Tag::createInstance($tag_line, $this);
225+
$result[$key] = Tag::createInstance($tag_line, $this);
225226
}
226227
}
227228

@@ -271,7 +272,7 @@ public function getLocation()
271272
/**
272273
* Returns the tags for this DocBlock.
273274
*
274-
* @return DocBlock\Tag[]
275+
* @return Tag[]
275276
*/
276277
public function getTags()
277278
{
@@ -284,13 +285,13 @@ public function getTags()
284285
*
285286
* @param string $name String to search by.
286287
*
287-
* @return DocBlock\Tag[]
288+
* @return Tag[]
288289
*/
289290
public function getTagsByName($name)
290291
{
291292
$result = array();
292293

293-
/** @var DocBlock\Tag $tag */
294+
/** @var Tag $tag */
294295
foreach ($this->getTags() as $tag) {
295296
if ($tag->getName() != $name) {
296297
continue;
@@ -311,7 +312,7 @@ public function getTagsByName($name)
311312
*/
312313
public function hasTag($name)
313314
{
314-
/** @var DocBlock\Tag $tag */
315+
/** @var Tag $tag */
315316
foreach ($this->getTags() as $tag) {
316317
if ($tag->getName() == $name) {
317318
return true;
@@ -320,6 +321,32 @@ public function hasTag($name)
320321

321322
return false;
322323
}
324+
325+
/**
326+
* Appends a tag at the end of the list of tags.
327+
*
328+
* @param Tag $tag The tag to add.
329+
*
330+
* @return Tag The newly added tag.
331+
*
332+
* @throws \LogicException When the tag belongs to a different DocBlock.
333+
*/
334+
public function appendTag(Tag $tag)
335+
{
336+
if (null === $tag->getDocBlock()) {
337+
$tag->setDocBlock($this);
338+
}
339+
340+
if ($tag->getDocBlock() === $this) {
341+
$this->tags[] = $tag;
342+
} else {
343+
throw new \LogicException(
344+
'This tag belongs to a different DocBlock object.'
345+
);
346+
}
347+
348+
return $tag;
349+
}
323350

324351
/**
325352
* Builds a string representation of this object.

src/phpDocumentor/Reflection/DocBlock/Description.php

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,16 @@ class Description implements \Reflector
3535
/**
3636
* Populates the fields of a description.
3737
*
38-
* @param string $content The DocBlock contents without asterisks.
38+
* @param string $content The description's conetnts.
3939
* @param DocBlock $docblock The DocBlock which this description belongs to.
4040
*/
4141
public function __construct($content, DocBlock $docblock = null)
4242
{
43-
$this->contents = trim($content);
44-
$this->docblock = $docblock;
43+
$this->setContent($content)->setDocBlock($docblock);
4544
}
4645

4746
/**
48-
* Returns the text of this description.
47+
* Gets the text of this description.
4948
*
5049
* @return string
5150
*/
@@ -54,6 +53,21 @@ public function getContents()
5453
return $this->contents;
5554
}
5655

56+
/**
57+
* Sets the text of this description.
58+
*
59+
* @param string $content The new text of this description.
60+
*
61+
* @return $this
62+
*/
63+
public function setContent($content)
64+
{
65+
$this->contents = trim($content);
66+
67+
$this->parsedContents = null;
68+
return $this;
69+
}
70+
5771
/**
5872
* Returns the parsed text of this description.
5973
*
@@ -98,7 +112,9 @@ public function getParsedContents()
98112
null,
99113
PREG_SPLIT_DELIM_CAPTURE
100114
);
101-
for ($i=1, $l = count($this->parsedContents); $i<$l; $i += 2) {
115+
116+
$count = count($this->parsedContents);
117+
for ($i=1; $i<$count; $i += 2) {
102118
$this->parsedContents[$i] = Tag::createInstance(
103119
$this->parsedContents[$i],
104120
$this->docblock
@@ -108,7 +124,7 @@ public function getParsedContents()
108124
//In order to allow "literal" inline tags, the otherwise invalid
109125
//sequence "{@}" is changed to "@", and "{}" is changed to "}".
110126
//See unit tests for examples.
111-
for ($i=0, $l = count($this->parsedContents); $i<$l; $i += 2) {
127+
for ($i=0; $i<$count; $i += 2) {
112128
$this->parsedContents[$i] = str_replace(
113129
array('{@}', '{}'),
114130
array('@', '}'),
@@ -153,6 +169,31 @@ public function getFormattedContents()
153169
return trim($result);
154170
}
155171

172+
/**
173+
* Gets the docblock this tag belongs to.
174+
*
175+
* @return DocBlock The docblock this description belongs to.
176+
*/
177+
public function getDocBlock()
178+
{
179+
return $this->docblock;
180+
}
181+
182+
/**
183+
* Sets the docblock this tag belongs to.
184+
*
185+
* @param DocBlock $docblock The new docblock this description belongs to.
186+
* Setting NULL removes any association.
187+
*
188+
* @return $this
189+
*/
190+
public function setDocBlock(DocBlock $docblock = null)
191+
{
192+
$this->docblock = $docblock;
193+
194+
return $this;
195+
}
196+
156197
/**
157198
* Builds a string representation of this object.
158199
*

src/phpDocumentor/Reflection/DocBlock/Location.php

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,55 @@
2222
class Location
2323
{
2424
/** @var int Line where the DocBlock text starts. */
25-
protected $line_number = 0;
25+
protected $lineNumber = 0;
2626

2727
/** @var int Column where the DocBlock text starts. */
28-
protected $column_number = 0;
28+
protected $columnNumber = 0;
2929

3030
public function __construct(
31-
$line_number = 0,
32-
$column_number = 0
31+
$lineNumber = 0,
32+
$columnNumber = 0
3333
) {
34-
$this->line_number = (int)$line_number;
35-
$this->column_number = (int)$column_number;
34+
$this->setLineNumber($lineNumber)->setColumnNumber($columnNumber);
3635
}
3736

3837
/**
3938
* @return int Line where the DocBlock text starts.
4039
*/
4140
public function getLineNumber()
4241
{
43-
return $this->line_number;
42+
return $this->lineNumber;
43+
}
44+
45+
/**
46+
*
47+
* @param type $lineNumber
48+
* @return $this
49+
*/
50+
public function setLineNumber($lineNumber)
51+
{
52+
$this->lineNumber = (int)$lineNumber;
53+
54+
return $this;
4455
}
4556

4657
/**
4758
* @return int Column where the DocBlock text starts.
4859
*/
4960
public function getColumnNumber()
5061
{
51-
return $this->column_number;
62+
return $this->columnNumber;
63+
}
64+
65+
/**
66+
*
67+
* @param int $columnNumber
68+
* @return $this
69+
*/
70+
public function setColumnNumber($columnNumber)
71+
{
72+
$this->columnNumber = (int)$columnNumber;
73+
74+
return $this;
5275
}
5376
}

src/phpDocumentor/Reflection/DocBlock/Tag/AuthorTag.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
*/
2424
class AuthorTag extends Tag
2525
{
26+
/**
27+
* PCRE regular expression matching any valid value for the name component.
28+
*/
29+
const REGEX_AUTHOR_NAME = '[^\<]*';
30+
31+
/**
32+
* PCRE regular expression matching any valid value for the email component.
33+
*/
34+
const REGEX_AUTHOR_EMAIL = '[^\>]*';
35+
2636
/** @var string The name of the author */
2737
protected $authorName = '';
2838

@@ -48,7 +58,9 @@ public function setContent($content)
4858
{
4959
parent::setContent($content);
5060
if (preg_match(
51-
'/^([^\<]*)(\<([^\>]*)\>)?$/u',
61+
'/^(' . self::REGEX_AUTHOR_NAME .
62+
')(\<(' . self::REGEX_AUTHOR_EMAIL .
63+
')\>)?$/u',
5264
$this->description,
5365
$matches
5466
)) {
@@ -75,13 +87,16 @@ public function getAuthorName()
7587
* Sets the author's name.
7688
*
7789
* @param string $authorName The new author name.
90+
* An invalid value will set an empty string.
7891
*
7992
* @return $this
8093
*/
8194
public function setAuthorName($authorName)
8295
{
8396
$this->content = null;
84-
$this->authorName = $authorName;
97+
$this->authorName
98+
= preg_match('/^' . self::REGEX_AUTHOR_NAME . '$/u', $authorName)
99+
? $authorName : '';
85100

86101
return $this;
87102
}
@@ -100,14 +115,17 @@ public function getAuthorEmail()
100115
* Sets the author's email.
101116
*
102117
* @param string $authorEmail The new author email.
118+
* An invalid value will set an empty string.
103119
*
104120
* @return $this
105121
*/
106122
public function setAuthorEmail($authorEmail)
107123
{
108-
$this->content = null;
109-
$this->authorEmail = $authorEmail;
124+
$this->authorEmail
125+
= preg_match('/^' . self::REGEX_AUTHOR_EMAIL . '$/u', $authorEmail)
126+
? $authorEmail : '';
110127

128+
$this->content = null;
111129
return $this;
112130
}
113131
}

0 commit comments

Comments
 (0)