1
0
mirror of synced 2025-01-31 04:21:44 +03:00

[2.0] DDC-78 - Added support for context specific information in Annotation Parser Syntax Error Exceptions.

This commit is contained in:
beberlei 2009-10-30 20:58:06 +00:00
parent a0c9e9db8d
commit a05bd5e20c
4 changed files with 118 additions and 11 deletions

View File

@ -99,7 +99,7 @@ class AnnotationReader
return $this->_cache->fetch($cacheKey);
}
$annotations = $this->_parser->parse($class->getDocComment());
$annotations = $this->_parser->parse($class->getDocComment(), "class ".$class->getName());
$this->_cache->save($cacheKey, $annotations, null);
return $annotations;
@ -133,8 +133,9 @@ class AnnotationReader
if ($this->_cache->contains($cacheKey)) {
return $this->_cache->fetch($cacheKey);
}
$annotations = $this->_parser->parse($property->getDocComment());
$context = "property ".$property->getDeclaringClass()->getName()."::\$".$property->getName();
$annotations = $this->_parser->parse($property->getDocComment(), $context);
$this->_cache->save($cacheKey, $annotations, null);
return $annotations;
@ -168,8 +169,9 @@ class AnnotationReader
if ($this->_cache->contains($cacheKey)) {
return $this->_cache->fetch($cacheKey);
}
$annotations = $this->_parser->parse($method->getDocComment());
$context = "method ".$method->getDeclaringClass()->getName()."::".$method->getName()."()";
$annotations = $this->_parser->parse($method->getDocComment(), $context);
$this->_cache->save($cacheKey, $annotations, null);
return $annotations;

View File

@ -72,6 +72,11 @@ class Parser
* @var array
*/
private $_namespaceAliases = array();
/**
* @var string
*/
private $_context = '';
/**
* Constructs a new AnnotationParser.
@ -107,11 +112,14 @@ class Parser
/**
* Parses the given docblock string for annotations.
*
* @param $docBlockString
* @param string $docBlockString
* @param string $context
* @return array Array of Annotations. If no annotations are found, an empty array is returned.
*/
public function parse($docBlockString)
public function parse($docBlockString, $context='')
{
$this->_context = $context;
// Strip out some known inline tags.
$input = str_replace(self::$_strippedInlineTags, '', $docBlockString);
@ -164,10 +172,15 @@ class Parser
$message = "Expected '{$expected}', got ";
if ($this->_lexer->lookahead === null) {
$message .= 'end of string.';
$message .= 'end of string';
} else {
$message .= "'{$token['value']}' at position {$token['position']}.";
$message .= "'{$token['value']}' at position {$token['position']}";
}
if(strlen($this->_context)) {
$message .= ' in '.$this->_context;
}
$message .= '.';
throw AnnotationException::syntaxError($message);
}

View File

@ -57,6 +57,57 @@ class AnnotationReaderTest extends \Doctrine\Tests\DoctrineTestCase
$classAnnot = $reader->getClassAnnotation($class, 'Doctrine\Tests\Common\Annotations\DummyAnnotation');
$this->assertEquals('hello', $classAnnot->dummyValue);
}
public function testClassSyntaxErrorContext()
{
$this->setExpectedException(
"Doctrine\Common\Annotations\AnnotationException",
"[Syntax Error] Expected '', got ')' at position 18 in class ".
"Doctrine\Tests\Common\Annotations\DummyClassSyntaxError."
);
$class = new \ReflectionClass('\Doctrine\Tests\Common\Annotations\DummyClassSyntaxError');
$reader = $this->createAnnotationReader();
$reader->getClassAnnotations($class);
}
public function testMethodSyntaxErrorContext()
{
$this->setExpectedException(
"Doctrine\Common\Annotations\AnnotationException",
"[Syntax Error] Expected '', got ')' at position 18 in ".
"method Doctrine\Tests\Common\Annotations\DummyClassMethodSyntaxError::foo()."
);
$class = new \ReflectionClass('\Doctrine\Tests\Common\Annotations\DummyClassMethodSyntaxError');
$method = $class->getMethod('foo');
$reader = $this->createAnnotationReader();
$reader->getMethodAnnotations($method);
}
public function testPropertySyntaxErrorContext()
{
$this->setExpectedException(
"Doctrine\Common\Annotations\AnnotationException",
"[Syntax Error] Expected '', got ')' at position 18 in ".
"property Doctrine\Tests\Common\Annotations\DummyClassPropertySyntaxError::\$foo."
);
$class = new \ReflectionClass('\Doctrine\Tests\Common\Annotations\DummyClassPropertySyntaxError');
$property = $class->getProperty('foo');
$reader = $this->createAnnotationReader();
$reader->getPropertyAnnotations($property);
}
public function createAnnotationReader()
{
$reader = new AnnotationReader(new \Doctrine\Common\Cache\ArrayCache);
$reader->setDefaultAnnotationNamespace('Doctrine\Tests\Common\Annotations\\');
return $reader;
}
}
/**
@ -107,4 +158,31 @@ class DummyJoinTable extends \Doctrine\Common\Annotations\Annotation {
public $name;
public $joinColumns;
public $inverseJoinColumns;
}
/**
* @DummyAnnotation(@)
*/
class DummyClassSyntaxError
{
}
class DummyClassMethodSyntaxError
{
/**
* @DummyAnnotation(@)
*/
public function foo()
{
}
}
class DummyClassPropertySyntaxError
{
/**
* @DummyAnnotation(@)
*/
public $foo;
}

View File

@ -10,8 +10,7 @@ class ParserTest extends \Doctrine\Tests\DoctrineTestCase
{
public function testBasicAnnotations()
{
$parser = new Parser;
$parser->setDefaultAnnotationNamespace('Doctrine\Tests\Common\Annotations\\');
$parser = $this->createTestParser();
// Marker annotation
$result = $parser->parse("@Name");
@ -108,6 +107,21 @@ DOCBLOCK;
$parser->setDefaultAnnotationNamespace('Doctrine\Tests\Common\Annotations\\');
return $parser;
}
/**
* @group DDC-78
*/
public function testSyntaxErrorWithContextDescription()
{
$this->setExpectedException(
'Doctrine\Common\Annotations\AnnotationException',
"[Syntax Error] Expected 'PlainValue', got ''' at position 10 ".
"in class \Doctrine\Tests\Common\Annotations\Name"
);
$parser = $this->createTestParser();
$parser->parse("@Name(foo='bar')", "class \Doctrine\Tests\Common\Annotations\Name");
}
}
class Name extends \Doctrine\Common\Annotations\Annotation {