Added the support of wildcard in ignore annotation (addGlobalIgnoredName) #156

Closed
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+82 −3
Split
@@ -70,13 +70,23 @@
'Target' => true, 'SuppressWarnings' => true,
);
+ /**
+ * Specify to ignore wildcard namespace (*) in annotation
+ *
+ * @var boolean
+ */
+ private static $ignoreWildcardNamespace = true;
@stof

stof Jun 22, 2012

Member

the naming is crappy. According to the implementation, it will be set to false to ignore them, and to true to disable the feature.

@Ocramius

Ocramius Jun 22, 2012

Owner

@stof skip it for now :)

@stof

stof Jun 22, 2012

Member

well, in fact, you should probably keep the name, but change the case where you use true and false

+
/**
* Add a new annotation to the globally ignored annotation names with regard to exception handling.
*
* @param string $name
*/
static public function addGlobalIgnoredName($name)
{
+ if (self::$ignoreWildcardNamespace && false !== strpos($name, '\\*')) {
+ self::$ignoreWildcardNamespace = false;
+ }
self::$globalIgnoredNames[$name] = true;
}
@@ -145,7 +155,8 @@ public function getClassAnnotations(ReflectionClass $class)
$this->parser->setTarget(Target::TARGET_CLASS);
$this->parser->setImports($this->getImports($class));
$this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
-
+ $this->parser->setIgnoreWildcardNamespace(self::$ignoreWildcardNamespace);
+
@stof

stof Jun 22, 2012

Member

please don't add trailing whitespaces

return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
}
@@ -114,6 +114,13 @@
*/
private $ignoredAnnotationNames = array();
+ /**
+ * Specify to ignore wildcard namespace (*) in annotation
+ *
+ * @var boolean
+ */
+ private $ignoreWildcardNamespace = true;
+
/**
* @var string
*/
@@ -223,6 +230,16 @@ public function setIgnoredAnnotationNames(array $names)
$this->ignoredAnnotationNames = $names;
}
+ /**
+ * Sets ignore wildcard namespace in annotation
+ *
+ * @param boolean $bool
+ */
+ public function setIgnoreWildcardNamespace ($bool)
+ {
+ $this->ignoreWildcardNamespace = (Boolean) $bool;
+ }
+
/**
* Sets ignore on not-imported annotations
*
@@ -588,7 +605,12 @@ private function Annotation()
if ($this->ignoreNotImportedAnnotations || isset($this->ignoredAnnotationNames[$name])) {
return false;
}
-
+ if (!$this->ignoreWildcardNamespace) {
+ $namespace = explode('\\', $name, 2);
+ if (isset($this->ignoredAnnotationNames[$namespace[0].'\\*'])) {
+ return false;
+ }
+ }
throw AnnotationException::semanticalError(sprintf('The annotation "@%s" in %s was never imported. Did you maybe forget to add a "use" statement for this annotation?', $name, $this->context));
}
}
@@ -325,7 +325,7 @@ public function testInvalidAnnotationButIgnored()
$this->assertCount(0, $reader->getMethodAnnotations($class->getMethod('bar')));
$this->assertCount(0, $reader->getPropertyAnnotations($class->getProperty('foo')));
}
-
+
abstract protected function getReader();
}
@@ -10,4 +10,16 @@ protected function getReader()
{
return new AnnotationReader();
}
+
+ public function testSupportWildcardIgnoredAnnotation()
+ {
+ $reader = $this->getReader();
+ AnnotationReader::addGlobalIgnoredName('Foo\\*');
+
+ $class = new \ReflectionClass('Doctrine\Tests\Common\Annotations\Fixtures\IgnoreAnnotationWithWildcard');
+ $annots = $reader->getClassAnnotations($class);
+
+ $this->assertEquals('foo', $annots[0]->bar);
+ $this->assertEquals('bar', $annots[1]->foo);
+ }
}
@@ -0,0 +1,34 @@
+<?php
+
+namespace MyCompany\Annotations {
+ /**
+ * @Annotation
+ */
+ class Foo
+ {
+ public $bar;
+ }
+
+ /**
+ * @Annotation
+ */
+ class Bar
+ {
+ public $foo;
+ }
+}
+
+namespace Doctrine\Tests\Common\Annotations\Fixtures {
+
+
+ /**
+ * @MyCompany\Annotations\Foo(bar="foo")
+ * @MyCompany\Annotations\Bar(foo="bar")
+ * @Foo\Bar
+ * @Foo\Bar\Baz
+ */
+ class IgnoreAnnotationWithWildcard
+ {
+
+ }
+}