From f82485e651763fbd1b34879726f4d3b91c358bd9 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Mon, 19 Dec 2022 22:51:58 +0100 Subject: [PATCH] Support doctrine/annotations 2 (#10320) --- .github/workflows/continuous-integration.yml | 12 ++++++++++++ .github/workflows/static-analysis.yml | 6 ++++++ composer.json | 4 ++-- lib/Doctrine/ORM/Configuration.php | 10 +++++++++- phpstan-baseline.neon | 5 ----- phpstan-persistence2.neon | 5 +++++ psalm-baseline.xml | 3 +-- psalm.xml | 3 +++ tests/Doctrine/Tests/ORM/ConfigurationTest.php | 11 +++++++++++ tests/Doctrine/Tests/ORM/Tools/SetupTest.php | 6 +++--- 10 files changed, 52 insertions(+), 13 deletions(-) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index a33cd450e87..3b79790ebb4 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -78,6 +78,9 @@ jobs: run: "composer require doctrine/dbal ^${{ matrix.dbal-version }} --no-update" if: "${{ matrix.dbal-version != 'default' }}" + - name: "Uninstall PHPBench" + run: "composer remove --dev --no-update phpbench/phpbench" + - name: "Install dependencies with Composer" uses: "ramsey/composer-install@v2" with: @@ -153,6 +156,9 @@ jobs: run: "composer require doctrine/dbal ^${{ matrix.dbal-version }} --no-update" if: "${{ matrix.dbal-version != 'default' }}" + - name: "Uninstall PHPBench" + run: "composer remove --dev --no-update phpbench/phpbench" + - name: "Install dependencies with Composer" uses: "ramsey/composer-install@v2" with: @@ -214,6 +220,9 @@ jobs: run: "composer require doctrine/dbal ^${{ matrix.dbal-version }} --no-update" if: "${{ matrix.dbal-version != 'default' }}" + - name: "Uninstall PHPBench" + run: "composer remove --dev --no-update phpbench/phpbench" + - name: "Install PHP" uses: "shivammathur/setup-php@v2" with: @@ -291,6 +300,9 @@ jobs: run: "composer require doctrine/dbal ^${{ matrix.dbal-version }} --no-update" if: "${{ matrix.dbal-version != 'default' }}" + - name: "Uninstall PHPBench" + run: "composer remove --dev --no-update phpbench/phpbench" + - name: "Install dependencies with Composer" uses: "ramsey/composer-install@v2" with: diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index 2e9263b7a57..8e947594e9d 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -57,6 +57,9 @@ jobs: - name: "Require specific persistence version" run: "composer require doctrine/persistence ^$([ ${{ matrix.persistence-version }} = default ] && echo '3.1' || echo ${{ matrix.persistence-version }}) --no-update" + - name: "Uninstall PHPBench" + run: "composer remove --dev --no-update phpbench/phpbench" + - name: "Install dependencies with Composer" uses: "ramsey/composer-install@v2" with: @@ -94,6 +97,9 @@ jobs: - name: "Require specific persistence version" run: "composer require doctrine/persistence ^3.1 --no-update" + - name: "Uninstall PHPBench" + run: "composer remove --dev --no-update phpbench/phpbench" + - name: "Install dependencies with Composer" uses: "ramsey/composer-install@v2" with: diff --git a/composer.json b/composer.json index 28338e419bd..a1538a6b217 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "symfony/polyfill-php80": "^1.16" }, "require-dev": { - "doctrine/annotations": "^1.13", + "doctrine/annotations": "^1.13 || ^2", "doctrine/coding-standard": "^9.0.2 || ^11.0", "phpbench/phpbench": "^0.16.10 || ^1.0", "phpstan/phpstan": "~1.4.10 || 1.9.4", @@ -52,7 +52,7 @@ "vimeo/psalm": "4.30.0 || 5.3.0" }, "conflict": { - "doctrine/annotations": "<1.13 || >= 2.0" + "doctrine/annotations": "<1.13 || >= 3.0" }, "suggest": { "ext-dom": "Provides support for XSD validation for XML mapping files", diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index 1f6783289c0..1f3694c397b 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -4,6 +4,7 @@ namespace Doctrine\ORM; +use BadMethodCallException; use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Annotations\CachedReader; use Doctrine\Common\Annotations\SimpleAnnotationReader; @@ -182,6 +183,13 @@ public function newDefaultAnnotationDriver($paths = [], $useSimpleAnnotationRead } if ($useSimpleAnnotationReader) { + if (! class_exists(SimpleAnnotationReader::class)) { + throw new BadMethodCallException( + 'SimpleAnnotationReader has been removed in doctrine/annotations 2.' + . ' Downgrade to version 1 or set $useSimpleAnnotationReader to false.' + ); + } + // Register the ORM Annotations in the AnnotationRegistry $reader = new SimpleAnnotationReader(); $reader->addNamespace('Doctrine\ORM\Mapping'); @@ -189,7 +197,7 @@ public function newDefaultAnnotationDriver($paths = [], $useSimpleAnnotationRead $reader = new AnnotationReader(); } - if (class_exists(ArrayCache::class)) { + if (class_exists(ArrayCache::class) && class_exists(CachedReader::class)) { $reader = new CachedReader($reader, new ArrayCache()); } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 06edcd9b719..e4d6f332302 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -110,11 +110,6 @@ parameters: count: 2 path: lib/Doctrine/ORM/Configuration.php - - - message: "#^Parameter \\#2 \\$cache of class Doctrine\\\\Common\\\\Annotations\\\\CachedReader constructor expects Doctrine\\\\Common\\\\Cache\\\\Cache, Doctrine\\\\Common\\\\Cache\\\\ArrayCache given\\.$#" - count: 1 - path: lib/Doctrine/ORM/Configuration.php - - message: "#^Method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:find\\(\\) invoked with 4 parameters, 2 required\\.$#" count: 1 diff --git a/phpstan-persistence2.neon b/phpstan-persistence2.neon index 394498d9e89..074f7473d5e 100644 --- a/phpstan-persistence2.neon +++ b/phpstan-persistence2.neon @@ -41,3 +41,8 @@ parameters: # Symfony cache supports passing a key prefix to the clear method. - '/^Method Psr\\Cache\\CacheItemPoolInterface\:\:clear\(\) invoked with 1 parameter, 0 required\.$/' + + # Cache 1 compatibility + - + message: '~^Parameter #2 \$cache of class Doctrine\\Common\\Annotations\\CachedReader constructor expects Doctrine\\Common\\Cache\\Cache, Doctrine\\Common\\Cache\\ArrayCache given\.~' + path: lib/Doctrine/ORM/Configuration.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 613a58b3f60..16a5a3999bc 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -248,8 +248,7 @@ new CachedReader($reader, new ArrayCache()) new SimpleAnnotationReader() - - AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php') + getMetadataCacheImpl getQueryCacheImpl diff --git a/psalm.xml b/psalm.xml index 26888dd84e0..d4e9af51f08 100644 --- a/psalm.xml +++ b/psalm.xml @@ -220,6 +220,9 @@ + + + diff --git a/tests/Doctrine/Tests/ORM/ConfigurationTest.php b/tests/Doctrine/Tests/ORM/ConfigurationTest.php index e75721b9cdd..c4656649911 100644 --- a/tests/Doctrine/Tests/ORM/ConfigurationTest.php +++ b/tests/Doctrine/Tests/ORM/ConfigurationTest.php @@ -4,6 +4,7 @@ namespace Doctrine\Tests\ORM; +use Doctrine\Common\Annotations\SimpleAnnotationReader; use Doctrine\Common\Cache\ArrayCache; use Doctrine\Common\Cache\Cache; use Doctrine\Common\Cache\Psr6\CacheAdapter; @@ -105,6 +106,16 @@ public function testNewDefaultAnnotationDriver(): void AnnotationNamespace\PrePersist::class ); self::assertInstanceOf(AnnotationNamespace\PrePersist::class, $annotation); + } + + public function testNewDefaultAnnotationDriverWithSimpleAnnotationReader(): void + { + if (! class_exists(SimpleAnnotationReader::class)) { + self::markTestSkipped('Requires doctrine/annotations 1.x'); + } + + $paths = [__DIR__]; + $reflectionClass = new ReflectionClass(ConfigurationTestAnnotationReaderChecker::class); $annotationDriver = $this->configuration->newDefaultAnnotationDriver($paths); $reader = $annotationDriver->getReader(); diff --git a/tests/Doctrine/Tests/ORM/Tools/SetupTest.php b/tests/Doctrine/Tests/ORM/Tools/SetupTest.php index dc5af62c3ef..df5b0141661 100644 --- a/tests/Doctrine/Tests/ORM/Tools/SetupTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/SetupTest.php @@ -61,7 +61,7 @@ public function testDirectoryAutoload(): void public function testAnnotationConfiguration(): void { - $config = Setup::createAnnotationMetadataConfiguration([], true); + $config = Setup::createAnnotationMetadataConfiguration([], true, null, null, false); self::assertInstanceOf(Configuration::class, $config); self::assertEquals(sys_get_temp_dir(), $config->getProxyDir()); @@ -135,7 +135,7 @@ public function testConfiguredCacheNamespaceShouldBeUsedAsPrefixOfGeneratedNames /** @group DDC-1350 */ public function testConfigureProxyDir(): void { - $config = Setup::createAnnotationMetadataConfiguration([], true, '/foo'); + $config = Setup::createAnnotationMetadataConfiguration([], true, '/foo', null, false); self::assertEquals('/foo', $config->getProxyDir()); } @@ -144,7 +144,7 @@ public function testConfigureCache(): void { $adapter = new ArrayAdapter(); $cache = DoctrineProvider::wrap($adapter); - $config = Setup::createAnnotationMetadataConfiguration([], true, null, $cache); + $config = Setup::createAnnotationMetadataConfiguration([], true, null, $cache, false); self::assertSame($adapter, $config->getResultCache()->getCache()->getPool()); self::assertSame($cache, $config->getResultCacheImpl());