/** * Parses the token substream and prepares namespace reflections from the file. * * @param \TokenReflection\Stream\StreamBase $tokenStream Token substream * @param \TokenReflection\IReflection $parent Parent reflection object * * @return \TokenReflection\ReflectionFile */ protected function parseStream(Stream $tokenStream, IReflection $parent = null) { $this->name = $tokenStream->getFileName(); if (1 >= $tokenStream->count()) { // No PHP content $this->docComment = new ReflectionAnnotation($this, null); return $this; } $docCommentPosition = null; if (!$tokenStream->is(T_OPEN_TAG)) { $this->namespaces[] = new ReflectionFileNamespace($tokenStream, $this->broker, $this); } else { $tokenStream->skipWhitespaces(); while (null !== ($type = $tokenStream->getType())) { switch ($type) { case T_DOC_COMMENT: if (null === $docCommentPosition) { $docCommentPosition = $tokenStream->key(); } case T_WHITESPACE: case T_COMMENT: break; case T_DECLARE: // Intentionally twice call of skipWhitespaces() $tokenStream->skipWhitespaces()->findMatchingBracket()->skipWhitespaces()->skipWhitespaces(); break; case T_NAMESPACE: $docCommentPosition = $docCommentPosition ?: -1; break 2; default: $docCommentPosition = $docCommentPosition ?: -1; $this->namespaces[] = new ReflectionFileNamespace($tokenStream, $this->broker, $this); break 2; } $tokenStream->skipWhitespaces(); } while (null !== ($type = $tokenStream->getType())) { if (T_NAMESPACE === $type) { $this->namespaces[] = new ReflectionFileNamespace($tokenStream, $this->broker, $this); } else { $tokenStream->skipWhitespaces(); } } } if (null !== $docCommentPosition && !empty($this->namespaces) && $docCommentPosition === $this->namespaces[0]->getStartPosition()) { $docCommentPosition = null; } $this->docComment = new ReflectionAnnotation($this, null !== $docCommentPosition ? $tokenStream->getTokenValue($docCommentPosition) : null); return $this; }
/** * Constructor. * * @param \TokenReflection\Stream\StreamBase $tokenStream Token substream * @param \TokenReflection\Broker $broker Reflection broker * @param \TokenReflection\IReflection $parent Parent reflection object * @throws \TokenReflection\Exception\ParseException If an empty token stream was provided */ public final function __construct(Stream $tokenStream, Broker $broker, IReflection $parent = null) { if (0 === $tokenStream->count()) { throw new Exception\ParseException($this, $tokenStream, 'Reflection token stream must not be empty.', Exception\ParseException::INVALID_ARGUMENT); } parent::__construct($tokenStream, $broker, $parent); }