From ffae7e2f4266ddf6fc893f724f97e4a3a70dc1e4 Mon Sep 17 00:00:00 2001 From: Alexander Lisachenko Date: Tue, 10 Apr 2018 15:45:34 +0300 Subject: [PATCH 1/6] Adjust source code to remove dependency on Annotation classes --- src/Reflection/ClassReflection.php | 35 ------------- src/Reflection/MethodReflection.php | 33 ------------ src/Reflection/PropertyReflection.php | 34 ------------- src/Scanner/CachingFileScanner.php | 18 ++----- src/Scanner/ClassScanner.php | 16 ------ src/Scanner/ConstantScanner.php | 14 ------ src/Scanner/DocBlockScanner.php | 21 -------- src/Scanner/FileScanner.php | 6 +-- src/Scanner/MethodScanner.php | 14 ------ src/Scanner/PropertyScanner.php | 14 ------ src/Scanner/TokenArrayScanner.php | 20 +------- test/Reflection/ClassReflectionTest.php | 24 --------- test/Reflection/MethodReflectionTest.php | 25 ---------- test/Reflection/PropertyReflectionTest.php | 50 ------------------- .../Reflection/TestAsset/SampleAnnotation.php | 4 +- test/Scanner/CachingFileScannerTest.php | 10 ++-- test/Scanner/ClassScannerTest.php | 31 ------------ 17 files changed, 12 insertions(+), 357 deletions(-) diff --git a/src/Reflection/ClassReflection.php b/src/Reflection/ClassReflection.php index 10b0c19f..86d353a2 100644 --- a/src/Reflection/ClassReflection.php +++ b/src/Reflection/ClassReflection.php @@ -10,9 +10,6 @@ namespace Zend\Code\Reflection; use ReflectionClass; -use Zend\Code\Annotation\AnnotationCollection; -use Zend\Code\Annotation\AnnotationManager; -use Zend\Code\Scanner\AnnotationScanner; use Zend\Code\Scanner\FileScanner; use function array_shift; @@ -25,10 +22,6 @@ class ClassReflection extends ReflectionClass implements ReflectionInterface { - /** - * @var AnnotationScanner - */ - protected $annotations; /** * @var DocBlockReflection @@ -68,34 +61,6 @@ public function getDocBlock() return $this->docBlock; } - /** - * @param AnnotationManager $annotationManager - * @return AnnotationCollection - */ - public function getAnnotations(AnnotationManager $annotationManager) - { - $docComment = $this->getDocComment(); - - if ($docComment == '') { - return false; - } - - if ($this->annotations) { - return $this->annotations; - } - - $fileScanner = $this->createFileScanner($this->getFileName()); - $nameInformation = $fileScanner->getClassNameInformation($this->getName()); - - if (! $nameInformation) { - return false; - } - - $this->annotations = new AnnotationScanner($annotationManager, $docComment, $nameInformation); - - return $this->annotations; - } - /** * Return the start line of the class * diff --git a/src/Reflection/MethodReflection.php b/src/Reflection/MethodReflection.php index d7b75a6c..d897bcd7 100644 --- a/src/Reflection/MethodReflection.php +++ b/src/Reflection/MethodReflection.php @@ -10,8 +10,6 @@ namespace Zend\Code\Reflection; use ReflectionMethod as PhpReflectionMethod; -use Zend\Code\Annotation\AnnotationManager; -use Zend\Code\Scanner\AnnotationScanner; use Zend\Code\Scanner\CachingFileScanner; use function array_shift; @@ -41,11 +39,6 @@ class MethodReflection extends PhpReflectionMethod implements ReflectionInterfac */ const PROTOTYPE_AS_STRING = 'prototype_as_string'; - /** - * @var AnnotationScanner - */ - protected $annotations; - /** * Retrieve method DocBlock reflection * @@ -62,32 +55,6 @@ public function getDocBlock() return $instance; } - /** - * @param AnnotationManager $annotationManager - * @return AnnotationScanner - */ - public function getAnnotations(AnnotationManager $annotationManager) - { - if (($docComment = $this->getDocComment()) == '') { - return false; - } - - if ($this->annotations) { - return $this->annotations; - } - - $cachingFileScanner = $this->createFileScanner($this->getFileName()); - $nameInformation = $cachingFileScanner->getClassNameInformation($this->getDeclaringClass()->getName()); - - if (! $nameInformation) { - return false; - } - - $this->annotations = new AnnotationScanner($annotationManager, $docComment, $nameInformation); - - return $this->annotations; - } - /** * Get start line (position) of method * diff --git a/src/Reflection/PropertyReflection.php b/src/Reflection/PropertyReflection.php index c277f8e1..e748031d 100644 --- a/src/Reflection/PropertyReflection.php +++ b/src/Reflection/PropertyReflection.php @@ -10,8 +10,6 @@ namespace Zend\Code\Reflection; use ReflectionProperty as PhpReflectionProperty; -use Zend\Code\Annotation\AnnotationManager; -use Zend\Code\Scanner\AnnotationScanner; use Zend\Code\Scanner\CachingFileScanner; /** @@ -19,11 +17,6 @@ */ class PropertyReflection extends PhpReflectionProperty implements ReflectionInterface { - /** - * @var AnnotationScanner - */ - protected $annotations; - /** * Get declaring class reflection object * @@ -62,33 +55,6 @@ public function getDocBlock() return $docBlockReflection; } - /** - * @param AnnotationManager $annotationManager - * @return AnnotationScanner - */ - public function getAnnotations(AnnotationManager $annotationManager) - { - if (null !== $this->annotations) { - return $this->annotations; - } - - if (($docComment = $this->getDocComment()) == '') { - return false; - } - - $class = $this->getDeclaringClass(); - $cachingFileScanner = $this->createFileScanner($class->getFileName()); - $nameInformation = $cachingFileScanner->getClassNameInformation($class->getName()); - - if (! $nameInformation) { - return false; - } - - $this->annotations = new AnnotationScanner($annotationManager, $docComment, $nameInformation); - - return $this->annotations; - } - /** * @return string */ diff --git a/src/Scanner/CachingFileScanner.php b/src/Scanner/CachingFileScanner.php index 2a9a5524..5ee7ca75 100644 --- a/src/Scanner/CachingFileScanner.php +++ b/src/Scanner/CachingFileScanner.php @@ -9,7 +9,6 @@ namespace Zend\Code\Scanner; -use Zend\Code\Annotation\AnnotationManager; use Zend\Code\Exception; use Zend\Code\NameInformation; @@ -33,10 +32,9 @@ class CachingFileScanner extends FileScanner /** * @param string $file - * @param AnnotationManager $annotationManager * @throws Exception\InvalidArgumentException */ - public function __construct($file, AnnotationManager $annotationManager = null) + public function __construct($file) { if (! file_exists($file)) { throw new Exception\InvalidArgumentException(sprintf( @@ -47,14 +45,12 @@ public function __construct($file, AnnotationManager $annotationManager = null) $file = realpath($file); - $cacheId = md5($file) . '/' . (isset($annotationManager) - ? spl_object_hash($annotationManager) - : 'no-annotation'); + $cacheId = md5($file); if (isset(static::$cache[$cacheId])) { $this->fileScanner = static::$cache[$cacheId]; } else { - $this->fileScanner = new FileScanner($file, $annotationManager); + $this->fileScanner = new FileScanner($file); static::$cache[$cacheId] = $this->fileScanner; } } @@ -67,14 +63,6 @@ public static function clearCache() static::$cache = []; } - /** - * @return AnnotationManager - */ - public function getAnnotationManager() - { - return $this->fileScanner->getAnnotationManager(); - } - /** * @return array|null|string */ diff --git a/src/Scanner/ClassScanner.php b/src/Scanner/ClassScanner.php index ba734e30..cf90e8be 100644 --- a/src/Scanner/ClassScanner.php +++ b/src/Scanner/ClassScanner.php @@ -10,7 +10,6 @@ namespace Zend\Code\Scanner; use ReflectionClass; -use Zend\Code\Annotation; use Zend\Code\Exception; use Zend\Code\NameInformation; @@ -140,21 +139,6 @@ public function __construct(array $classTokens, NameInformation $nameInformation $this->nameInformation = $nameInformation; } - /** - * Get annotations - * - * @param Annotation\AnnotationManager $annotationManager - * @return Annotation\AnnotationCollection - */ - public function getAnnotations(Annotation\AnnotationManager $annotationManager) - { - if (($docComment = $this->getDocComment()) == '') { - return false; - } - - return new AnnotationScanner($annotationManager, $docComment, $this->nameInformation); - } - /** * Return documentation comment * diff --git a/src/Scanner/ConstantScanner.php b/src/Scanner/ConstantScanner.php index 1be19b53..14bfaa54 100644 --- a/src/Scanner/ConstantScanner.php +++ b/src/Scanner/ConstantScanner.php @@ -9,7 +9,6 @@ namespace Zend\Code\Scanner; -use Zend\Code\Annotation; use Zend\Code\Exception; use Zend\Code\NameInformation; @@ -131,19 +130,6 @@ public function getDocComment() return $this->docComment; } - /** - * @param Annotation\AnnotationManager $annotationManager - * @return AnnotationScanner - */ - public function getAnnotations(Annotation\AnnotationManager $annotationManager) - { - if (($docComment = $this->getDocComment()) == '') { - return false; - } - - return new AnnotationScanner($annotationManager, $docComment, $this->nameInformation); - } - /** * @return string */ diff --git a/src/Scanner/DocBlockScanner.php b/src/Scanner/DocBlockScanner.php index 80b9ac22..30f8611b 100644 --- a/src/Scanner/DocBlockScanner.php +++ b/src/Scanner/DocBlockScanner.php @@ -9,7 +9,6 @@ namespace Zend\Code\Scanner; -use Zend\Code\Annotation\AnnotationManager; use Zend\Code\NameInformation; use function array_pop; @@ -42,11 +41,6 @@ class DocBlockScanner implements ScannerInterface */ protected $nameInformation; - /** - * @var AnnotationManager - */ - protected $annotationManager; - /** * @var string */ @@ -62,11 +56,6 @@ class DocBlockScanner implements ScannerInterface */ protected $tags = []; - /** - * @var array - */ - protected $annotations = []; - /** * @param string $docComment * @param null|NameInformation $nameInformation @@ -107,16 +96,6 @@ public function getTags() return $this->tags; } - /** - * @return array - */ - public function getAnnotations() - { - $this->scan(); - - return $this->annotations; - } - /** * @return void */ diff --git a/src/Scanner/FileScanner.php b/src/Scanner/FileScanner.php index 66cf8e8e..bcdc30fa 100644 --- a/src/Scanner/FileScanner.php +++ b/src/Scanner/FileScanner.php @@ -9,7 +9,6 @@ namespace Zend\Code\Scanner; -use Zend\Code\Annotation\AnnotationManager; use Zend\Code\Exception; use function file_exists; @@ -26,10 +25,9 @@ class FileScanner extends TokenArrayScanner implements ScannerInterface /** * @param string $file - * @param null|AnnotationManager $annotationManager * @throws Exception\InvalidArgumentException */ - public function __construct($file, AnnotationManager $annotationManager = null) + public function __construct($file) { $this->file = $file; if (! file_exists($file)) { @@ -38,7 +36,7 @@ public function __construct($file, AnnotationManager $annotationManager = null) $file )); } - parent::__construct(token_get_all(file_get_contents($file)), $annotationManager); + parent::__construct(token_get_all(file_get_contents($file))); } /** diff --git a/src/Scanner/MethodScanner.php b/src/Scanner/MethodScanner.php index 05c67412..68c26b36 100644 --- a/src/Scanner/MethodScanner.php +++ b/src/Scanner/MethodScanner.php @@ -9,7 +9,6 @@ namespace Zend\Code\Scanner; -use Zend\Code\Annotation\AnnotationManager; use Zend\Code\Exception; use Zend\Code\NameInformation; @@ -187,19 +186,6 @@ public function getDocComment() return $this->docComment; } - /** - * @param AnnotationManager $annotationManager - * @return AnnotationScanner - */ - public function getAnnotations(AnnotationManager $annotationManager) - { - if (($docComment = $this->getDocComment()) == '') { - return false; - } - - return new AnnotationScanner($annotationManager, $docComment, $this->nameInformation); - } - /** * @return bool */ diff --git a/src/Scanner/PropertyScanner.php b/src/Scanner/PropertyScanner.php index b11dd9b3..56b1a0e1 100644 --- a/src/Scanner/PropertyScanner.php +++ b/src/Scanner/PropertyScanner.php @@ -9,7 +9,6 @@ namespace Zend\Code\Scanner; -use Zend\Code\Annotation; use Zend\Code\Exception; use Zend\Code\NameInformation; @@ -209,19 +208,6 @@ public function getDocComment() return $this->docComment; } - /** - * @param Annotation\AnnotationManager $annotationManager - * @return AnnotationScanner - */ - public function getAnnotations(Annotation\AnnotationManager $annotationManager) - { - if (($docComment = $this->getDocComment()) == '') { - return false; - } - - return new AnnotationScanner($annotationManager, $docComment, $this->nameInformation); - } - /** * @return string */ diff --git a/src/Scanner/TokenArrayScanner.php b/src/Scanner/TokenArrayScanner.php index 9fa199bc..372048b3 100644 --- a/src/Scanner/TokenArrayScanner.php +++ b/src/Scanner/TokenArrayScanner.php @@ -9,7 +9,6 @@ namespace Zend\Code\Scanner; -use Zend\Code\Annotation\AnnotationManager; use Zend\Code\Exception; use Zend\Code\NameInformation; @@ -47,27 +46,12 @@ class TokenArrayScanner implements ScannerInterface */ protected $infos = []; - /** - * @var AnnotationManager - */ - protected $annotationManager; - /** * @param null|array $tokens - * @param null|AnnotationManager $annotationManager - */ - public function __construct($tokens, AnnotationManager $annotationManager = null) - { - $this->tokens = $tokens; - $this->annotationManager = $annotationManager; - } - - /** - * @return AnnotationManager */ - public function getAnnotationManager() + public function __construct($tokens) { - return $this->annotationManager; + $this->tokens = $tokens; } /** diff --git a/test/Reflection/ClassReflectionTest.php b/test/Reflection/ClassReflectionTest.php index 0f084487..8e1d3da1 100644 --- a/test/Reflection/ClassReflectionTest.php +++ b/test/Reflection/ClassReflectionTest.php @@ -10,7 +10,6 @@ namespace ZendTest\Code\Reflection; use PHPUnit\Framework\TestCase; -use Zend\Code\Annotation\AnnotationManager; use Zend\Code\Reflection\ClassReflection; use Zend\Code\Reflection\MethodReflection; use Zend\Code\Reflection\PropertyReflection; @@ -156,29 +155,6 @@ public function testGetDeclaringFileReturnsFilename() self::assertContains('TestSampleClass2.php', $reflectionClass->getDeclaringFile()->getFileName()); } - public function testGetAnnotationsWithNoNameInformations() - { - $reflectionClass = new InjectableClassReflection( - // TestSampleClass5 has the annotations required to get to the - // right point in the getAnnotations method. - TestAsset\TestSampleClass5::class - ); - - $annotationManager = new AnnotationManager(); - - $fileScanner = $this->getMockBuilder(FileScanner::class) - ->disableOriginalConstructor() - ->getMock(); - - $reflectionClass->setFileScanner($fileScanner); - - $fileScanner->expects($this->any()) - ->method('getClassNameInformation') - ->will($this->returnValue(false)); - - self::assertFalse($reflectionClass->getAnnotations($annotationManager)); - } - public function testGetContentsReturnsEmptyContentsOnEvaldCode() { $className = uniqid('ClassReflectionTestGenerated'); diff --git a/test/Reflection/MethodReflectionTest.php b/test/Reflection/MethodReflectionTest.php index 3b47032a..ef5309c7 100644 --- a/test/Reflection/MethodReflectionTest.php +++ b/test/Reflection/MethodReflectionTest.php @@ -10,7 +10,6 @@ namespace ZendTest\Code\Reflection; use PHPUnit\Framework\TestCase; -use Zend\Code\Annotation\AnnotationManager; use Zend\Code\Reflection\ClassReflection; use Zend\Code\Reflection\MethodReflection; use Zend\Code\Reflection\ParameterReflection; @@ -295,30 +294,6 @@ public function testGetPrototypeMethod() ); } - public function testGetAnnotationsWithNoNameInformations() - { - $reflectionMethod = new InjectableMethodReflection( - // TestSampleClass5 has the annotations required to get to the - // right point in the getAnnotations method. - TestAsset\TestSampleClass5::class, - 'doSomething' - ); - - $annotationManager = new AnnotationManager(); - - $fileScanner = $this->getMockBuilder(CachingFileScanner::class) - ->disableOriginalConstructor() - ->getMock(); - - $reflectionMethod->setFileScanner($fileScanner); - - $fileScanner->expects($this->any()) - ->method('getClassNameInformation') - ->will($this->returnValue(false)); - - self::assertFalse($reflectionMethod->getAnnotations($annotationManager)); - } - /** * @group 5062 */ diff --git a/test/Reflection/PropertyReflectionTest.php b/test/Reflection/PropertyReflectionTest.php index 912b8477..4554fa1b 100644 --- a/test/Reflection/PropertyReflectionTest.php +++ b/test/Reflection/PropertyReflectionTest.php @@ -10,9 +10,6 @@ namespace ZendTest\Code\Reflection; use PHPUnit\Framework\TestCase; -use Zend\Code\Annotation\AnnotationCollection; -use Zend\Code\Annotation\AnnotationManager; -use Zend\Code\Annotation\Parser\GenericAnnotationParser; use Zend\Code\Reflection\ClassReflection; use Zend\Code\Reflection\PropertyReflection; use Zend\Code\Scanner\CachingFileScanner; @@ -32,51 +29,4 @@ public function testDeclaringClassReturn() self::assertInstanceOf(ClassReflection::class, $property->getDeclaringClass()); self::assertEquals(TestAsset\TestSampleClass2::class, $property->getDeclaringClass()->getName()); } - - public function testAnnotationScanningIsPossible() - { - $manager = new AnnotationManager(); - $parser = new GenericAnnotationParser(); - $parser->registerAnnotation(new TestAsset\SampleAnnotation()); - $manager->attach($parser); - - $property = new PropertyReflection(TestAsset\TestSampleClass2::class, '_prop2'); - $annotations = $property->getAnnotations($manager); - self::assertInstanceOf(AnnotationCollection::class, $annotations); - self::assertTrue($annotations->hasAnnotation(TestAsset\SampleAnnotation::class)); - $found = false; - foreach ($annotations as $key => $annotation) { - if (! $annotation instanceof TestAsset\SampleAnnotation) { - continue; - } - self::assertEquals(get_class($annotation) . ': {"foo":"bar"}', $annotation->content); - $found = true; - break; - } - self::assertTrue($found); - } - - public function testGetAnnotationsWithNoNameInformations() - { - $reflectionProperty = new InjectablePropertyReflection( - // TestSampleClass5 has the annotations required to get to the - // right point in the getAnnotations method. - TestAsset\TestSampleClass2::class, - '_prop2' - ); - - $annotationManager = new AnnotationManager(); - - $fileScanner = $this->getMockBuilder(CachingFileScanner::class) - ->disableOriginalConstructor() - ->getMock(); - - $reflectionProperty->setFileScanner($fileScanner); - - $fileScanner->expects($this->any()) - ->method('getClassNameInformation') - ->will($this->returnValue(false)); - - self::assertFalse($reflectionProperty->getAnnotations($annotationManager)); - } } diff --git a/test/Reflection/TestAsset/SampleAnnotation.php b/test/Reflection/TestAsset/SampleAnnotation.php index 5fb93a7f..4d214ff1 100644 --- a/test/Reflection/TestAsset/SampleAnnotation.php +++ b/test/Reflection/TestAsset/SampleAnnotation.php @@ -9,9 +9,7 @@ namespace ZendTest\Code\Reflection\TestAsset; -use Zend\Code\Annotation\AnnotationInterface; - -class SampleAnnotation implements AnnotationInterface +class SampleAnnotation { public $content; diff --git a/test/Scanner/CachingFileScannerTest.php b/test/Scanner/CachingFileScannerTest.php index 33abc9bc..a5f26844 100644 --- a/test/Scanner/CachingFileScannerTest.php +++ b/test/Scanner/CachingFileScannerTest.php @@ -10,7 +10,6 @@ namespace ZendTest\Code\Scanner; use PHPUnit\Framework\TestCase; -use Zend\Code\Annotation\AnnotationManager; use Zend\Code\Scanner\CachingFileScanner; use ZendTest\Code\TestAsset\BarClass; @@ -23,7 +22,7 @@ protected function setUp() CachingFileScanner::clearCache(); } - public function testCachingFileScannerWillUseSameInternalFileScannerWithMatchingFileNameAnAnnotationManagerObject() + public function testCachingFileScannerWillUseSameInternalFileScannerWithMatchingFileName() { CachingFileScanner::clearCache(); @@ -42,16 +41,15 @@ public function testCachingFileScannerWillUseSameInternalFileScannerWithMatching self::assertEquals(2, $this->getCacheCount($cfs3)); self::assertDifferentInternalFileScanner($cfs2, $cfs3); - $annoManager = new AnnotationManager(); - $cfs4 = new CachingFileScanner(__DIR__ . '/../TestAsset/FooClass.php', $annoManager); + $cfs4 = new CachingFileScanner(__DIR__ . '/../TestAsset/FooClass.php'); self::assertEquals(3, $this->getCacheCount($cfs4)); self::assertDifferentInternalFileScanner($cfs3, $cfs4); - $cfs5 = new CachingFileScanner(__DIR__ . '/../TestAsset/FooClass.php', $annoManager); + $cfs5 = new CachingFileScanner(__DIR__ . '/../TestAsset/FooClass.php'); self::assertEquals(3, $this->getCacheCount($cfs5)); self::assertSameInternalFileScanner($cfs4, $cfs5); - $cfs6 = new CachingFileScanner(__DIR__ . '/../TestAsset/BarClass.php', $annoManager); + $cfs6 = new CachingFileScanner(__DIR__ . '/../TestAsset/BarClass.php'); self::assertEquals(4, $this->getCacheCount($cfs6)); self::assertDifferentInternalFileScanner($cfs5, $cfs6); } diff --git a/test/Scanner/ClassScannerTest.php b/test/Scanner/ClassScannerTest.php index bb255c0d..372083d6 100644 --- a/test/Scanner/ClassScannerTest.php +++ b/test/Scanner/ClassScannerTest.php @@ -10,16 +10,12 @@ namespace ZendTest\Code\Scanner; use PHPUnit\Framework\TestCase; -use Zend\Code\Annotation; use Zend\Code\Exception\RuntimeException; use Zend\Code\Scanner\ConstantScanner; use Zend\Code\Scanner\FileScanner; use Zend\Code\Scanner\MethodScanner; use Zend\Code\Scanner\PropertyScanner; use Zend\Stdlib\ErrorHandler; -use ZendTest\Code\Annotation\TestAsset\Bar; -use ZendTest\Code\Annotation\TestAsset\EntityWithAnnotations; -use ZendTest\Code\Annotation\TestAsset\Foo; use ZendTest\Code\TestAsset\BarClass; use ZendTest\Code\TestAsset\BarTrait; use ZendTest\Code\TestAsset\BazTrait; @@ -36,19 +32,6 @@ class ClassScannerTest extends TestCase { - protected $manager; - - public function setUp() - { - $this->manager = new Annotation\AnnotationManager(); - - $genericParser = new Annotation\Parser\GenericAnnotationParser(); - $genericParser->registerAnnotation(Foo::class); - $genericParser->registerAnnotation(Bar::class); - - $this->manager->attach($genericParser); - } - public function testClassScannerHasClassInformation() { $file = new FileScanner(__DIR__ . '/../TestAsset/FooClass.php'); @@ -181,20 +164,6 @@ public function testClassScannerCanReturnLineNumbers() self::assertEquals(42, $class->getLineEnd()); } - public function testClassScannerCanScanAnnotations() - { - $file = new FileScanner(__DIR__ . '/../Annotation/TestAsset/EntityWithAnnotations.php'); - $class = $file->getClass(EntityWithAnnotations::class); - $annotations = $class->getAnnotations($this->manager); - - self::assertTrue($annotations->hasAnnotation(Foo::class)); - self::assertTrue($annotations->hasAnnotation(Bar::class)); - - self::assertEquals('first', $annotations[0]->content); - self::assertEquals('second', $annotations[1]->content); - self::assertEquals('third', $annotations[2]->content); - } - /** * @group trait1 */ From 240236f638803dcbd8afb0a7f857dce0d59ad233 Mon Sep 17 00:00:00 2001 From: Alexander Lisachenko Date: Tue, 10 Apr 2018 15:42:50 +0300 Subject: [PATCH 2/6] Remove all Annotation-related classes from the library --- src/Annotation/AnnotationCollection.php | 34 -- src/Annotation/AnnotationInterface.php | 21 - src/Annotation/AnnotationManager.php | 112 ------ .../Parser/DoctrineAnnotationParser.php | 162 -------- .../Parser/GenericAnnotationParser.php | 237 ----------- src/Annotation/Parser/ParserInterface.php | 39 -- src/Scanner/AnnotationScanner.php | 379 ------------------ test/Annotation/AnnotationManagerTest.php | 63 --- .../DoctrineAnnotationParserTest.php | 100 ----- .../GenericAnnotationParserTest.php | 127 ------ test/Annotation/TestAsset/Bar.php | 22 - .../TestAsset/DoctrineAnnotation.php | 23 -- .../TestAsset/EntityWithAnnotations.php | 27 -- .../TestAsset/EntityWithMixedAnnotations.php | 20 - test/Annotation/TestAsset/Foo.php | 22 - test/Scanner/AnnotationScannerTest.php | 59 --- test/Scanner/TestAsset/Annotation/Bar.php | 22 - test/Scanner/TestAsset/Annotation/Foo.php | 28 -- 18 files changed, 1497 deletions(-) delete mode 100644 src/Annotation/AnnotationCollection.php delete mode 100644 src/Annotation/AnnotationInterface.php delete mode 100644 src/Annotation/AnnotationManager.php delete mode 100644 src/Annotation/Parser/DoctrineAnnotationParser.php delete mode 100644 src/Annotation/Parser/GenericAnnotationParser.php delete mode 100644 src/Annotation/Parser/ParserInterface.php delete mode 100644 src/Scanner/AnnotationScanner.php delete mode 100644 test/Annotation/AnnotationManagerTest.php delete mode 100644 test/Annotation/DoctrineAnnotationParserTest.php delete mode 100644 test/Annotation/GenericAnnotationParserTest.php delete mode 100644 test/Annotation/TestAsset/Bar.php delete mode 100644 test/Annotation/TestAsset/DoctrineAnnotation.php delete mode 100644 test/Annotation/TestAsset/EntityWithAnnotations.php delete mode 100644 test/Annotation/TestAsset/EntityWithMixedAnnotations.php delete mode 100644 test/Annotation/TestAsset/Foo.php delete mode 100644 test/Scanner/AnnotationScannerTest.php delete mode 100644 test/Scanner/TestAsset/Annotation/Bar.php delete mode 100644 test/Scanner/TestAsset/Annotation/Foo.php diff --git a/src/Annotation/AnnotationCollection.php b/src/Annotation/AnnotationCollection.php deleted file mode 100644 index 74e2b050..00000000 --- a/src/Annotation/AnnotationCollection.php +++ /dev/null @@ -1,34 +0,0 @@ -setIdentifiers([ - __CLASS__, - get_class($this), - ]); - $this->events = $events; - - return $this; - } - - /** - * Retrieve event manager - * - * Lazy loads an instance if none registered. - * - * @return EventManagerInterface - */ - public function getEventManager() - { - if (null === $this->events) { - $this->setEventManager(new EventManager()); - } - - return $this->events; - } - - /** - * Attach a parser to listen to the createAnnotation event - * - * @param ParserInterface $parser - * @return AnnotationManager - */ - public function attach(ParserInterface $parser) - { - $this->getEventManager() - ->attach(self::EVENT_CREATE_ANNOTATION, [$parser, 'onCreateAnnotation']); - - return $this; - } - - /** - * Create Annotation - * - * @param string[] $annotationData - * @return false|\stdClass - */ - public function createAnnotation(array $annotationData) - { - $event = new Event(); - $event->setName(self::EVENT_CREATE_ANNOTATION); - $event->setTarget($this); - $event->setParams([ - 'class' => $annotationData[0], - 'content' => $annotationData[1], - 'raw' => $annotationData[2], - ]); - - $eventManager = $this->getEventManager(); - $results = $eventManager->triggerEventUntil(function ($r) { - return is_object($r); - }, $event); - - $annotation = $results->last(); - - return is_object($annotation) ? $annotation : false; - } -} diff --git a/src/Annotation/Parser/DoctrineAnnotationParser.php b/src/Annotation/Parser/DoctrineAnnotationParser.php deleted file mode 100644 index 64f7ffa5..00000000 --- a/src/Annotation/Parser/DoctrineAnnotationParser.php +++ /dev/null @@ -1,162 +0,0 @@ -docParser = $docParser; - return $this; - } - - /** - * Retrieve the DocParser instance - * - * If none is registered, lazy-loads a new instance. - * - * @return DocParser - */ - public function getDocParser() - { - if (! $this->docParser instanceof DocParser) { - $this->setDocParser(new DocParser()); - } - - return $this->docParser; - } - - /** - * Handle annotation creation - * - * @param EventInterface $e - * @return false|\stdClass - */ - public function onCreateAnnotation(EventInterface $e) - { - $annotationClass = $e->getParam('class', false); - if (! $annotationClass) { - return false; - } - - if (! isset($this->allowedAnnotations[$annotationClass])) { - return false; - } - - $annotationString = $e->getParam('raw', false); - if (! $annotationString) { - return false; - } - - // Annotation classes provided by the AnnotationScanner are already - // resolved to fully-qualified class names. Adding the global namespace - // prefix allows the Doctrine annotation parser to locate the annotation - // class correctly. - $annotationString = preg_replace('/^(@)/', '$1\\', $annotationString); - - $parser = $this->getDocParser(); - $annotations = $parser->parse($annotationString); - if (empty($annotations)) { - return false; - } - - $annotation = array_shift($annotations); - if (! is_object($annotation)) { - return false; - } - - return $annotation; - } - - /** - * Specify an allowed annotation class - * - * @param string $annotation - * @return DoctrineAnnotationParser - */ - public function registerAnnotation($annotation) - { - $this->allowedAnnotations[$annotation] = true; - return $this; - } - - /** - * Set many allowed annotations at once - * - * @param array|Traversable $annotations Array or traversable object of - * annotation class names - * @throws Exception\InvalidArgumentException - * @return DoctrineAnnotationParser - */ - public function registerAnnotations($annotations) - { - if (! is_array($annotations) && ! $annotations instanceof Traversable) { - throw new Exception\InvalidArgumentException(sprintf( - '%s: expects an array or Traversable; received "%s"', - __METHOD__, - is_object($annotations) ? get_class($annotations) : gettype($annotations) - )); - } - - foreach ($annotations as $annotation) { - $this->allowedAnnotations[$annotation] = true; - } - - return $this; - } -} diff --git a/src/Annotation/Parser/GenericAnnotationParser.php b/src/Annotation/Parser/GenericAnnotationParser.php deleted file mode 100644 index 6515e775..00000000 --- a/src/Annotation/Parser/GenericAnnotationParser.php +++ /dev/null @@ -1,237 +0,0 @@ -getParam('class', false); - if (! $class || ! $this->hasAnnotation($class)) { - return false; - } - - $content = $e->getParam('content', ''); - $content = trim($content, '()'); - - if ($this->hasAlias($class)) { - $class = $this->resolveAlias($class); - } - - $index = array_search($class, $this->annotationNames); - $annotation = $this->annotations[$index]; - - $newAnnotation = clone $annotation; - if ($content) { - $newAnnotation->initialize($content); - } - - return $newAnnotation; - } - - /** - * Register annotations - * - * @param string|AnnotationInterface $annotation String class name of an - * AnnotationInterface implementation, or actual instance - * @return void - * @throws Exception\InvalidArgumentException - */ - public function registerAnnotation($annotation) - { - $class = false; - if (is_string($annotation) && class_exists($annotation)) { - $class = $annotation; - $annotation = new $annotation(); - } - - if (! $annotation instanceof AnnotationInterface) { - throw new Exception\InvalidArgumentException(sprintf( - '%s: expects an instance of %s\AnnotationInterface; received "%s"', - __METHOD__, - __NAMESPACE__, - is_object($annotation) ? get_class($annotation) : gettype($annotation) - )); - } - - $class = $class ?: get_class($annotation); - - if (in_array($class, $this->annotationNames)) { - throw new Exception\InvalidArgumentException(sprintf( - 'An annotation for this class %s already exists', - $class - )); - } - - $this->annotations[] = $annotation; - $this->annotationNames[] = $class; - } - - /** - * Register many annotations at once - * - * @param array|Traversable $annotations - * @throws Exception\InvalidArgumentException - * @return GenericAnnotationParser - */ - public function registerAnnotations($annotations) - { - if (! is_array($annotations) && ! $annotations instanceof Traversable) { - throw new Exception\InvalidArgumentException(sprintf( - '%s: expects an array or Traversable; received "%s"', - __METHOD__, - is_object($annotations) ? get_class($annotations) : gettype($annotations) - )); - } - - foreach ($annotations as $annotation) { - $this->registerAnnotation($annotation); - } - - return $this; - } - - /** - * Checks if the manager has annotations for a class - * - * @param string $class - * @return bool - */ - public function hasAnnotation($class) - { - if (in_array($class, $this->annotationNames)) { - return true; - } - - if ($this->hasAlias($class)) { - return true; - } - - return false; - } - - /** - * Alias an annotation name - * - * @param string $alias - * @param string $class May be either a registered annotation name or another alias - * @throws Exception\InvalidArgumentException - * @return GenericAnnotationParser - */ - public function setAlias($alias, $class) - { - if (! in_array($class, $this->annotationNames) && ! $this->hasAlias($class)) { - throw new Exception\InvalidArgumentException(sprintf( - '%s: Cannot alias "%s" to "%s", as class "%s" is not currently a registered annotation or alias', - __METHOD__, - $alias, - $class, - $class - )); - } - - $alias = $this->normalizeAlias($alias); - $this->aliases[$alias] = $class; - - return $this; - } - - /** - * Normalize an alias name - * - * @param string $alias - * @return string - */ - protected function normalizeAlias($alias) - { - return strtolower(str_replace(['-', '_', ' ', '\\', '/'], '', $alias)); - } - - /** - * Do we have an alias by the provided name? - * - * @param string $alias - * @return bool - */ - protected function hasAlias($alias) - { - $alias = $this->normalizeAlias($alias); - - return isset($this->aliases[$alias]); - } - - /** - * Resolve an alias to a class name - * - * @param string $alias - * @return string - */ - protected function resolveAlias($alias) - { - do { - $normalized = $this->normalizeAlias($alias); - $class = $this->aliases[$normalized]; - } while ($this->hasAlias($class)); - - return $class; - } -} diff --git a/src/Annotation/Parser/ParserInterface.php b/src/Annotation/Parser/ParserInterface.php deleted file mode 100644 index f27ea471..00000000 --- a/src/Annotation/Parser/ParserInterface.php +++ /dev/null @@ -1,39 +0,0 @@ -annotationManager = $annotationManager; - $this->docComment = $docComment; - $this->nameInformation = $nameInformation; - $this->scan($this->tokenize()); - } - - /** - * @param NameInformation $nameInformation - */ - public function setNameInformation(NameInformation $nameInformation) - { - $this->nameInformation = $nameInformation; - } - - /** - * @param array $tokens - */ - protected function scan(array $tokens) - { - $annotations = []; - $annotationIndex = -1; - $contentEnd = false; - - reset($tokens); - - SCANNER_TOP: - $token = current($tokens); - - switch ($token[0]) { - case 'ANNOTATION_CLASS': - $contentEnd = false; - $annotationIndex++; - $class = substr($token[1], 1); - $class = $this->nameInformation->resolveName($class); - $annotations[$annotationIndex] = [$class, null]; - goto SCANNER_CONTINUE; - // goto no break needed - - case 'ANNOTATION_CONTENT_START': - $annotations[$annotationIndex][1] = ''; - // fall-through - - case 'ANNOTATION_CONTENT_END': - case 'ANNOTATION_CONTENT': - case 'ANNOTATION_WHITESPACE': - case 'ANNOTATION_NEWLINE': - if (! $contentEnd - && isset($annotations[$annotationIndex]) - && is_string($annotations[$annotationIndex][1]) - ) { - $annotations[$annotationIndex][1] .= $token[1]; - } - - if ($token[0] === 'ANNOTATION_CONTENT_END') { - $contentEnd = true; - } - - goto SCANNER_CONTINUE; - // goto no break needed - } - - SCANNER_CONTINUE: - if (next($tokens) === false) { - goto SCANNER_END; - } - goto SCANNER_TOP; - - SCANNER_END: - - foreach ($annotations as $annotation) { - $annotation[] = '@' . $annotation[0] . $annotation[1]; - $annotationObject = $this->annotationManager->createAnnotation($annotation); - if ($annotationObject) { - $this->append($annotationObject); - } - } - } - - /** - * @return array - */ - protected function tokenize() - { - static $CONTEXT_DOCBLOCK = 0x01; - static $CONTEXT_ASTERISK = 0x02; - static $CONTEXT_CLASS = 0x04; - static $CONTEXT_CONTENT = 0x08; - - $context = 0x00; - $stream = $this->docComment; - $streamIndex = null; - $tokens = []; - $tokenIndex = null; - $currentChar = null; - $currentWord = null; - $currentLine = null; - - $annotationParentCount = 0; - - $MACRO_STREAM_ADVANCE_CHAR = function ($positionsForward = 1) use ( - &$stream, - &$streamIndex, - &$currentChar, - &$currentWord, - &$currentLine - ) { - $positionsForward = $positionsForward > 0 ? $positionsForward : 1; - $streamIndex = $streamIndex === null ? 0 : $streamIndex + $positionsForward; - if (! isset($stream[$streamIndex])) { - $currentChar = false; - - return false; - } - $currentChar = $stream[$streamIndex]; - $matches = []; - $currentLine = preg_match('#(.*?)(?:\n|\r\n?)#', $stream, $matches, null, $streamIndex) === 1 - ? $matches[1] - : substr($stream, $streamIndex); - if ($currentChar === ' ') { - $currentWord = preg_match('#( +)#', $currentLine, $matches) === 1 ? $matches[1] : $currentLine; - } else { - $currentWord = ($matches = strpos($currentLine, ' ')) !== false - ? substr($currentLine, 0, $matches) - : $currentLine; - } - - return $currentChar; - }; - $MACRO_STREAM_ADVANCE_WORD = function () use (&$currentWord, &$MACRO_STREAM_ADVANCE_CHAR) { - return $MACRO_STREAM_ADVANCE_CHAR(strlen($currentWord)); - }; - $MACRO_STREAM_ADVANCE_LINE = function () use (&$currentLine, &$MACRO_STREAM_ADVANCE_CHAR) { - return $MACRO_STREAM_ADVANCE_CHAR(strlen($currentLine)); - }; - $MACRO_TOKEN_ADVANCE = function () use (&$tokenIndex, &$tokens) { - $tokenIndex = $tokenIndex === null ? 0 : $tokenIndex + 1; - $tokens[$tokenIndex] = ['ANNOTATION_UNKNOWN', '']; - }; - $MACRO_TOKEN_SET_TYPE = function ($type) use (&$tokenIndex, &$tokens) { - $tokens[$tokenIndex][0] = $type; - }; - $MACRO_TOKEN_APPEND_CHAR = function () use (&$currentChar, &$tokens, &$tokenIndex) { - $tokens[$tokenIndex][1] .= $currentChar; - }; - $MACRO_TOKEN_APPEND_WORD = function () use (&$currentWord, &$tokens, &$tokenIndex) { - $tokens[$tokenIndex][1] .= $currentWord; - }; - $MACRO_TOKEN_APPEND_LINE = function () use (&$currentLine, &$tokens, &$tokenIndex) { - $tokens[$tokenIndex][1] .= $currentLine; - }; - $MACRO_HAS_CONTEXT = function ($which) use (&$context) { - return ($context & $which) === $which; - }; - - $MACRO_STREAM_ADVANCE_CHAR(); - $MACRO_TOKEN_ADVANCE(); - - TOKENIZER_TOP: - - if ($context === 0x00 && $currentChar === '/' && $currentWord === '/**') { - $MACRO_TOKEN_SET_TYPE('ANNOTATION_COMMENTSTART'); - $MACRO_TOKEN_APPEND_WORD(); - $MACRO_TOKEN_ADVANCE(); - $context |= $CONTEXT_DOCBLOCK; - $context |= $CONTEXT_ASTERISK; - if ($MACRO_STREAM_ADVANCE_WORD() === false) { - goto TOKENIZER_END; - } - goto TOKENIZER_TOP; - } - - if ($MACRO_HAS_CONTEXT($CONTEXT_CLASS)) { - if (in_array($currentChar, [' ', '(', "\n", "\r"])) { - $context &= ~$CONTEXT_CLASS; - $MACRO_TOKEN_ADVANCE(); - } else { - $MACRO_TOKEN_APPEND_CHAR(); - if ($MACRO_STREAM_ADVANCE_CHAR() === false) { - goto TOKENIZER_END; - } - goto TOKENIZER_TOP; - } - } - - // Since we don't know what line endings are used in the file, we check for all scenarios. If we find a - // carriage return (\r), we check the next character for a line feed (\n). If so we consume it and act as - // if the carriage return was a line feed. - $lineEnded = $currentChar === "\n"; - if ($currentChar === "\r") { - $lineEnded = true; - - $nextChar = $MACRO_STREAM_ADVANCE_CHAR(); - if ($nextChar !== "\n") { - $streamIndex--; - } - - $currentChar = "\n"; - } - - if ($lineEnded) { - $MACRO_TOKEN_SET_TYPE('ANNOTATION_NEWLINE'); - $MACRO_TOKEN_APPEND_CHAR(); - $MACRO_TOKEN_ADVANCE(); - $context &= ~$CONTEXT_ASTERISK; - $context &= ~$CONTEXT_CLASS; - if ($MACRO_STREAM_ADVANCE_CHAR() === false) { - goto TOKENIZER_END; - } - goto TOKENIZER_TOP; - } - - if ($currentChar === ' ') { - $MACRO_TOKEN_SET_TYPE( - $MACRO_HAS_CONTEXT($CONTEXT_ASTERISK) - ? 'ANNOTATION_WHITESPACE' - : 'ANNOTATION_WHITESPACE_INDENT' - ); - $MACRO_TOKEN_APPEND_WORD(); - $MACRO_TOKEN_ADVANCE(); - if ($MACRO_STREAM_ADVANCE_WORD() === false) { - goto TOKENIZER_END; - } - goto TOKENIZER_TOP; - } - - if ($MACRO_HAS_CONTEXT($CONTEXT_CONTENT) && $MACRO_HAS_CONTEXT($CONTEXT_ASTERISK)) { - $MACRO_TOKEN_SET_TYPE('ANNOTATION_CONTENT'); - $annotationParentCount += substr_count($currentWord, '('); - $annotationParentCount -= substr_count($currentWord, ')'); - - if ($annotationParentCount === 0) { - $context &= ~$CONTEXT_CONTENT; - $MACRO_TOKEN_SET_TYPE('ANNOTATION_CONTENT_END'); - } - $MACRO_TOKEN_APPEND_WORD(); - $MACRO_TOKEN_ADVANCE(); - if ($MACRO_STREAM_ADVANCE_WORD() === false) { - goto TOKENIZER_END; - } - goto TOKENIZER_TOP; - } - - if ($currentChar === '(' && $tokens[$tokenIndex - 1][0] === 'ANNOTATION_CLASS') { - $context |= $CONTEXT_CONTENT; - $annotationParentCount = 1; - $MACRO_TOKEN_SET_TYPE('ANNOTATION_CONTENT_START'); - $MACRO_TOKEN_APPEND_CHAR(); - $MACRO_TOKEN_ADVANCE(); - if ($MACRO_STREAM_ADVANCE_CHAR() === false) { - goto TOKENIZER_END; - } - goto TOKENIZER_TOP; - } - - if ($MACRO_HAS_CONTEXT($CONTEXT_DOCBLOCK) && $currentWord === '*/') { - $MACRO_TOKEN_SET_TYPE('ANNOTATION_COMMENTEND'); - $MACRO_TOKEN_APPEND_WORD(); - $MACRO_TOKEN_ADVANCE(); - $context &= ~$CONTEXT_DOCBLOCK; - if ($MACRO_STREAM_ADVANCE_WORD() === false) { - goto TOKENIZER_END; - } - goto TOKENIZER_TOP; - } - - if ($currentChar === '*') { - if ($MACRO_HAS_CONTEXT($CONTEXT_DOCBLOCK) && ($MACRO_HAS_CONTEXT($CONTEXT_ASTERISK))) { - $MACRO_TOKEN_SET_TYPE('ANNOTATION_IGNORE'); - } else { - $MACRO_TOKEN_SET_TYPE('ANNOTATION_ASTERISK'); - $context |= $CONTEXT_ASTERISK; - } - $MACRO_TOKEN_APPEND_CHAR(); - $MACRO_TOKEN_ADVANCE(); - if ($MACRO_STREAM_ADVANCE_CHAR() === false) { - goto TOKENIZER_END; - } - goto TOKENIZER_TOP; - } - - if ($currentChar === '@') { - $MACRO_TOKEN_SET_TYPE('ANNOTATION_CLASS'); - $context |= $CONTEXT_CLASS; - $MACRO_TOKEN_APPEND_CHAR(); - if ($MACRO_STREAM_ADVANCE_CHAR() === false) { - goto TOKENIZER_END; - } - goto TOKENIZER_TOP; - } - - TOKENIZER_CONTINUE: - - if ($context && $CONTEXT_CONTENT) { - $MACRO_TOKEN_APPEND_CHAR(); - if ($MACRO_STREAM_ADVANCE_CHAR() === false) { - goto TOKENIZER_END; - } - } else { - $MACRO_TOKEN_SET_TYPE('ANNOTATION_IGNORE'); - $MACRO_TOKEN_APPEND_LINE(); - $MACRO_TOKEN_ADVANCE(); - if ($MACRO_STREAM_ADVANCE_LINE() === false) { - goto TOKENIZER_END; - } - } - goto TOKENIZER_TOP; - - TOKENIZER_END: - - array_pop($tokens); - - return $tokens; - } -} diff --git a/test/Annotation/AnnotationManagerTest.php b/test/Annotation/AnnotationManagerTest.php deleted file mode 100644 index ae2f5937..00000000 --- a/test/Annotation/AnnotationManagerTest.php +++ /dev/null @@ -1,63 +0,0 @@ -markTestSkipped( - 'Enable TESTS_ZEND_CODE_ANNOTATION_DOCTRINE_SUPPORT to test doctrine annotation parsing' - ); - } - - $this->manager = new Annotation\AnnotationManager(); - } - - public function testAllowsMultipleParsingStrategies() - { - $genericParser = new Annotation\Parser\GenericAnnotationParser(); - $genericParser->registerAnnotation(TestAsset\Foo::class); - $doctrineParser = new Annotation\Parser\DoctrineAnnotationParser(); - $doctrineParser->registerAnnotation(TestAsset\DoctrineAnnotation::class); - - $this->manager->attach($genericParser); - $this->manager->attach($doctrineParser); - - $reflection = new Reflection\ClassReflection(TestAsset\EntityWithMixedAnnotations::class); - $prop = $reflection->getProperty('test'); - $annotations = $prop->getAnnotations($this->manager); - - self::assertTrue($annotations->hasAnnotation(TestAsset\Foo::class)); - self::assertTrue($annotations->hasAnnotation(TestAsset\DoctrineAnnotation::class)); - self::assertFalse($annotations->hasAnnotation(TestAsset\Bar::class)); - - foreach ($annotations as $annotation) { - switch (get_class($annotation)) { - case TestAsset\Foo::class: - self::assertEquals('first', $annotation->content); - break; - case TestAsset\DoctrineAnnotation::class: - self::assertEquals(['foo' => 'bar', 'bar' => 'baz'], $annotation->value); - break; - default: - $this->fail('Received unexpected annotation "' . get_class($annotation) . '"'); - } - } - } -} diff --git a/test/Annotation/DoctrineAnnotationParserTest.php b/test/Annotation/DoctrineAnnotationParserTest.php deleted file mode 100644 index 1f5ab2c4..00000000 --- a/test/Annotation/DoctrineAnnotationParserTest.php +++ /dev/null @@ -1,100 +0,0 @@ -markTestSkipped( - 'Enable TESTS_ZEND_CODE_ANNOTATION_DOCTRINE_SUPPORT to test doctrine annotation parsing' - ); - } - - $this->parser = new DoctrineAnnotationParser(); - } - - public function getEvent() - { - $event = new Event(); - $event->setParams([ - 'class' => __NAMESPACE__ . '\TestAsset\DoctrineAnnotation', - 'content' => '(foo="bar")', - 'raw' => '@' . __NAMESPACE__ . '\TestAsset\DoctrineAnnotation(foo="bar")', - ]); - return $event; - } - - public function testParserCreatesNewAnnotationInstances() - { - $this->parser->registerAnnotation(__NAMESPACE__ . '\TestAsset\DoctrineAnnotation'); - - $event = $this->getEvent(); - $test = $this->parser->onCreateAnnotation($event); - self::assertInstanceOf(__NAMESPACE__ . '\TestAsset\DoctrineAnnotation', $test); - self::assertEquals(['foo' => 'bar'], $test->value); - } - - public function testReturnsFalseDuringCreationIfAnnotationIsNotRegistered() - { - $event = $this->getEvent(); - self::assertFalse($this->parser->onCreateAnnotation($event)); - } - - public function testReturnsFalseClassNotSet() - { - self::assertFalse($this->parser->onCreateAnnotation(new Event())); - } - - public function testReturnsFalseRawNotSet() - { - $this->parser->registerAnnotation(__NAMESPACE__ . '\TestAsset\DoctrineAnnotation'); - $event = $this->getEvent(); - $event->setParam('raw', false); - - self::assertFalse($this->parser->onCreateAnnotation($event)); - } - - public function testReturnsFalseEmptyAnnotations() - { - $this->parser->registerAnnotation(__NAMESPACE__ . '\TestAsset\DoctrineAnnotation'); - $event = $this->getEvent(); - $event->setParam('raw', 'foo'); - self::assertFalse($this->parser->onCreateAnnotation($event)); - } - - public function testRegisterAnnotationsThrowsException() - { - $this->expectException(InvalidArgumentException::class); - $this->parser->registerAnnotations('some string'); - } - - public function testRegisterAnnotations() - { - $this->parser->registerAnnotations([__NAMESPACE__ . '\TestAsset\DoctrineAnnotation']); - $event = $this->getEvent(); - $test = $this->parser->onCreateAnnotation($event); - self::assertInstanceOf(__NAMESPACE__ . '\TestAsset\DoctrineAnnotation', $test); - self::assertEquals(['foo' => 'bar'], $test->value); - } -} diff --git a/test/Annotation/GenericAnnotationParserTest.php b/test/Annotation/GenericAnnotationParserTest.php deleted file mode 100644 index 22d7c551..00000000 --- a/test/Annotation/GenericAnnotationParserTest.php +++ /dev/null @@ -1,127 +0,0 @@ -parser = new Annotation\Parser\GenericAnnotationParser(); - } - - public function getFooEvent() - { - $event = new Event(); - $event->setParams([ - 'class' => __NAMESPACE__ . '\TestAsset\Foo', - 'content' => '(test content)', - 'raw' => '@' . __NAMESPACE__ . '\TestAsset\Foo(test content)', - ]); - return $event; - } - - public function testParserKeepsTrackOfAllowedAnnotations() - { - $this->parser->registerAnnotation(new TestAsset\Foo()); - $this->parser->registerAnnotation(new TestAsset\Bar()); - - self::assertTrue($this->parser->hasAnnotation(__NAMESPACE__ . '\TestAsset\Foo')); - self::assertTrue($this->parser->hasAnnotation(__NAMESPACE__ . '\TestAsset\Bar')); - self::assertFalse($this->parser->hasAnnotation(__NAMESPACE__ . '\TestAsset\Bogus')); - } - - public function testParserCreatesNewAnnotationInstances() - { - $foo = new TestAsset\Foo(); - $this->parser->registerAnnotation($foo); - - $event = $this->getFooEvent(); - $test = $this->parser->onCreateAnnotation($event); - self::assertInstanceOf(__NAMESPACE__ . '\TestAsset\Foo', $test); - self::assertNotSame($foo, $test); - self::assertEquals('test content', $test->content); - } - - public function testReturnsFalseDuringCreationIfAnnotationIsNotRegistered() - { - $event = $this->getFooEvent(); - self::assertFalse($this->parser->onCreateAnnotation($event)); - } - - public function testParserAllowsPassingArrayOfAnnotationInstances() - { - $this->parser->registerAnnotations([ - new TestAsset\Foo(), - new TestAsset\Bar(), - ]); - self::assertTrue($this->parser->hasAnnotation(__NAMESPACE__ . '\TestAsset\Foo')); - self::assertTrue($this->parser->hasAnnotation(__NAMESPACE__ . '\TestAsset\Bar')); - } - - public function testAllowsSpecifyingAliases() - { - $bar = new TestAsset\Bar(); - $this->parser->registerAnnotation($bar); - $this->parser->setAlias(__NAMESPACE__ . '\TestAsset\Foo', get_class($bar)); - - $event = $this->getFooEvent(); - $test = $this->parser->onCreateAnnotation($event); - self::assertInstanceOf(__NAMESPACE__ . '\TestAsset\Bar', $test); - self::assertNotSame($bar, $test); - self::assertEquals('test content', $test->content); - } - - public function testRegisterAnnotationAllowsAnnotationInterfaceOnly() - { - $this->expectException(InvalidArgumentException::class); - $this->parser->registerAnnotation(new \stdClass()); - } - - public function testAllowRegistrationOnceOnly() - { - $bar = new TestAsset\Bar(); - $this->parser->registerAnnotation($bar); - - $this->expectException(InvalidArgumentException::class); - $this->parser->registerAnnotation($bar); - } - - public function testRegisterAnnotations() - { - $this->parser->registerAnnotations([new TestAsset\Foo()]); - $event = $this->getFooEvent(); - $test = $this->parser->onCreateAnnotation($event); - self::assertInstanceOf(__NAMESPACE__ . '\TestAsset\Foo', $test); - } - - public function testRegisterAnnotationsThrowsException() - { - $this->expectException(InvalidArgumentException::class); - $this->parser->registerAnnotations('some string'); - } - - public function testSetAliasNotRegisteredClassThrowsException() - { - $this->expectException(InvalidArgumentException::class); - $this->parser->setAlias('bar', 'foo'); - } -} diff --git a/test/Annotation/TestAsset/Bar.php b/test/Annotation/TestAsset/Bar.php deleted file mode 100644 index 6b58b282..00000000 --- a/test/Annotation/TestAsset/Bar.php +++ /dev/null @@ -1,22 +0,0 @@ -content = $content; - } -} diff --git a/test/Annotation/TestAsset/DoctrineAnnotation.php b/test/Annotation/TestAsset/DoctrineAnnotation.php deleted file mode 100644 index 59d08b46..00000000 --- a/test/Annotation/TestAsset/DoctrineAnnotation.php +++ /dev/null @@ -1,23 +0,0 @@ -value = $value; - } -} diff --git a/test/Annotation/TestAsset/EntityWithAnnotations.php b/test/Annotation/TestAsset/EntityWithAnnotations.php deleted file mode 100644 index bfe8efe2..00000000 --- a/test/Annotation/TestAsset/EntityWithAnnotations.php +++ /dev/null @@ -1,27 +0,0 @@ -content = $content; - } -} diff --git a/test/Scanner/AnnotationScannerTest.php b/test/Scanner/AnnotationScannerTest.php deleted file mode 100644 index 965cdf5a..00000000 --- a/test/Scanner/AnnotationScannerTest.php +++ /dev/null @@ -1,59 +0,0 @@ -registerAnnotations([ - $foo = new TestAsset\Annotation\Foo(), - $bar = new TestAsset\Annotation\Bar(), - ]); - $annotationManager->attach($parser); - - $docComment = '/**' . $newLine - . ' * @Test\Foo(\'anything I want()' . $newLine - . ' * to be\')' . $newLine - . ' * @Test\Bar' . $newLine . ' */'; - - $nameInfo = new NameInformation(); - $nameInfo->addUse('ZendTest\Code\Scanner\TestAsset\Annotation', 'Test'); - - $annotationScanner = new AnnotationScanner($annotationManager, $docComment, $nameInfo); - self::assertEquals(get_class($foo), get_class($annotationScanner[0])); - self::assertEquals("'anything I want()\n to be'", $annotationScanner[0]->getContent()); - self::assertEquals(get_class($bar), get_class($annotationScanner[1])); - } - - public function newLine() - { - return [ - ["\n"], - ["\r"], - ["\r\n"], - ]; - } -} diff --git a/test/Scanner/TestAsset/Annotation/Bar.php b/test/Scanner/TestAsset/Annotation/Bar.php deleted file mode 100644 index 8e852167..00000000 --- a/test/Scanner/TestAsset/Annotation/Bar.php +++ /dev/null @@ -1,22 +0,0 @@ -content = $content; - } -} diff --git a/test/Scanner/TestAsset/Annotation/Foo.php b/test/Scanner/TestAsset/Annotation/Foo.php deleted file mode 100644 index adafb857..00000000 --- a/test/Scanner/TestAsset/Annotation/Foo.php +++ /dev/null @@ -1,28 +0,0 @@ -content = $content; - } - - public function getContent() - { - return $this->content; - } - -} From 40b1b6d9fddebce272ed4f28c059791ddd5da386 Mon Sep 17 00:00:00 2001 From: Alexander Lisachenko Date: Tue, 10 Apr 2018 18:34:00 +0300 Subject: [PATCH 3/6] Remove zendframework/zend-eventmanager dependency used for annotations --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index fa3ff43c..05f10a03 100644 --- a/composer.json +++ b/composer.json @@ -13,8 +13,7 @@ } }, "require": { - "php": "^7.1", - "zendframework/zend-eventmanager": "^2.6 || ^3.0" + "php": "^7.1" }, "require-dev": { "ext-phar": "*", From 60e51e5174734f4530dca534ad25c67bb9e52432 Mon Sep 17 00:00:00 2001 From: Alexander Lisachenko Date: Wed, 11 Apr 2018 14:06:28 +0300 Subject: [PATCH 4/6] Update composer.lock according to the composer.json --- composer.lock | 123 +++++++++++++++++++++++++------------------------- 1 file changed, 61 insertions(+), 62 deletions(-) diff --git a/composer.lock b/composer.lock index c302013f..a842bf76 100644 --- a/composer.lock +++ b/composer.lock @@ -4,63 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "9adb99a36380302c2a3436e1d28315cb", - "packages": [ - { - "name": "zendframework/zend-eventmanager", - "version": "3.2.0", - "source": { - "type": "git", - "url": "https://github.com/zendframework/zend-eventmanager.git", - "reference": "9d72db10ceb6e42fb92350c0cb54460da61bd79c" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/zendframework/zend-eventmanager/zipball/9d72db10ceb6e42fb92350c0cb54460da61bd79c", - "reference": "9d72db10ceb6e42fb92350c0cb54460da61bd79c", - "shasum": "" - }, - "require": { - "php": "^5.6 || ^7.0" - }, - "require-dev": { - "athletic/athletic": "^0.1", - "container-interop/container-interop": "^1.1.0", - "phpunit/phpunit": "^6.0.7 || ^5.7.14", - "zendframework/zend-coding-standard": "~1.0.0", - "zendframework/zend-stdlib": "^2.7.3 || ^3.0" - }, - "suggest": { - "container-interop/container-interop": "^1.1.0, to use the lazy listeners feature", - "zendframework/zend-stdlib": "^2.7.3 || ^3.0, to use the FilterChain feature" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.2-dev", - "dev-develop": "3.3-dev" - } - }, - "autoload": { - "psr-4": { - "Zend\\EventManager\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "description": "Trigger and listen to events within a PHP application", - "homepage": "https://github.com/zendframework/zend-eventmanager", - "keywords": [ - "event", - "eventmanager", - "events", - "zf2" - ], - "time": "2017-07-11T19:17:22+00:00" - } - ], + "content-hash": "1095d7a314d3a9a40103c1ec6a96e6c4", + "packages": [], "packages-dev": [ { "name": "doctrine/annotations", @@ -1095,16 +1040,16 @@ }, { "name": "sebastian/diff", - "version": "dev-master", + "version": "2.0.x-dev", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/diff.git", - "reference": "ed724f5d860f9a4cf8eb8ba27ea45cac9541bb5b" + "reference": "abcc70409ddfb310a8cb41ef0c2e857425438cf4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/ed724f5d860f9a4cf8eb8ba27ea45cac9541bb5b", - "reference": "ed724f5d860f9a4cf8eb8ba27ea45cac9541bb5b", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/abcc70409ddfb310a8cb41ef0c2e857425438cf4", + "reference": "abcc70409ddfb310a8cb41ef0c2e857425438cf4", "shasum": "" }, "require": { @@ -1143,7 +1088,7 @@ "keywords": [ "diff" ], - "time": "2017-07-13 02:44:20" + "time": "2017-12-14T11:32:19+00:00" }, { "name": "sebastian/environment", @@ -1740,6 +1685,60 @@ ], "time": "2016-11-09T21:30:43+00:00" }, + { + "name": "zendframework/zend-eventmanager", + "version": "3.2.0", + "source": { + "type": "git", + "url": "https://github.com/zendframework/zend-eventmanager.git", + "reference": "9d72db10ceb6e42fb92350c0cb54460da61bd79c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/zendframework/zend-eventmanager/zipball/9d72db10ceb6e42fb92350c0cb54460da61bd79c", + "reference": "9d72db10ceb6e42fb92350c0cb54460da61bd79c", + "shasum": "" + }, + "require": { + "php": "^5.6 || ^7.0" + }, + "require-dev": { + "athletic/athletic": "^0.1", + "container-interop/container-interop": "^1.1.0", + "phpunit/phpunit": "^6.0.7 || ^5.7.14", + "zendframework/zend-coding-standard": "~1.0.0", + "zendframework/zend-stdlib": "^2.7.3 || ^3.0" + }, + "suggest": { + "container-interop/container-interop": "^1.1.0, to use the lazy listeners feature", + "zendframework/zend-stdlib": "^2.7.3 || ^3.0, to use the FilterChain feature" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.2-dev", + "dev-develop": "3.3-dev" + } + }, + "autoload": { + "psr-4": { + "Zend\\EventManager\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "description": "Trigger and listen to events within a PHP application", + "homepage": "https://github.com/zendframework/zend-eventmanager", + "keywords": [ + "event", + "eventmanager", + "events", + "zf2" + ], + "time": "2017-07-11T19:17:22+00:00" + }, { "name": "zendframework/zend-stdlib", "version": "3.1.0", From ce7c25061515cfb858b95fe59691dbf66382335a Mon Sep 17 00:00:00 2001 From: Alexander Lisachenko Date: Fri, 13 Apr 2018 16:37:40 +0300 Subject: [PATCH 5/6] Add notification about removed annotation API in the CHANGELOG --- CHANGELOG.md | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24283637..4b3fb400 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. -## 3.4.0 - TBD +## 4.0.0 - TBD ### Added @@ -14,25 +14,8 @@ All notable changes to this project will be documented in this file, in reverse ### Removed -- Nothing. - -### Fixed - -- Nothing. - -## 3.3.1 - TBD - -### Added - -- Nothing. - -### Deprecated - -- Nothing. - -### Removed - -- Nothing. +- [#153](https://github.com/zendframework/zend-code/pull/153) Removed + the annotation API from the library ### Fixed From 021b1c5ef5dc71bf24cd1499c6c90b3a0a382d2b Mon Sep 17 00:00:00 2001 From: Alexander Lisachenko Date: Fri, 13 Apr 2018 16:49:33 +0300 Subject: [PATCH 6/6] Fix broken test https://travis-ci.org/zendframework/zend-code/jobs/364674886 --- test/Scanner/CachingFileScannerTest.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/test/Scanner/CachingFileScannerTest.php b/test/Scanner/CachingFileScannerTest.php index a5f26844..74f1feea 100644 --- a/test/Scanner/CachingFileScannerTest.php +++ b/test/Scanner/CachingFileScannerTest.php @@ -40,18 +40,6 @@ public function testCachingFileScannerWillUseSameInternalFileScannerWithMatching $cfs3 = new CachingFileScanner(__DIR__ . '/../TestAsset/FooClass.php'); self::assertEquals(2, $this->getCacheCount($cfs3)); self::assertDifferentInternalFileScanner($cfs2, $cfs3); - - $cfs4 = new CachingFileScanner(__DIR__ . '/../TestAsset/FooClass.php'); - self::assertEquals(3, $this->getCacheCount($cfs4)); - self::assertDifferentInternalFileScanner($cfs3, $cfs4); - - $cfs5 = new CachingFileScanner(__DIR__ . '/../TestAsset/FooClass.php'); - self::assertEquals(3, $this->getCacheCount($cfs5)); - self::assertSameInternalFileScanner($cfs4, $cfs5); - - $cfs6 = new CachingFileScanner(__DIR__ . '/../TestAsset/BarClass.php'); - self::assertEquals(4, $this->getCacheCount($cfs6)); - self::assertDifferentInternalFileScanner($cfs5, $cfs6); } protected function getCacheCount(CachingFileScanner $cfs)