Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dbal4 compat #253

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
],
"require": {
"php": "^7.4 || ^8.0",
"doctrine/dbal": "^2.8 || ^3.0",
"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",
"ergebnis/composer-normalize": "^2.28.3",
"mockery/mockery": "^1.5",
"php-parallel-lint/php-console-highlighter": "^1.0",
Expand Down Expand Up @@ -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>
6 changes: 6 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ 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::.*#'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@greg0ire this will throw an error when the method does exists (i,e in DBAL v4)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Either set reportUnmatchedErrors to false, or use different configs for different jobs, or run static analysis with only one version of DBAL.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just did that ;)

Thanks!


# Necessary type hint for testing methods that no longer exist in the parent class with DBAL 4
- message: '#Method.*getType.*should return .*#'
44 changes: 44 additions & 0 deletions src/GetBindingTypeImplementation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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
{
public function getBindingType(): ParameterType
{
return ParameterType::BINARY;
}
}
} else {
/**
* @internal
*/
trait GetBindingTypeImplementation
{
public function getBindingType(): int
{
return ParameterType::BINARY;
}
}
}
53 changes: 34 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,7 +111,9 @@ 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);
}

public function getName(): string
Expand All @@ -118,11 +126,6 @@ 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 +161,17 @@ 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,
) :
ConversionException::conversionFailedFormat(
$value->toString(),
self::NAME,
self::ASSERT_FORMAT,
);
}
}

Expand All @@ -184,11 +193,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
20 changes: 11 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,7 +101,9 @@ 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);
}

public function getName(): string
Expand All @@ -108,9 +115,4 @@ public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}

public function getBindingType(): int
{
return ParameterType::BINARY;
}
}
10 changes: 8 additions & 2 deletions src/UuidType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\GuidType;
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 Down Expand Up @@ -53,7 +55,9 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?UuidInte
try {
$uuid = Uuid::fromString($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 @@ -81,7 +85,9 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
return (string) $value;
}

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

public function getName(): string
Expand Down
5 changes: 3 additions & 2 deletions tests/UuidBinaryOrderedTimeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Ramsey\Uuid\Doctrine;

use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
Expand Down Expand Up @@ -39,7 +40,7 @@ private function getPlatform(): MockInterface
return $platform;
}

private function getType(): Type
private function getType(): UuidBinaryOrderedTimeType
{
return Type::getType('uuid_binary_ordered_time');
}
Expand Down Expand Up @@ -142,6 +143,6 @@ public function testRequiresSQLCommentHint(): void

public function testItReturnsAppropriateBindingType(): void
{
$this->assertEquals(16, $this->getType()->getBindingType());
$this->assertEquals(ParameterType::BINARY, $this->getType()->getBindingType());
}
}
5 changes: 3 additions & 2 deletions tests/UuidBinaryTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Ramsey\Uuid\Doctrine;

use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
Expand Down Expand Up @@ -41,7 +42,7 @@ private function getPlatform(): MockInterface
return $platform;
}

private function getType(): Type
private function getType(): UuidBinaryType
{
return Type::getType('uuid_binary');
}
Expand Down Expand Up @@ -140,6 +141,6 @@ public function testRequiresSQLCommentHint(): void

public function testItReturnsAppropriateBindingType(): void
{
$this->assertEquals(16, $this->getType()->getBindingType());
$this->assertEquals(ParameterType::BINARY, $this->getType()->getBindingType());
}
}
2 changes: 1 addition & 1 deletion tests/UuidTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private function getPlatform(): MockInterface
return $platform;
}

private function getType(): Type
private function getType(): UuidType
{
return Type::getType('uuid');
}
Expand Down