DocCommentParser::getTagValues()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
namespace Romm\ConfigurationObject\Legacy\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 = [];
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
     */
39
    public function parseDocComment($docComment)
40
    {
41
        $this->description = '';
42
        $this->tags = [];
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 (empty($this->tags)) {
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
    {
61
        return $this->tags;
62
    }
63
64
    /**
65
     * Returns the values of the specified tag. The doc comment
66
     * must be parsed with parseDocComment() before tags are
67
     * available.
68
     *
69
     * @param string $tagName The tag name to retrieve the values for
70
     * @throws \RuntimeException
71
     * @return array The tag's values
72
     */
73
    public function getTagValues($tagName)
74
    {
75
        if (!$this->isTaggedWith($tagName)) {
76
            throw new \RuntimeException('Tag "' . $tagName . '" does not exist.', 1169128255);
77
        }
78
        return $this->tags[$tagName];
79
    }
80
81
    /**
82
     * Checks if a tag with the given name exists
83
     *
84
     * @param string $tagName The tag name to check for
85
     * @return bool TRUE the tag exists, otherwise FALSE
86
     */
87
    public function isTaggedWith($tagName)
88
    {
89
        return isset($this->tags[$tagName]);
90
    }
91
92
    /**
93
     * Returns the description which has been previously parsed
94
     *
95
     * @return string The description which has been parsed
96
     */
97
    public function getDescription()
98
    {
99
        return $this->description;
100
    }
101
102
    /**
103
     * Parses a line of a doc comment for a tag and its value.
104
     * The result is stored in the interal tags array.
105
     *
106
     * @param string $line A line of a doc comment which starts with an @-sign
107
     */
108
    protected function parseTag($line)
109
    {
110
        $tagAndValue = preg_split('/\\s/', $line, 2);
111
        $tag = substr($tagAndValue[0], 1);
112
        if (count($tagAndValue) > 1) {
113
            $this->tags[$tag][] = trim($tagAndValue[1]);
114
        } else {
115
            $this->tags[$tag] = [];
116
        }
117
    }
118
}
119