[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/vendor/zendframework/zend-code/src/Scanner/ -> DocBlockScanner.php (source)

   1  <?php
   2  /**
   3   * Zend Framework (http://framework.zend.com/)
   4   *
   5   * @link      http://github.com/zendframework/zf2 for the canonical source repository
   6   * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
   7   * @license   http://framework.zend.com/license/new-bsd New BSD License
   8   */
   9  
  10  namespace Zend\Code\Scanner;
  11  
  12  use Zend\Code\Annotation\AnnotationManager;
  13  use Zend\Code\NameInformation;
  14  
  15  use function array_pop;
  16  use function array_push;
  17  use function current;
  18  use function end;
  19  use function key;
  20  use function next;
  21  use function preg_match;
  22  use function reset;
  23  use function strlen;
  24  use function strpos;
  25  use function substr;
  26  use function trim;
  27  
  28  class DocBlockScanner implements ScannerInterface
  29  {
  30      /**
  31       * @var bool
  32       */
  33      protected $isScanned = false;
  34  
  35      /**
  36       * @var string
  37       */
  38      protected $docComment;
  39  
  40      /**
  41       * @var NameInformation
  42       */
  43      protected $nameInformation;
  44  
  45      /**
  46       * @var AnnotationManager
  47       */
  48      protected $annotationManager;
  49  
  50      /**
  51       * @var string
  52       */
  53      protected $shortDescription;
  54  
  55      /**
  56       * @var string
  57       */
  58      protected $longDescription = '';
  59  
  60      /**
  61       * @var array
  62       */
  63      protected $tags = [];
  64  
  65      /**
  66       * @var array
  67       */
  68      protected $annotations = [];
  69  
  70      /**
  71       * @param  string $docComment
  72       * @param null|NameInformation $nameInformation
  73       */
  74      public function __construct($docComment, NameInformation $nameInformation = null)
  75      {
  76          $this->docComment      = $docComment;
  77          $this->nameInformation = $nameInformation;
  78      }
  79  
  80      /**
  81       * @return string
  82       */
  83      public function getShortDescription()
  84      {
  85          $this->scan();
  86  
  87          return $this->shortDescription;
  88      }
  89  
  90      /**
  91       * @return string
  92       */
  93      public function getLongDescription()
  94      {
  95          $this->scan();
  96  
  97          return $this->longDescription;
  98      }
  99  
 100      /**
 101       * @return array
 102       */
 103      public function getTags()
 104      {
 105          $this->scan();
 106  
 107          return $this->tags;
 108      }
 109  
 110      /**
 111       * @return array
 112       */
 113      public function getAnnotations()
 114      {
 115          $this->scan();
 116  
 117          return $this->annotations;
 118      }
 119  
 120      /**
 121       * @return void
 122       */
 123      protected function scan()
 124      {
 125          if ($this->isScanned) {
 126              return;
 127          }
 128  
 129          $mode = 1;
 130  
 131          $tokens   = $this->tokenize();
 132          $tagIndex = null;
 133          reset($tokens);
 134  
 135          SCANNER_TOP:
 136          $token = current($tokens);
 137  
 138          switch ($token[0]) {
 139              case 'DOCBLOCK_NEWLINE':
 140                  if ($this->shortDescription != '' && $tagIndex === null) {
 141                      $mode = 2;
 142                  } else {
 143                      $this->longDescription .= $token[1];
 144                  }
 145                  goto SCANNER_CONTINUE;
 146                  //goto no break needed
 147  
 148              case 'DOCBLOCK_WHITESPACE':
 149              case 'DOCBLOCK_TEXT':
 150                  if ($tagIndex !== null) {
 151                      $this->tags[$tagIndex]['value'] .= $this->tags[$tagIndex]['value'] == ''
 152                          ? $token[1]
 153                          : ' ' . $token[1];
 154                      goto SCANNER_CONTINUE;
 155                  } elseif ($mode <= 2) {
 156                      if ($mode == 1) {
 157                          $this->shortDescription .= $token[1];
 158                      } else {
 159                          $this->longDescription .= $token[1];
 160                      }
 161                      goto SCANNER_CONTINUE;
 162                  }
 163                  //gotos no break needed
 164              case 'DOCBLOCK_TAG':
 165                  array_push($this->tags, [
 166                      'name'  => $token[1],
 167                      'value' => '',
 168                  ]);
 169                  end($this->tags);
 170                  $tagIndex = key($this->tags);
 171                  $mode     = 3;
 172                  goto SCANNER_CONTINUE;
 173                  //goto no break needed
 174  
 175              case 'DOCBLOCK_COMMENTEND':
 176                  goto SCANNER_END;
 177          }
 178  
 179          SCANNER_CONTINUE:
 180          if (next($tokens) === false) {
 181              goto SCANNER_END;
 182          }
 183          goto SCANNER_TOP;
 184  
 185          SCANNER_END:
 186  
 187          $this->shortDescription = trim($this->shortDescription);
 188          $this->longDescription  = trim($this->longDescription);
 189          $this->isScanned        = true;
 190      }
 191  
 192      /**
 193       * @return array
 194       */
 195      protected function tokenize()
 196      {
 197          static $CONTEXT_INSIDE_DOCBLOCK = 0x01;
 198          static $CONTEXT_INSIDE_ASTERISK = 0x02;
 199  
 200          $context     = 0x00;
 201          $stream      = $this->docComment;
 202          $streamIndex = null;
 203          $tokens      = [];
 204          $tokenIndex  = null;
 205          $currentChar = null;
 206          $currentWord = null;
 207          $currentLine = null;
 208  
 209          $MACRO_STREAM_ADVANCE_CHAR = function ($positionsForward = 1) use (
 210              &$stream,
 211              &$streamIndex,
 212              &$currentChar,
 213              &$currentWord,
 214              &$currentLine
 215          ) {
 216              $positionsForward = $positionsForward > 0 ? $positionsForward : 1;
 217              $streamIndex      = $streamIndex === null ? 0 : $streamIndex + $positionsForward;
 218              if (! isset($stream[$streamIndex])) {
 219                  $currentChar = false;
 220  
 221                  return false;
 222              }
 223              $currentChar = $stream[$streamIndex];
 224              $matches     = [];
 225              $currentLine = preg_match('#(.*?)\r?\n#', $stream, $matches, null, $streamIndex) === 1
 226                  ? $matches[1]
 227                  : substr($stream, $streamIndex);
 228              if ($currentChar === ' ') {
 229                  $currentWord = preg_match('#( +)#', $currentLine, $matches) === 1 ? $matches[1] : $currentLine;
 230              } else {
 231                  $currentWord = ($matches = strpos($currentLine, ' ')) !== false
 232                      ? substr($currentLine, 0, $matches)
 233                      : $currentLine;
 234              }
 235  
 236              return $currentChar;
 237          };
 238          $MACRO_STREAM_ADVANCE_WORD       = function () use (&$currentWord, &$MACRO_STREAM_ADVANCE_CHAR) {
 239              return $MACRO_STREAM_ADVANCE_CHAR(strlen($currentWord));
 240          };
 241          $MACRO_STREAM_ADVANCE_LINE       = function () use (&$currentLine, &$MACRO_STREAM_ADVANCE_CHAR) {
 242              return $MACRO_STREAM_ADVANCE_CHAR(strlen($currentLine));
 243          };
 244          $MACRO_TOKEN_ADVANCE             = function () use (&$tokenIndex, &$tokens) {
 245              $tokenIndex          = $tokenIndex === null ? 0 : $tokenIndex + 1;
 246              $tokens[$tokenIndex] = ['DOCBLOCK_UNKNOWN', ''];
 247          };
 248          $MACRO_TOKEN_SET_TYPE            = function ($type) use (&$tokenIndex, &$tokens) {
 249              $tokens[$tokenIndex][0] = $type;
 250          };
 251          $MACRO_TOKEN_APPEND_CHAR         = function () use (&$currentChar, &$tokens, &$tokenIndex) {
 252              $tokens[$tokenIndex][1] .= $currentChar;
 253          };
 254          $MACRO_TOKEN_APPEND_WORD         = function () use (&$currentWord, &$tokens, &$tokenIndex) {
 255              $tokens[$tokenIndex][1] .= $currentWord;
 256          };
 257          $MACRO_TOKEN_APPEND_WORD_PARTIAL = function ($length) use (&$currentWord, &$tokens, &$tokenIndex) {
 258              $tokens[$tokenIndex][1] .= substr($currentWord, 0, $length);
 259          };
 260          $MACRO_TOKEN_APPEND_LINE         = function () use (&$currentLine, &$tokens, &$tokenIndex) {
 261              $tokens[$tokenIndex][1] .= $currentLine;
 262          };
 263  
 264          $MACRO_STREAM_ADVANCE_CHAR();
 265          $MACRO_TOKEN_ADVANCE();
 266  
 267          TOKENIZER_TOP:
 268  
 269          if ($context === 0x00 && $currentChar === '/' && $currentWord === '/**') {
 270              $MACRO_TOKEN_SET_TYPE('DOCBLOCK_COMMENTSTART');
 271              $MACRO_TOKEN_APPEND_WORD();
 272              $MACRO_TOKEN_ADVANCE();
 273              $context |= $CONTEXT_INSIDE_DOCBLOCK;
 274              $context |= $CONTEXT_INSIDE_ASTERISK;
 275              if ($MACRO_STREAM_ADVANCE_WORD() === false) {
 276                  goto TOKENIZER_END;
 277              }
 278              goto TOKENIZER_TOP;
 279          }
 280  
 281          if ($context & $CONTEXT_INSIDE_DOCBLOCK && $currentWord === '*/') {
 282              $MACRO_TOKEN_SET_TYPE('DOCBLOCK_COMMENTEND');
 283              $MACRO_TOKEN_APPEND_WORD();
 284              $MACRO_TOKEN_ADVANCE();
 285              $context &= ~$CONTEXT_INSIDE_DOCBLOCK;
 286              if ($MACRO_STREAM_ADVANCE_WORD() === false) {
 287                  goto TOKENIZER_END;
 288              }
 289              goto TOKENIZER_TOP;
 290          }
 291  
 292          if ($currentChar === ' ' || $currentChar === "\t") {
 293              $MACRO_TOKEN_SET_TYPE(
 294                  $context & $CONTEXT_INSIDE_ASTERISK
 295                  ? 'DOCBLOCK_WHITESPACE'
 296                  : 'DOCBLOCK_WHITESPACE_INDENT'
 297              );
 298              $MACRO_TOKEN_APPEND_WORD();
 299              $MACRO_TOKEN_ADVANCE();
 300              if ($MACRO_STREAM_ADVANCE_WORD() === false) {
 301                  goto TOKENIZER_END;
 302              }
 303              goto TOKENIZER_TOP;
 304          }
 305  
 306          if ($currentChar === '*') {
 307              if (($context & $CONTEXT_INSIDE_DOCBLOCK) && ($context & $CONTEXT_INSIDE_ASTERISK)) {
 308                  $MACRO_TOKEN_SET_TYPE('DOCBLOCK_TEXT');
 309              } else {
 310                  $MACRO_TOKEN_SET_TYPE('DOCBLOCK_ASTERISK');
 311                  $context |= $CONTEXT_INSIDE_ASTERISK;
 312              }
 313              $MACRO_TOKEN_APPEND_CHAR();
 314              $MACRO_TOKEN_ADVANCE();
 315              if ($MACRO_STREAM_ADVANCE_CHAR() === false) {
 316                  goto TOKENIZER_END;
 317              }
 318              goto TOKENIZER_TOP;
 319          }
 320  
 321          if ($currentChar === '@') {
 322              $MACRO_TOKEN_SET_TYPE('DOCBLOCK_TAG');
 323              $MACRO_TOKEN_APPEND_WORD();
 324              $MACRO_TOKEN_ADVANCE();
 325              if ($MACRO_STREAM_ADVANCE_WORD() === false) {
 326                  goto TOKENIZER_END;
 327              }
 328              goto TOKENIZER_TOP;
 329          }
 330  
 331          if ($currentChar === "\n") {
 332              $MACRO_TOKEN_SET_TYPE('DOCBLOCK_NEWLINE');
 333              $MACRO_TOKEN_APPEND_CHAR();
 334              $MACRO_TOKEN_ADVANCE();
 335              $context &= ~$CONTEXT_INSIDE_ASTERISK;
 336              if ($MACRO_STREAM_ADVANCE_CHAR() === false) {
 337                  goto TOKENIZER_END;
 338              }
 339              goto TOKENIZER_TOP;
 340          }
 341  
 342          $MACRO_TOKEN_SET_TYPE('DOCBLOCK_TEXT');
 343          $MACRO_TOKEN_APPEND_LINE();
 344          $MACRO_TOKEN_ADVANCE();
 345          if ($MACRO_STREAM_ADVANCE_LINE() === false) {
 346              goto TOKENIZER_END;
 347          }
 348          goto TOKENIZER_TOP;
 349  
 350          TOKENIZER_END:
 351  
 352          array_pop($tokens);
 353  
 354          return $tokens;
 355      }
 356  }


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1