[BUGFIX] Strip trailing slash from parsed doc comment
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Reflection / DocCommentParser.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Reflection;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * A little parser which creates tag objects from doc comments
19 */
20 class DocCommentParser
21 {
22 /**
23 * @var string The description as found in the doc comment
24 */
25 protected $description = '';
26
27 /**
28 * @var array An array of tag names and their values (multiple values are possible)
29 */
30 protected $tags = array();
31
32 /**
33 * Parses the given doc comment and saves the result (description and
34 * tags) in the parser's object. They can be retrieved by the
35 * getTags() getTagValues() and getDescription() methods.
36 *
37 * @param string $docComment A doc comment as returned by the reflection getDocComment() method
38 * @return void
39 */
40 public function parseDocComment($docComment)
41 {
42 $this->description = '';
43 $this->tags = array();
44 $lines = explode(LF, $docComment);
45 foreach ($lines as $line) {
46 if ($line !== '' && strpos($line, '@') !== false) {
47 $this->parseTag(substr($line, strpos($line, '@')));
48 } elseif (empty($this->tags)) {
49 $this->description .= preg_replace('#\\s*/?[*/]*(.*)$#', '$1', $line) . LF;
50 }
51 }
52 $this->description = trim($this->description);
53 }
54
55 /**
56 * Returns the tags which have been previously parsed
57 *
58 * @return array Array of tag names and their (multiple) values
59 */
60 public function getTagsValues()
61 {
62 return $this->tags;
63 }
64
65 /**
66 * Returns the values of the specified tag. The doc comment
67 * must be parsed with parseDocComment() before tags are
68 * available.
69 *
70 * @param string $tagName The tag name to retrieve the values for
71 * @throws \RuntimeException
72 * @return array The tag's values
73 */
74 public function getTagValues($tagName)
75 {
76 if (!$this->isTaggedWith($tagName)) {
77 throw new \RuntimeException('Tag "' . $tagName . '" does not exist.', 1169128255);
78 }
79 return $this->tags[$tagName];
80 }
81
82 /**
83 * Checks if a tag with the given name exists
84 *
85 * @param string $tagName The tag name to check for
86 * @return bool TRUE the tag exists, otherwise FALSE
87 */
88 public function isTaggedWith($tagName)
89 {
90 return isset($this->tags[$tagName]);
91 }
92
93 /**
94 * Returns the description which has been previously parsed
95 *
96 * @return string The description which has been parsed
97 */
98 public function getDescription()
99 {
100 return $this->description;
101 }
102
103 /**
104 * Parses a line of a doc comment for a tag and its value.
105 * The result is stored in the interal tags array.
106 *
107 * @param string $line A line of a doc comment which starts with an @-sign
108 * @return void
109 */
110 protected function parseTag($line)
111 {
112 $tagAndValue = preg_split('/\\s/', $line, 2);
113 $tag = substr($tagAndValue[0], 1);
114 if (count($tagAndValue) > 1) {
115 $this->tags[$tag][] = trim($tagAndValue[1]);
116 } else {
117 $this->tags[$tag] = array();
118 }
119 }
120 }