/** * Returns the value of a constant. * * @return mixed The value of the constant. * @throws ClassConstantNotFoundException if the constant is a class constant and if this class cannot be found. * @throws UndefinedConstantExceptions if the constant isn't defined. */ private function getConstant() { /* @var \com\mohiva\common\lang\AnnotationToken $token */ if ($this->stream->isNext(AnnotationLexer::T_NS_SEPARATOR)) { $token = $this->next(AnnotationLexer::T_NS_SEPARATOR); $constant = $token->getValue(); } else { $constant = ''; } $token = $this->next(AnnotationLexer::T_NAME); $constant .= $token->getValue(); while ($this->stream->isNext(AnnotationLexer::T_NS_SEPARATOR)) { $token = $this->next(AnnotationLexer::T_NS_SEPARATOR); $constant .= $token->getValue(); $token = $this->next(AnnotationLexer::T_NAME); $constant .= $token->getValue(); } // Get a class constant $colon = $this->stream->isNext(AnnotationLexer::T_DOUBLE_COLON); if ($colon && $constant == 'self') { $constant = $this->context->getClass(); $token = $this->next(AnnotationLexer::T_DOUBLE_COLON); $constant .= $token->getValue(); $token = $this->next(AnnotationLexer::T_NAME); $constant .= $token->getValue(); } else { if ($colon) { $constant = $this->getFullyQualifiedName($constant); $token = $this->next(AnnotationLexer::T_DOUBLE_COLON); $constant .= $token->getValue(); $token = $this->next(AnnotationLexer::T_NAME); $constant .= $token->getValue(); } } try { return constant($constant); } catch (ClassNotFoundException $e) { $snippet = preg_replace('/[\\r\\n\\t]*/', '', substr($this->stream->getSource(), $token->getOffset(), 50)); $message = "The class for constant `{$constant}` cannot be found; "; $message .= "called in DocBlock for: {$this->context->getLocation()}; "; $message .= "found at offset `{$token->getOffset()}` near `{$snippet}`"; throw new ClassNotFoundException($message, null, $e); } catch (Exception $e) { $snippet = preg_replace('/[\\r\\n\\t]*/', '', substr($this->stream->getSource(), $token->getOffset(), 20)); $message = "The constant `{$constant}` is not defined; "; $message .= "called in DocBlock for: {$this->context->getLocation()}; "; $message .= "found at offset `{$token->getOffset()}` near `{$snippet}`"; throw new UndefinedConstantException($message, null, $e); } }
/** * Test if a specified token is the next token. * * @param \com\mohiva\common\parser\TokenStream $stream The token stream to use for this test. * @dataProvider tokenStreamProvider */ public function testIsNext(TokenStream $stream) { // user.name.split(" ").join("-") $this->assertTrue($stream->isNext(self::T_POINT)); $stream->next(); $this->assertTrue($stream->isNext(self::T_NAME)); $stream->next(); $this->assertTrue($stream->isNext(self::T_POINT)); $stream->next(); $this->assertTrue($stream->isNext(self::T_NAME)); $stream->next(); $this->assertTrue($stream->isNext(self::T_OPEN_PARENTHESIS)); $stream->next(); $this->assertTrue($stream->isNext(self::T_VALUE)); $this->assertTrue($stream->isNext(self::T_CLOSE_PARENTHESIS, 2)); $this->assertTrue($stream->isNext(self::T_POINT, 3)); $this->assertTrue($stream->isNext(self::T_NAME, 4)); $this->assertTrue($stream->isNext(self::T_OPEN_PARENTHESIS, 5)); $this->assertTrue($stream->isNext(self::T_VALUE, 6)); $this->assertTrue($stream->isNext(self::T_CLOSE_PARENTHESIS, 7)); $this->assertFalse($stream->isNext(self::T_NAME, 8)); }