diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index 691e4820dca..01780c3c3ff 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -51,6 +51,12 @@ jobs: - name: "Run a static analysis with phpstan/phpstan" continue-on-error: "${{ matrix.status == 'experimental' }}" run: "vendor/bin/phpstan analyse" + if: "${{ matrix.dbal-version == 'default' }}" + + - name: "Run a static analysis with phpstan/phpstan" + continue-on-error: "${{ matrix.status == 'experimental' }}" + run: "vendor/bin/phpstan analyse -c phpstan-dbal3.neon" + if: "${{ matrix.dbal-version != 'default' }}" static-analysis-psalm: name: "Static Analysis with Psalm" diff --git a/lib/Doctrine/ORM/Exception/NotSupported.php b/lib/Doctrine/ORM/Exception/NotSupported.php index 852826b6f6e..8669aa17a9d 100644 --- a/lib/Doctrine/ORM/Exception/NotSupported.php +++ b/lib/Doctrine/ORM/Exception/NotSupported.php @@ -10,4 +10,9 @@ public static function create(): self { return new self('This behaviour is (currently) not supported by Doctrine 2'); } + + public static function createForDbal3(): self + { + return new self('Feature was deprecated in doctrine/dbal 2.x and is not supported by installed doctrine/dbal:3.x, please see the doctrine/deprecations logs for new alternative approaches.'); + } } diff --git a/lib/Doctrine/ORM/Id/UuidGenerator.php b/lib/Doctrine/ORM/Id/UuidGenerator.php index be12ba617a6..f6b87589378 100644 --- a/lib/Doctrine/ORM/Id/UuidGenerator.php +++ b/lib/Doctrine/ORM/Id/UuidGenerator.php @@ -4,8 +4,12 @@ namespace Doctrine\ORM\Id; +use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\Deprecations\Deprecation; use Doctrine\ORM\EntityManager; +use Doctrine\ORM\Exception\NotSupported; + +use function method_exists; /** * Represents an ID generator that uses the database UUID expression @@ -22,10 +26,16 @@ public function __construct() '%s is deprecated with no replacement, use an application-side generator instead', self::class ); + + if (! method_exists(AbstractPlatform::class, 'getGuidExpression')) { + throw NotSupported::createForDbal3(); + } } /** * {@inheritDoc} + * + * @throws NotSupported */ public function generate(EntityManager $em, $entity) { diff --git a/phpstan-dbal3.neon b/phpstan-dbal3.neon new file mode 100644 index 00000000000..217657bd647 --- /dev/null +++ b/phpstan-dbal3.neon @@ -0,0 +1,8 @@ +includes: + - phpstan-baseline.neon + - phpstan-params.neon + +parameters: + ignoreErrors: + # deprecations from doctrine/dbal:3.x + - '/^Call to an undefined method Doctrine\\DBAL\\Platforms\\AbstractPlatform::getGuidExpression\(\).$/' diff --git a/phpstan-params.neon b/phpstan-params.neon new file mode 100644 index 00000000000..df303cb0365 --- /dev/null +++ b/phpstan-params.neon @@ -0,0 +1,14 @@ +parameters: + level: 5 + paths: + - lib + - tests/Doctrine/StaticAnalysis + excludePaths: + - lib/Doctrine/ORM/Mapping/Driver/AttributeReader.php + earlyTerminatingMethodCalls: + Doctrine\ORM\Query\Parser: + - syntaxError + phpVersion: 70100 + ignoreErrors: + # The class was added in PHP 8.1 + - '/^Attribute class ReturnTypeWillChange does not exist.$/' diff --git a/phpstan.neon b/phpstan.neon index fbe0fb36d3b..57c10af3d7e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,21 +1,8 @@ includes: - phpstan-baseline.neon + - phpstan-params.neon parameters: - level: 5 - paths: - - lib - - tests/Doctrine/StaticAnalysis - excludePaths: - - lib/Doctrine/ORM/Mapping/Driver/AttributeReader.php - earlyTerminatingMethodCalls: - Doctrine\ORM\Query\Parser: - - syntaxError - phpVersion: 70100 - ignoreErrors: - # The class was added in PHP 8.1 - - '/^Attribute class ReturnTypeWillChange does not exist.$/' - # https://github.com/doctrine/collections/pull/282 - '/Variable \$offset in isset\(\) always exists and is not nullable\./' diff --git a/psalm.xml b/psalm.xml index a236e670f74..13a3d928783 100644 --- a/psalm.xml +++ b/psalm.xml @@ -47,5 +47,11 @@ + + + + + + diff --git a/tests/Doctrine/Tests/ORM/Functional/UUIDGeneratorTest.php b/tests/Doctrine/Tests/ORM/Functional/UUIDGeneratorTest.php index 00d262b526f..b67a64d225f 100644 --- a/tests/Doctrine/Tests/ORM/Functional/UUIDGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/UUIDGeneratorTest.php @@ -4,13 +4,17 @@ namespace Doctrine\Tests\ORM\Functional; +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; +use Doctrine\ORM\Exception\NotSupported; use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Mapping\GeneratedValue; use Doctrine\ORM\Mapping\Id; use Doctrine\Tests\OrmFunctionalTestCase; +use function method_exists; use function strlen; /** @@ -22,6 +26,10 @@ class UUIDGeneratorTest extends OrmFunctionalTestCase public function testItIsDeprecated(): void { + if (! method_exists(AbstractPlatform::class, 'getGuidExpression')) { + self::markTestSkipped('Test valid for doctrine/dbal:2.x only.'); + } + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/7312'); $this->_em->getClassMetadata(UUIDEntity::class); } @@ -32,6 +40,10 @@ public function testGenerateUUID(): void self::markTestSkipped('Currently restricted to MySQL platform.'); } + if (! method_exists(AbstractPlatform::class, 'getGuidExpression')) { + self::markTestSkipped('Test valid for doctrine/dbal:2.x only.'); + } + $this->_schemaTool->createSchema([ $this->_em->getClassMetadata(UUIDEntity::class), ]); @@ -41,6 +53,16 @@ public function testGenerateUUID(): void self::assertNotNull($entity->getId()); self::assertGreaterThan(0, strlen($entity->getId())); } + + public function testItCannotBeInitialised(): void + { + if (method_exists(AbstractPlatform::class, 'getGuidExpression')) { + self::markTestSkipped('Test valid for doctrine/dbal:3.x only.'); + } + + $this->expectException(NotSupported::class); + $this->_em->getClassMetadata(UUIDEntity::class); + } } /**