2 namespace Consolidation\AnnotatedCommand\Parser\Internal;
5 * Hold the tag definition for one tag in a DocBlock.
7 * The tag can be sliced into the following forms:
9 * - "@tag word description"
10 * - "@tag $variable description"
11 * - "@tag word $variable description"
15 /** @var string Name of the tag */
18 /** @var string|null Contents of the tag. */
21 const TAG_REGEX = '@(?P<tag>[^\s$]+)[\s]*';
22 const VARIABLE_REGEX = '\\$(?P<variable>[^\s$]+)[\s]*';
23 const VARIABLE_OR_WORD_REGEX = '\\$?(?P<variable>[^\s$]+)[\s]*';
24 const TYPE_REGEX = '(?P<type>[^\s$]+)[\s]*';
25 const WORD_REGEX = '(?P<word>[^\s$]+)[\s]*';
26 const DESCRIPTION_REGEX = '(?P<description>.*)';
27 const IS_TAG_REGEX = '/^[*\s]*@/';
30 * Check if the provided string begins with a tag
31 * @param string $subject
34 public static function isTag($subject)
36 return preg_match(self::IS_TAG_REGEX, $subject);
40 * Use a regular expression to separate the tag from the content.
42 * @param string $subject
43 * @param string[] &$matches Sets $matches['tag'] and $matches['description']
46 public static function splitTagAndContent($subject, &$matches)
48 $regex = '/' . self::TAG_REGEX . self::DESCRIPTION_REGEX . '/s';
49 return preg_match($regex, $subject, $matches);
53 * DockblockTag constructor
55 public function __construct($tag, $content = null)
58 $this->content = $content;
62 * Add more content onto a tag during parsing.
64 public function appendContent($line)
66 $this->content .= "\n$line";
70 * Return the tag - e.g. "@foo description" returns 'foo'
74 public function getTag()
80 * Return the content portion of the tag - e.g. "@foo bar baz boz" returns
85 public function getContent()
87 return $this->content;
91 * Convert tag back into a string.
93 public function __toString()
95 return '@' . $this->getTag() . ' ' . $this->getContent();
99 * Determine if tag is one of:
100 * - "@tag variable description"
101 * - "@tag $variable description"
102 * - "@tag type $variable description"
104 * @param string $subject
105 * @param string[] &$matches Sets $matches['variable'] and
106 * $matches['description']; might set $matches['type'].
109 public function hasVariable(&$matches)
112 $this->hasTypeVariableAndDescription($matches) ||
113 $this->hasVariableAndDescription($matches);
117 * Determine if tag is "@tag $variable description"
118 * @param string $subject
119 * @param string[] &$matches Sets $matches['variable'] and
120 * $matches['description']
123 public function hasVariableAndDescription(&$matches)
125 $regex = '/^\s*' . self::VARIABLE_OR_WORD_REGEX . self::DESCRIPTION_REGEX . '/s';
126 return preg_match($regex, $this->getContent(), $matches);
130 * Determine if tag is "@tag type $variable description"
132 * @param string $subject
133 * @param string[] &$matches Sets $matches['variable'],
134 * $matches['description'] and $matches['type'].
137 public function hasTypeVariableAndDescription(&$matches)
139 $regex = '/^\s*' . self::TYPE_REGEX . self::VARIABLE_REGEX . self::DESCRIPTION_REGEX . '/s';
140 return preg_match($regex, $this->getContent(), $matches);
144 * Determine if tag is "@tag word description"
145 * @param string $subject
146 * @param string[] &$matches Sets $matches['word'] and
147 * $matches['description']
150 public function hasWordAndDescription(&$matches)
152 $regex = '/^\s*' . self::WORD_REGEX . self::DESCRIPTION_REGEX . '/s';
153 return preg_match($regex, $this->getContent(), $matches);