[CLEANUP] Replace strlen() with === for zero length check
[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 $this->description = '';
42 $this->tags = array();
43 $lines = explode(LF, $docComment);
44 foreach ($lines as $line) {
45 if ($line !== '' && strpos($line, '@') !== FALSE) {
46 $this->parseTag(substr($line, strpos($line, '@')));
47 } elseif (count($this->tags) === 0) {
48 $this->description .= preg_replace('/\\s*\\/?[\\\\*]*(.*)$/', '$1', $line) . LF;
49 }
50 }
51 $this->description = trim($this->description);
52 }
53
54 /**
55 * Returns the tags which have been previously parsed
56 *
57 * @return array Array of tag names and their (multiple) values
58 */
59 public function getTagsValues() {
60 return $this->tags;
61 }
62
63 /**
64 * Returns the values of the specified tag. The doc comment
65 * must be parsed with parseDocComment() before tags are
66 * available.
67 *
68 * @param string $tagName The tag name to retrieve the values for
69 * @throws \RuntimeException
70 * @return array The tag's values
71 */
72 public function getTagValues($tagName) {
73 if (!$this->isTaggedWith($tagName)) {
74 throw new \RuntimeException('Tag "' . $tagName . '" does not exist.', 1169128255);
75 }
76 return $this->tags[$tagName];
77 }
78
79 /**
80 * Checks if a tag with the given name exists
81 *
82 * @param string $tagName The tag name to check for
83 * @return bool TRUE the tag exists, otherwise FALSE
84 */
85 public function isTaggedWith($tagName) {
86 return isset($this->tags[$tagName]);
87 }
88
89 /**
90 * Returns the description which has been previously parsed
91 *
92 * @return string The description which has been parsed
93 */
94 public function getDescription() {
95 return $this->description;
96 }
97
98 /**
99 * Parses a line of a doc comment for a tag and its value.
100 * The result is stored in the interal tags array.
101 *
102 * @param string $line A line of a doc comment which starts with an @-sign
103 * @return void
104 */
105 protected function parseTag($line) {
106 $tagAndValue = preg_split('/\\s/', $line, 2);
107 $tag = substr($tagAndValue[0], 1);
108 if (count($tagAndValue) > 1) {
109 $this->tags[$tag][] = trim($tagAndValue[1]);
110 } else {
111 $this->tags[$tag] = array();
112 }
113 }
114
115 }