` docblock tags and their descriptions from a docblock. * * See the `$tags` property for the list of supported tags. * * @param string $string The string to be parsed for tags * @return array Returns an array where each docblock tag is a key name, and the corresponding * values are either strings (if one of each tag), or arrays (if multiple of the same * tag). */ public static function tags($string) { $regex = '/\n@(?P' . join('|', static::$tags) . ")/msi"; $string = trim($string); $result = preg_split($regex, "\n$string", -1, PREG_SPLIT_DELIM_CAPTURE); $tags = []; for ($i = 1; $i < count($result) - 1; $i += 2) { $type = trim(strtolower($result[$i])); $text = trim($result[$i + 1]); if (isset($tags[$type])) { $tags[$type] = is_array($tags[$type]) ? $tags[$type] : (array) $tags[$type]; $tags[$type][] = $text; } else { $tags[$type] = $text; } } if (isset($tags['param'])) { $tags['params'] = static::_params((array) $tags['param']); unset($tags['param']); } return $tags; } /** * Parses `@param` docblock tags to separate out the parameter type from the description. * * @param array $params An array of `@param` tags, as parsed from the `tags()` method. * @return array Returns an array where each key is a parameter name, and each value is an * associative array containing `'type'` and `'text'` keys. */ protected static function _params(array $params) { $result = []; foreach ($params as $param) { $param = explode(' ', $param, 3); $type = $name = $text = null; foreach (['type', 'name', 'text'] as $i => $key) { if (!isset($param[$i])) { break; } ${$key} = $param[$i]; } if ($name) { $result[$name] = compact('type', 'text'); } } return $result; } } ?>