Skip to content

Commit

Permalink
Throw exception NotSupported Exception for UuidGenerator with doctr…
Browse files Browse the repository at this point in the history
…ine/dbal:3.x. (#8898)

Generating `getGuidExpression` has been removed in doctrine/dbal:3.x.

Partially fixes #8884
  • Loading branch information
scyzoryck authored Aug 22, 2021
1 parent e775886 commit 8bed630
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/ORM/Exception/NotSupported.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
}
10 changes: 10 additions & 0 deletions lib/Doctrine/ORM/Id/UuidGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
{
Expand Down
8 changes: 8 additions & 0 deletions phpstan-dbal3.neon
Original file line number Diff line number Diff line change
@@ -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\(\).$/'
14 changes: 14 additions & 0 deletions phpstan-params.neon
Original file line number Diff line number Diff line change
@@ -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.$/'
15 changes: 1 addition & 14 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -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\./'
6 changes: 6 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,11 @@
<referencedClass name="Doctrine\Common\Cache\XcacheCache"/>
</errorLevel>
</UndefinedClass>
<UndefinedMethod>
<errorLevel type="suppress">
<!-- See https://github.com/doctrine/orm/issues/8884 -->
<referencedMethod name="Doctrine\DBAL\Platforms\AbstractPlatform::getGuidExpression"/>
</errorLevel>
</UndefinedMethod>
</issueHandlers>
</psalm>
22 changes: 22 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/UUIDGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
}
Expand All @@ -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),
]);
Expand All @@ -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);
}
}

/**
Expand Down

0 comments on commit 8bed630

Please sign in to comment.