Skip to content

Commit

Permalink
feat: allow doctrine/dbal v4
Browse files Browse the repository at this point in the history
  • Loading branch information
kochen committed Mar 13, 2024
1 parent fafebbe commit 1229543
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 62 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,9 @@ jobs:
fail-fast: false
matrix:
php:
- "7.4"
- "8.0"
- "8.1"
- "8.2"
- "8.3"
composer-deps: ["lowest", "highest"]

steps:
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
}
],
"require": {
"php": "^7.4 || ^8.0",
"doctrine/dbal": "^2.8 || ^3.0",
"php": "^8.1",
"doctrine/dbal": "^2.8 || ^3.0 || ^4.0",
"ramsey/uuid": "^3.9.7 || ^4.0"
},
"require-dev": {
"captainhook/plugin-composer": "^5.3",
"doctrine/orm": "^2.5",
"doctrine/orm": "^2.5 || ^3.0",
"ergebnis/composer-normalize": "^2.28.3",
"mockery/mockery": "^1.5",
"php-parallel-lint/php-console-highlighter": "^1.0",
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpcsstandards/phpcsutils": "^1.0.0-alpha4",
"phpcsstandards/phpcsutils": "^1.0",
"phpstan/extension-installer": "^1.2",
"phpstan/phpstan": "^1.9",
"phpstan/phpstan-mockery": "^1.1",
Expand All @@ -55,11 +55,11 @@
},
"config": {
"allow-plugins": {
"captainhook/plugin-composer": true,
"composer/package-versions-deprecated": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"ergebnis/composer-normalize": true,
"phpstan/extension-installer": true,
"captainhook/plugin-composer": true
"phpstan/extension-installer": true
},
"sort-packages": true
},
Expand Down
4 changes: 4 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@

<rule ref="Ramsey"/>

<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
<exclude-pattern>src/GetBindingTypeImplementation.php</exclude-pattern>
</rule>

</ruleset>
10 changes: 10 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@ parameters:
paths:
- ./src
- ./tests
ignoreErrors:
# That class no longer holds methods in DBAL 4, but needs to be called with DBAL 3
- message: '#Call to an undefined static method Doctrine\\DBAL\\Types\\ConversionException::.*#'
reportUnmatched: false

# Necessary type hint for testing methods that no longer exist in the parent class with DBAL 4
- message: '#Method.*getBindingType.*should return .*#'
reportUnmatched: false
- message: '#Return type.*getBindingType.*should be compatible .*#'
reportUnmatched: false
11 changes: 10 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorLevel="1"
findUnusedPsalmSuppress="true"
findUnusedPsalmSuppress="false"
cacheDirectory="./build/cache/psalm"
errorBaseline="./psalm-baseline.xml">

Expand All @@ -20,4 +20,13 @@
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
</plugins>

<issueHandlers>
<UndefinedMethod>
<errorLevel type="suppress">
<referencedMethod name="Doctrine\DBAL\Types\ConversionException::conversionFailed" />
<referencedMethod name="Doctrine\DBAL\Types\ConversionException::conversionfailedformat" />
</errorLevel>
</UndefinedMethod>
</issueHandlers>

</psalm>
48 changes: 48 additions & 0 deletions src/GetBindingTypeImplementation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* This file is part of the ramsey/uuid-doctrine library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <http://benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/

declare(strict_types=1);

namespace Ramsey\Uuid\Doctrine;

use Doctrine\DBAL\ParameterType;

use function enum_exists;
use function function_exists;

if (function_exists('enum_exists') && enum_exists(ParameterType::class)) {
/**
* @internal
*/
trait GetBindingTypeImplementation
{
/**
* @psalm-suppress InvalidReturnType
* @psalm-suppress ImplementedReturnTypeMismatch
*/
public function getBindingType(): ParameterType
{
return ParameterType::BINARY;
}
}
} else {
/**
* @internal
*/
trait GetBindingTypeImplementation
{
public function getBindingType(): int
{
return ParameterType::BINARY;
}
}
}
64 changes: 45 additions & 19 deletions src/UuidBinaryOrderedTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

namespace Ramsey\Uuid\Doctrine;

use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Exception\InvalidFormat;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\Type;
use Ramsey\Uuid\Codec\OrderedTimeCodec;
use Ramsey\Uuid\Exception\UnsupportedOperationException;
Expand All @@ -25,6 +26,7 @@
use Throwable;

use function bin2hex;
use function class_exists;
use function is_object;
use function is_string;
use function method_exists;
Expand All @@ -37,6 +39,8 @@
*/
class UuidBinaryOrderedTimeType extends Type
{
use GetBindingTypeImplementation;

public const NAME = 'uuid_binary_ordered_time';

public const ASSERT_FORMAT = 'UuidV1';
Expand All @@ -52,7 +56,7 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform): st
{
return $platform->getBinaryTypeDeclarationSQL(
[
'length' => '16',
'length' => 16,
'fixed' => true,
],
);
Expand All @@ -76,7 +80,9 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?UuidInte
try {
return $this->decode($value);
} catch (Throwable $e) {
throw ConversionException::conversionFailed($value, self::NAME);
throw class_exists(ValueNotConvertible::class) ?
ValueNotConvertible::new($value, self::NAME) :
ConversionException::conversionFailed($value, self::NAME);
}
}

Expand Down Expand Up @@ -105,24 +111,31 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
// Ignore the exception and pass through.
}

throw ConversionException::conversionFailed($value, self::NAME);
throw class_exists(ValueNotConvertible::class) ?
ValueNotConvertible::new($value, self::NAME) :
ConversionException::conversionFailed($value, self::NAME);
}

/**
* {@inheritDoc}
*
* @deprecated this method is deprecated and will be removed in Uuid-Doctrine 3.0
*/
public function getName(): string
{
return self::NAME;
}

/**
* {@inheritDoc}
*
* @deprecated this method is deprecated and will be removed in Uuid-Doctrine 3.0
*/
public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}

public function getBindingType(): int
{
return ParameterType::BINARY;
}

/**
* Creates/returns a UuidFactory instance that uses a specific codec
* that creates a binary that can be time-ordered
Expand Down Expand Up @@ -158,11 +171,18 @@ private function assertUuidV1(UuidInterface $value): void
{
/** @psalm-suppress DeprecatedMethod */
if ($value->getVersion() !== 1) {
throw ConversionException::conversionFailedFormat(
$value->toString(),
self::NAME,
self::ASSERT_FORMAT,
);
throw class_exists(InvalidFormat::class) ?
InvalidFormat::new(
$value->toString(),
self::NAME,
self::ASSERT_FORMAT,
) :
/** @psalm-suppress DeprecatedMethod */
ConversionException::conversionFailedFormat(
$value->toString(),
self::NAME,
self::ASSERT_FORMAT,
);
}
}

Expand All @@ -184,11 +204,17 @@ private function decode(string $bytes): UuidInterface
try {
$decoded = $this->getCodec()->decodeBytes($bytes);
} catch (UnsupportedOperationException $e) {
throw ConversionException::conversionFailedFormat(
bin2hex($bytes),
self::NAME,
self::ASSERT_FORMAT,
);
throw class_exists(InvalidFormat::class) ?
InvalidFormat::new(
bin2hex($bytes),
self::NAME,
self::ASSERT_FORMAT,
) :
ConversionException::conversionFailedFormat(
bin2hex($bytes),
self::NAME,
self::ASSERT_FORMAT,
);
}

$this->assertUuidV1($decoded);
Expand Down
30 changes: 21 additions & 9 deletions src/UuidBinaryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@

namespace Ramsey\Uuid\Doctrine;

use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\Type;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Throwable;

use function class_exists;
use function is_object;
use function is_string;
use function method_exists;
Expand All @@ -34,6 +35,8 @@
*/
class UuidBinaryType extends Type
{
use GetBindingTypeImplementation;

public const NAME = 'uuid_binary';

/**
Expand All @@ -43,7 +46,7 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform): st
{
return $platform->getBinaryTypeDeclarationSQL(
[
'length' => '16',
'length' => 16,
'fixed' => true,
],
);
Expand All @@ -67,7 +70,9 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?UuidInte
try {
$uuid = Uuid::fromBytes($value);
} catch (Throwable $e) {
throw ConversionException::conversionFailed($value, self::NAME);
throw class_exists(ValueNotConvertible::class)
? ValueNotConvertible::new($value, self::NAME)
: ConversionException::conversionFailed($value, self::NAME);
}

return $uuid;
Expand Down Expand Up @@ -96,21 +101,28 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
// Ignore the exception and pass through.
}

throw ConversionException::conversionFailed($value, self::NAME);
throw class_exists(ValueNotConvertible::class)
? ValueNotConvertible::new($value, self::NAME)
: ConversionException::conversionFailed($value, self::NAME);
}

/**
* {@inheritDoc}
*
* @deprecated this method is deprecated and will be removed in Uuid-Doctrine 3.0
*/
public function getName(): string
{
return self::NAME;
}

/**
* {@inheritDoc}
*
* @deprecated this method is deprecated and will be removed in Uuid-Doctrine 3.0
*/
public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}

public function getBindingType(): int
{
return ParameterType::BINARY;
}
}
Loading

0 comments on commit 1229543

Please sign in to comment.