-
Notifications
You must be signed in to change notification settings - Fork 110
Add support for symfony uuid #689
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Type/Doctrine/Descriptors/Symfony/UlidTypeDescriptor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine\Descriptors\Symfony; | ||
|
||
use PHPStan\Type\Doctrine\Descriptors\DoctrineTypeDescriptor; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use Symfony\Component\Uid\Ulid; | ||
|
||
class UlidTypeDescriptor implements DoctrineTypeDescriptor | ||
{ | ||
|
||
/** @var class-string<\Doctrine\DBAL\Types\Type> */ | ||
private string $uuidTypeName; | ||
|
||
/** | ||
* @param class-string<\Doctrine\DBAL\Types\Type> $uuidTypeName | ||
*/ | ||
public function __construct(string $uuidTypeName) | ||
{ | ||
$this->uuidTypeName = $uuidTypeName; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return $this->uuidTypeName; | ||
} | ||
|
||
public function getWritableToPropertyType(): Type | ||
{ | ||
return new ObjectType(Ulid::class); | ||
} | ||
|
||
public function getWritableToDatabaseType(): Type | ||
{ | ||
return TypeCombinator::union( | ||
new StringType(), | ||
new ObjectType(Ulid::class), | ||
); | ||
} | ||
|
||
public function getDatabaseInternalType(): Type | ||
{ | ||
return new StringType(); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/Type/Doctrine/Descriptors/Symfony/UuidTypeDescriptor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine\Descriptors\Symfony; | ||
|
||
use PHPStan\Type\Doctrine\Descriptors\DoctrineTypeDescriptor; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
class UuidTypeDescriptor implements DoctrineTypeDescriptor | ||
{ | ||
|
||
/** @var class-string<\Doctrine\DBAL\Types\Type> */ | ||
private string $uuidTypeName; | ||
|
||
/** | ||
* @param class-string<\Doctrine\DBAL\Types\Type> $uuidTypeName | ||
*/ | ||
public function __construct(string $uuidTypeName) | ||
{ | ||
$this->uuidTypeName = $uuidTypeName; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return $this->uuidTypeName; | ||
} | ||
|
||
public function getWritableToPropertyType(): Type | ||
{ | ||
return new ObjectType(Uuid::class); | ||
} | ||
|
||
public function getWritableToDatabaseType(): Type | ||
{ | ||
return TypeCombinator::union( | ||
new StringType(), | ||
new ObjectType(Uuid::class), | ||
); | ||
} | ||
|
||
public function getDatabaseInternalType(): Type | ||
{ | ||
return new StringType(); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
* From https://github.com/ramsey/uuid-doctrine/blob/fafebbe972cdaba9274c286ea8923e2de2579027/src/UuidType.php | ||
* Copyright (c) 2012-2022 Ben Ramsey <[email protected]> | ||
*/ | ||
final class FakeTestingUuidType extends GuidType | ||
final class FakeTestingRamseyUuidType extends GuidType | ||
{ | ||
|
||
public const NAME = 'uuid'; | ||
|
113 changes: 113 additions & 0 deletions
113
tests/Rules/Doctrine/ORM/FakeTestingSymfonyUlidType.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Doctrine\ORM; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\ConversionException; | ||
use Doctrine\DBAL\Types\Type; | ||
use InvalidArgumentException; | ||
use Symfony\Component\Uid\AbstractUid; | ||
use Symfony\Component\Uid\Ulid; | ||
use function is_string; | ||
|
||
/** | ||
* From https://github.com/symfony/doctrine-bridge/blob/f5a2780640342e3bf0599fbfc9b3dd759db9b037/Types/UuidType.php | ||
* Copyright (c) Fabien Potencier <[email protected]> | ||
*/ | ||
final class FakeTestingSymfonyUlidType extends Type | ||
{ | ||
|
||
public const NAME = 'ulid'; | ||
|
||
/** | ||
* @not-deprecated | ||
*/ | ||
public function getName(): string | ||
{ | ||
return self::NAME; | ||
} | ||
|
||
protected function getUidClass(): string | ||
{ | ||
return Ulid::class; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string | ||
{ | ||
if ($this->hasNativeGuidType($platform)) { | ||
return $platform->getGuidTypeDeclarationSQL($column); | ||
} | ||
|
||
return $platform->getBinaryTypeDeclarationSQL([ | ||
'length' => '16', | ||
'fixed' => true, | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws ConversionException | ||
*/ | ||
public function convertToPHPValue($value, AbstractPlatform $platform): ?AbstractUid | ||
{ | ||
if ($value instanceof AbstractUid || $value === null) { | ||
return $value; | ||
} | ||
|
||
if (!is_string($value)) { | ||
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'string', AbstractUid::class]); | ||
} | ||
|
||
try { | ||
/** @phpstan-ignore-next-line method.dynamicName */ | ||
return $this->getUidClass()::fromString($value); | ||
} catch (InvalidArgumentException $e) { | ||
throw ConversionException::conversionFailed($value, $this->getName(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws ConversionException | ||
*/ | ||
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string | ||
{ | ||
$toString = $this->hasNativeGuidType($platform) ? 'toRfc4122' : 'toBinary'; | ||
|
||
if ($value instanceof AbstractUid) { | ||
/** @phpstan-ignore-next-line method.dynamicName */ | ||
return $value->$toString(); | ||
} | ||
|
||
if ($value === null || $value === '') { | ||
return null; | ||
} | ||
|
||
if (!is_string($value)) { | ||
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'string', AbstractUid::class]); | ||
} | ||
|
||
try { | ||
/** @phpstan-ignore-next-line method.dynamicName */ | ||
return $this->getUidClass()::fromString($value)->$toString(); | ||
} catch (InvalidArgumentException $e) { | ||
throw ConversionException::conversionFailed($value, $this->getName()); | ||
} | ||
} | ||
|
||
public function requiresSQLCommentHint(AbstractPlatform $platform): bool | ||
{ | ||
return true; | ||
} | ||
|
||
private function hasNativeGuidType(AbstractPlatform $platform): bool | ||
{ | ||
return $platform->getGuidTypeDeclarationSQL([]) !== $platform->getStringTypeDeclarationSQL(['fixed' => true, 'length' => 36]); | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.