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

feat: allow doctrine/dbal v4 #247

Merged
merged 1 commit into from
May 26, 2024
Merged
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.0",
"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>
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
52 changes: 51 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,54 @@
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
</plugins>

<issueHandlers>
<MethodSignatureMismatch>
<errorLevel type="suppress">
<file name="src/UuidBinaryOrderedTimeType.php" />
<file name="src/UuidBinaryType.php" />
<file name="src/UuidGenerator.php" />
<file name="src/UuidOrderedTimeGenerator.php" />
<file name="src/UuidType.php" />
<file name="src/UuidV7Generator.php" />
</errorLevel>
</MethodSignatureMismatch>
<MixedInferredReturnType>
<errorLevel type="suppress">
<file name="src/UuidBinaryOrderedTimeType.php" />
<file name="src/UuidBinaryType.php" />
</errorLevel>
</MixedInferredReturnType>
<MixedReturnStatement>
<errorLevel type="suppress">
<file name="src/UuidBinaryOrderedTimeType.php" />
<file name="src/UuidBinaryType.php" />
</errorLevel>
</MixedReturnStatement>
<UndefinedMethod>
<errorLevel type="suppress">
<referencedMethod name="Doctrine\DBAL\Types\ConversionException::conversionFailed" />
<referencedMethod name="Doctrine\DBAL\Types\ConversionException::conversionfailedformat" />
</errorLevel>
</UndefinedMethod>
<UndefinedClass>
<errorLevel type="suppress">
<file name="src/GetBindingTypeImplementation.php" />
<file name="src/UuidBinaryOrderedTimeType.php" />
<file name="src/UuidBinaryType.php" />
<file name="src/UuidType.php" />
</errorLevel>
</UndefinedClass>
<InvalidReturnType>
<errorLevel type="suppress">
<file name="src/GetBindingTypeImplementation.php" />
</errorLevel>
</InvalidReturnType>
<ImplementedReturnTypeMismatch>
<errorLevel type="suppress">
<file name="src/GetBindingTypeImplementation.php" />
</errorLevel>
</ImplementedReturnTypeMismatch>

</issueHandlers>

</psalm>
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

Check warning on line 39 in src/GetBindingTypeImplementation.php

View check run for this annotation

Codecov / codecov/patch

src/GetBindingTypeImplementation.php#L39

Added line #L39 was not covered by tests
{
return ParameterType::BINARY;

Check warning on line 41 in src/GetBindingTypeImplementation.php

View check run for this annotation

Codecov / codecov/patch

src/GetBindingTypeImplementation.php#L41

Added line #L41 was not covered by tests
}
}
}
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
Loading