Skip to content

Commit

Permalink
Turn ignore annotations into a whitelist for throwing an exception fo…
Browse files Browse the repository at this point in the history
…r importing yes/no. Add possibility for developers to add more annotations into the whitelist. Removed @ParseAnnotation as it is not necessary anymore. Did some performance enhancements
  • Loading branch information
beberlei committed May 24, 2011
1 parent a182cd9 commit 339cc92
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 72 deletions.
43 changes: 0 additions & 43 deletions lib/Doctrine/Common/Annotations/Annotation/ParseAnnotation.php

This file was deleted.

39 changes: 27 additions & 12 deletions lib/Doctrine/Common/Annotations/AnnotationReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,35 @@ final class AnnotationReader implements Reader
*/
private static $globalImports = array(
'ignoreannotation' => 'Doctrine\Common\Annotations\Annotation\IgnoreAnnotation',
'parseannotation' => 'Doctrine\Common\Annotations\Annotation\ParseAnnotation',
);

/**
* A list of globally ignored annotation names.
* A list with annotations that are not causing exceptions when not resolved to an annotation class.
*
* The names are case sensitive.
*
* @var array
*/
private static $globalIgnoredNames = array(
'access', 'author', 'copyright', 'deprecated', 'example', 'ignore',
'internal', 'link', 'see', 'since', 'tutorial', 'version', 'package',
'subpackage', 'name', 'global', 'param', 'return', 'staticvar',
'static', 'var', 'throws', 'inheritdoc', 'inheritDoc', 'license', 'todo',
'access'=> true, 'author'=> true, 'copyright'=> true, 'deprecated'=> true,
'example'=> true, 'ignore'=> true, 'internal'=> true, 'link'=> true, 'see'=> true,
'since'=> true, 'tutorial'=> true, 'version'=> true, 'package'=> true,
'subpackage'=> true, 'name'=> true, 'global'=> true, 'param'=> true,
'return'=> true, 'staticvar'=> true, 'category'=> true, 'staticVar'=> true,
'static'=> true, 'var'=> true, 'throws'=> true, 'inheritdoc'=> true,
'inheritDoc'=> true, 'license'=> true, 'todo'=> true, 'depcreated'=> true,

This comment has been minimized.

Copy link
@stof

stof May 24, 2011

Member

There is a typo in the name deprecated

This comment has been minimized.

Copy link
@beberlei

beberlei May 24, 2011

Author Member

narf.

'deprec'=> true, 'author'=> true,
);

/**
* Add a new annotation to the globally ignored annotation names with regard to exception handling.
*
* @param string $name
*/
static public function addGlobalIgnoredName($name)
{
self::$globalIgnoredNames[$name] = true;
}

/**
* Annotations Parser
Expand Down Expand Up @@ -256,7 +269,9 @@ public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
private function getIgnoredAnnotationNames(ReflectionClass $class)
{
if (isset($this->ignoredAnnotationNames[$name = $class->getName()])) {
return $this->ignoredAnnotationNames[$name];
return $this->ignoredAnnotationNames[$name]
? array_merge(self::$globalIgnoredNames, $this->ignoredAnnotationNames[$name])
: self::$globalIgnoredNames;
}
$this->collectParsingMetadata($class);

Expand All @@ -281,14 +296,14 @@ private function getImports(ReflectionClass $class)
private function collectParsingMetadata(ReflectionClass $class)
{
$imports = self::$globalImports;
$ignoredAnnotationNames = self::$globalIgnoredNames;
$ignoredAnnotationNames = array();

$annotations = $this->preParser->parse($class->getDocComment());
foreach ($annotations as $annotation) {
if ($annotation instanceof IgnoreAnnotation) {
$ignoredAnnotationNames = array_merge($ignoredAnnotationNames, $annotation->names);
} else if ($annotation instanceof ParseAnnotation) {
$ignoredAnnotationNames = array_diff($ignoredAnnotationNames, $annotation->names);
foreach ($annotation->names AS $annot) {
$ignoredAnnotationNames[$annot] = true;
}
}
}

Expand All @@ -298,6 +313,6 @@ private function collectParsingMetadata(ReflectionClass $class)
$this->phpParser->parseClass($class),
array('__NAMESPACE__' => $class->getNamespaceName())
);
$this->ignoredAnnotationNames[$name] = array_unique($ignoredAnnotationNames);
$this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;
}
}
9 changes: 2 additions & 7 deletions lib/Doctrine/Common/Annotations/DocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ final class DocParser
private $ignoreNotImportedAnnotations = false;

/**
* A list with annotations that are to be ignored during the parsing process.
* A list with annotations that are not causing exceptions when not resolved to an annotation class.
*
* The names must be the raw names as used in the class, not the fully qualified
* class names.
Expand Down Expand Up @@ -301,11 +301,6 @@ private function Annotation()
$name .= '\\'.$this->lexer->token['value'];
}

// check if name is supposed to be ignored
if (!$this->isNestedAnnotation && in_array($name, $this->ignoredAnnotationNames, true)) {
return false;
}

// only process names which are not fully qualified, yet
if ('\\' !== $name[0] && !$this->classExists($name)) {
$alias = (false === $pos = strpos($name, '\\'))? $name : substr($name, 0, $pos);
Expand All @@ -319,7 +314,7 @@ private function Annotation()
} elseif (isset($this->imports['__NAMESPACE__']) && $this->classExists($this->imports['__NAMESPACE__'].'\\'.$name)) {
$name = $this->imports['__NAMESPACE__'].'\\'.$name;
} else {
if ($this->ignoreNotImportedAnnotations) {
if ($this->ignoreNotImportedAnnotations || isset($this->ignoredAnnotationNames[$name])) {
return false;
}

Expand Down
10 changes: 0 additions & 10 deletions tests/Doctrine/Tests/Common/Annotations/AbstractReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,6 @@ public function testIgnoresAnnotationsNotPrefixedWithWhitespace()
$this->assertInstanceOf('Doctrine\Tests\Common\Annotations\Name', $annotation);
}

/**
* @expectedException Doctrine\Common\Annotations\AnnotationException
* @expectedExceptionMessage The annotation "@var" in property Doctrine\Tests\Common\Annotations\TestParseAnnotationClass::$field was never imported.
*/
public function testParseAnnotation()
{
$reader = $this->getReader();
$reader->getPropertyAnnotations(new \ReflectionProperty('Doctrine\Tests\Common\Annotations\TestParseAnnotationClass', 'field'));
}

abstract protected function getReader();
}

Expand Down

0 comments on commit 339cc92

Please sign in to comment.