annotate vendor/psy/psysh/src/Psy/Util/Docblock.php @ 7:848c88cfe644

More layout
author Chris Cannam
date Fri, 05 Jan 2018 13:59:44 +0000
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of Psy Shell.
Chris@0 5 *
Chris@0 6 * (c) 2012-2017 Justin Hileman
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Psy\Util;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * A docblock representation.
Chris@0 16 *
Chris@0 17 * Based on PHP-DocBlock-Parser by Paul Scott:
Chris@0 18 *
Chris@0 19 * {@link http://www.github.com/icio/PHP-DocBlock-Parser}
Chris@0 20 *
Chris@0 21 * @author Paul Scott <paul@duedil.com>
Chris@0 22 * @author Justin Hileman <justin@justinhileman.info>
Chris@0 23 */
Chris@0 24 class Docblock
Chris@0 25 {
Chris@0 26 /**
Chris@0 27 * Tags in the docblock that have a whitespace-delimited number of parameters
Chris@0 28 * (such as `@param type var desc` and `@return type desc`) and the names of
Chris@0 29 * those parameters.
Chris@0 30 *
Chris@0 31 * @var array
Chris@0 32 */
Chris@0 33 public static $vectors = array(
Chris@0 34 'throws' => array('type', 'desc'),
Chris@0 35 'param' => array('type', 'var', 'desc'),
Chris@0 36 'return' => array('type', 'desc'),
Chris@0 37 );
Chris@0 38
Chris@0 39 protected $reflector;
Chris@0 40
Chris@0 41 /**
Chris@0 42 * The description of the symbol.
Chris@0 43 *
Chris@0 44 * @var string
Chris@0 45 */
Chris@0 46 public $desc;
Chris@0 47
Chris@0 48 /**
Chris@0 49 * The tags defined in the docblock.
Chris@0 50 *
Chris@0 51 * The array has keys which are the tag names (excluding the @) and values
Chris@0 52 * that are arrays, each of which is an entry for the tag.
Chris@0 53 *
Chris@0 54 * In the case where the tag name is defined in {@see DocBlock::$vectors} the
Chris@0 55 * value within the tag-value array is an array in itself with keys as
Chris@0 56 * described by {@see DocBlock::$vectors}.
Chris@0 57 *
Chris@0 58 * @var array
Chris@0 59 */
Chris@0 60 public $tags;
Chris@0 61
Chris@0 62 /**
Chris@0 63 * The entire DocBlock comment that was parsed.
Chris@0 64 *
Chris@0 65 * @var string
Chris@0 66 */
Chris@0 67 public $comment;
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Docblock constructor.
Chris@0 71 *
Chris@0 72 * @param \Reflector $reflector
Chris@0 73 */
Chris@0 74 public function __construct(\Reflector $reflector)
Chris@0 75 {
Chris@0 76 $this->reflector = $reflector;
Chris@0 77 $this->setComment($reflector->getDocComment());
Chris@0 78 }
Chris@0 79
Chris@0 80 /**
Chris@0 81 * Set and parse the docblock comment.
Chris@0 82 *
Chris@0 83 * @param string $comment The docblock
Chris@0 84 */
Chris@0 85 protected function setComment($comment)
Chris@0 86 {
Chris@0 87 $this->desc = '';
Chris@0 88 $this->tags = array();
Chris@0 89 $this->comment = $comment;
Chris@0 90
Chris@0 91 $this->parseComment($comment);
Chris@0 92 }
Chris@0 93
Chris@0 94 /**
Chris@0 95 * Find the length of the docblock prefix.
Chris@0 96 *
Chris@0 97 * @param array $lines
Chris@0 98 *
Chris@0 99 * @return int Prefix length
Chris@0 100 */
Chris@0 101 protected static function prefixLength(array $lines)
Chris@0 102 {
Chris@0 103 // find only lines with interesting things
Chris@0 104 $lines = array_filter($lines, function ($line) {
Chris@0 105 return substr($line, strspn($line, "* \t\n\r\0\x0B"));
Chris@0 106 });
Chris@0 107
Chris@0 108 // if we sort the lines, we only have to compare two items
Chris@0 109 sort($lines);
Chris@0 110
Chris@0 111 $first = reset($lines);
Chris@0 112 $last = end($lines);
Chris@0 113
Chris@0 114 // find the longest common substring
Chris@0 115 $count = min(strlen($first), strlen($last));
Chris@0 116 for ($i = 0; $i < $count; $i++) {
Chris@0 117 if ($first[$i] !== $last[$i]) {
Chris@0 118 return $i;
Chris@0 119 }
Chris@0 120 }
Chris@0 121
Chris@0 122 return $count;
Chris@0 123 }
Chris@0 124
Chris@0 125 /**
Chris@0 126 * Parse the comment into the component parts and set the state of the object.
Chris@0 127 *
Chris@0 128 * @param string $comment The docblock
Chris@0 129 */
Chris@0 130 protected function parseComment($comment)
Chris@0 131 {
Chris@0 132 // Strip the opening and closing tags of the docblock
Chris@0 133 $comment = substr($comment, 3, -2);
Chris@0 134
Chris@0 135 // Split into arrays of lines
Chris@0 136 $comment = array_filter(preg_split('/\r?\n\r?/', $comment));
Chris@0 137
Chris@0 138 // Trim asterisks and whitespace from the beginning and whitespace from the end of lines
Chris@0 139 $prefixLength = self::prefixLength($comment);
Chris@0 140 $comment = array_map(function ($line) use ($prefixLength) {
Chris@0 141 return rtrim(substr($line, $prefixLength));
Chris@0 142 }, $comment);
Chris@0 143
Chris@0 144 // Group the lines together by @tags
Chris@0 145 $blocks = array();
Chris@0 146 $b = -1;
Chris@0 147 foreach ($comment as $line) {
Chris@0 148 if (self::isTagged($line)) {
Chris@0 149 $b++;
Chris@0 150 $blocks[] = array();
Chris@0 151 } elseif ($b === -1) {
Chris@0 152 $b = 0;
Chris@0 153 $blocks[] = array();
Chris@0 154 }
Chris@0 155 $blocks[$b][] = $line;
Chris@0 156 }
Chris@0 157
Chris@0 158 // Parse the blocks
Chris@0 159 foreach ($blocks as $block => $body) {
Chris@0 160 $body = trim(implode("\n", $body));
Chris@0 161
Chris@0 162 if ($block === 0 && !self::isTagged($body)) {
Chris@0 163 // This is the description block
Chris@0 164 $this->desc = $body;
Chris@0 165 } else {
Chris@0 166 // This block is tagged
Chris@0 167 $tag = substr(self::strTag($body), 1);
Chris@0 168 $body = ltrim(substr($body, strlen($tag) + 2));
Chris@0 169
Chris@0 170 if (isset(self::$vectors[$tag])) {
Chris@0 171 // The tagged block is a vector
Chris@0 172 $count = count(self::$vectors[$tag]);
Chris@0 173 if ($body) {
Chris@0 174 $parts = preg_split('/\s+/', $body, $count);
Chris@0 175 } else {
Chris@0 176 $parts = array();
Chris@0 177 }
Chris@0 178
Chris@0 179 // Default the trailing values
Chris@0 180 $parts = array_pad($parts, $count, null);
Chris@0 181
Chris@0 182 // Store as a mapped array
Chris@0 183 $this->tags[$tag][] = array_combine(self::$vectors[$tag], $parts);
Chris@0 184 } else {
Chris@0 185 // The tagged block is only text
Chris@0 186 $this->tags[$tag][] = $body;
Chris@0 187 }
Chris@0 188 }
Chris@0 189 }
Chris@0 190 }
Chris@0 191
Chris@0 192 /**
Chris@0 193 * Whether or not a docblock contains a given @tag.
Chris@0 194 *
Chris@0 195 * @param string $tag The name of the @tag to check for
Chris@0 196 *
Chris@0 197 * @return bool
Chris@0 198 */
Chris@0 199 public function hasTag($tag)
Chris@0 200 {
Chris@0 201 return is_array($this->tags) && array_key_exists($tag, $this->tags);
Chris@0 202 }
Chris@0 203
Chris@0 204 /**
Chris@0 205 * The value of a tag.
Chris@0 206 *
Chris@0 207 * @param string $tag
Chris@0 208 *
Chris@0 209 * @return array
Chris@0 210 */
Chris@0 211 public function tag($tag)
Chris@0 212 {
Chris@0 213 return $this->hasTag($tag) ? $this->tags[$tag] : null;
Chris@0 214 }
Chris@0 215
Chris@0 216 /**
Chris@0 217 * Whether or not a string begins with a @tag.
Chris@0 218 *
Chris@0 219 * @param string $str
Chris@0 220 *
Chris@0 221 * @return bool
Chris@0 222 */
Chris@0 223 public static function isTagged($str)
Chris@0 224 {
Chris@0 225 return isset($str[1]) && $str[0] === '@' && ctype_alpha($str[1]);
Chris@0 226 }
Chris@0 227
Chris@0 228 /**
Chris@0 229 * The tag at the beginning of a string.
Chris@0 230 *
Chris@0 231 * @param string $str
Chris@0 232 *
Chris@0 233 * @return string|null
Chris@0 234 */
Chris@0 235 public static function strTag($str)
Chris@0 236 {
Chris@0 237 if (preg_match('/^@[a-z0-9_]+/', $str, $matches)) {
Chris@0 238 return $matches[0];
Chris@0 239 }
Chris@0 240 }
Chris@0 241 }