Skip to content

Commit cf4a4f2

Browse files
scyzoryckgreg0ire
scyzoryck
authored andcommitted
Throw exception NotSupported Exception for UuidGenerator with doctrine/dbal:3.x.
Generating `getGuidExpression` has been removed in doctrine/dbal:3.x. Partially fixes #8884
1 parent aee197f commit cf4a4f2

File tree

8 files changed

+72
-14
lines changed

8 files changed

+72
-14
lines changed

.github/workflows/static-analysis.yml

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ jobs:
5151
- name: "Run a static analysis with phpstan/phpstan"
5252
continue-on-error: "${{ matrix.status == 'experimental' }}"
5353
run: "vendor/bin/phpstan analyse"
54+
if: "${{ matrix.dbal-version == 'default' }}"
55+
56+
- name: "Run a static analysis with phpstan/phpstan"
57+
continue-on-error: "${{ matrix.status == 'experimental' }}"
58+
run: "vendor/bin/phpstan analyse -c phpstan-dbal3.neon"
59+
if: "${{ matrix.dbal-version != 'default' }}"
5460

5561
static-analysis-psalm:
5662
name: "Static Analysis with Psalm"

lib/Doctrine/ORM/Exception/NotSupported.php

+5
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ public static function create(): self
1010
{
1111
return new self('This behaviour is (currently) not supported by Doctrine 2');
1212
}
13+
14+
public static function createForDbal3(): self
15+
{
16+
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.');
17+
}
1318
}

lib/Doctrine/ORM/Id/UuidGenerator.php

+10
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
namespace Doctrine\ORM\Id;
66

7+
use Doctrine\DBAL\Platforms\AbstractPlatform;
78
use Doctrine\Deprecations\Deprecation;
89
use Doctrine\ORM\EntityManager;
10+
use Doctrine\ORM\Exception\NotSupported;
11+
12+
use function method_exists;
913

1014
/**
1115
* Represents an ID generator that uses the database UUID expression
@@ -22,10 +26,16 @@ public function __construct()
2226
'%s is deprecated with no replacement, use an application-side generator instead',
2327
self::class
2428
);
29+
30+
if (! method_exists(AbstractPlatform::class, 'getGuidExpression')) {
31+
throw NotSupported::createForDbal3();
32+
}
2533
}
2634

2735
/**
2836
* {@inheritDoc}
37+
*
38+
* @throws NotSupported
2939
*/
3040
public function generate(EntityManager $em, $entity)
3141
{

phpstan-dbal3.neon

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
includes:
2+
- phpstan-baseline.neon
3+
- phpstan-params.neon
4+
5+
parameters:
6+
ignoreErrors:
7+
# deprecations from doctrine/dbal:3.x
8+
- '/^Call to an undefined method Doctrine\\DBAL\\Platforms\\AbstractPlatform::getGuidExpression\(\).$/'

phpstan-params.neon

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
parameters:
2+
level: 5
3+
paths:
4+
- lib
5+
- tests/Doctrine/StaticAnalysis
6+
excludePaths:
7+
- lib/Doctrine/ORM/Mapping/Driver/AttributeReader.php
8+
earlyTerminatingMethodCalls:
9+
Doctrine\ORM\Query\Parser:
10+
- syntaxError
11+
phpVersion: 70100
12+
ignoreErrors:
13+
# The class was added in PHP 8.1
14+
- '/^Attribute class ReturnTypeWillChange does not exist.$/'

phpstan.neon

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
11
includes:
22
- phpstan-baseline.neon
3+
- phpstan-params.neon
34

45
parameters:
5-
level: 5
6-
paths:
7-
- lib
8-
- tests/Doctrine/StaticAnalysis
9-
excludePaths:
10-
- lib/Doctrine/ORM/Mapping/Driver/AttributeReader.php
11-
earlyTerminatingMethodCalls:
12-
Doctrine\ORM\Query\Parser:
13-
- syntaxError
14-
phpVersion: 70100
15-
166
ignoreErrors:
17-
# The class was added in PHP 8.1
18-
- '/^Attribute class ReturnTypeWillChange does not exist.$/'
19-
207
# https://github.com/doctrine/collections/pull/282
218
- '/Variable \$offset in isset\(\) always exists and is not nullable\./'

psalm.xml

+6
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,11 @@
4747
<referencedClass name="Doctrine\Common\Cache\XcacheCache"/>
4848
</errorLevel>
4949
</UndefinedClass>
50+
<UndefinedMethod>
51+
<errorLevel type="suppress">
52+
<!-- See https://github.com/doctrine/orm/issues/8884 -->
53+
<referencedMethod name="Doctrine\DBAL\Platforms\AbstractPlatform::getGuidExpression"/>
54+
</errorLevel>
55+
</UndefinedMethod>
5056
</issueHandlers>
5157
</psalm>

tests/Doctrine/Tests/ORM/Functional/UUIDGeneratorTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
namespace Doctrine\Tests\ORM\Functional;
66

7+
use Doctrine\DBAL\Connection;
8+
use Doctrine\DBAL\Platforms\AbstractPlatform;
79
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
10+
use Doctrine\ORM\Exception\NotSupported;
811
use Doctrine\ORM\Mapping\Column;
912
use Doctrine\ORM\Mapping\Entity;
1013
use Doctrine\ORM\Mapping\GeneratedValue;
1114
use Doctrine\ORM\Mapping\Id;
1215
use Doctrine\Tests\OrmFunctionalTestCase;
1316

17+
use function method_exists;
1418
use function strlen;
1519

1620
/**
@@ -22,6 +26,10 @@ class UUIDGeneratorTest extends OrmFunctionalTestCase
2226

2327
public function testItIsDeprecated(): void
2428
{
29+
if (! method_exists(AbstractPlatform::class, 'getGuidExpression')) {
30+
self::markTestSkipped('Test valid for doctrine/dbal:2.x only.');
31+
}
32+
2533
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/7312');
2634
$this->_em->getClassMetadata(UUIDEntity::class);
2735
}
@@ -32,6 +40,10 @@ public function testGenerateUUID(): void
3240
self::markTestSkipped('Currently restricted to MySQL platform.');
3341
}
3442

43+
if (! method_exists(AbstractPlatform::class, 'getGuidExpression')) {
44+
self::markTestSkipped('Test valid for doctrine/dbal:2.x only.');
45+
}
46+
3547
$this->_schemaTool->createSchema([
3648
$this->_em->getClassMetadata(UUIDEntity::class),
3749
]);
@@ -41,6 +53,16 @@ public function testGenerateUUID(): void
4153
self::assertNotNull($entity->getId());
4254
self::assertGreaterThan(0, strlen($entity->getId()));
4355
}
56+
57+
public function testItCannotBeInitialised(): void
58+
{
59+
if (method_exists(AbstractPlatform::class, 'getGuidExpression')) {
60+
self::markTestSkipped('Test valid for doctrine/dbal:3.x only.');
61+
}
62+
63+
$this->expectException(NotSupported::class);
64+
$this->_em->getClassMetadata(UUIDEntity::class);
65+
}
4466
}
4567

4668
/**

0 commit comments

Comments
 (0)