Skip to content

Commit

Permalink
add symfony uuid type descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
alcohol committed Jan 21, 2025
1 parent bdb6a83 commit 6d4cc15
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
"phpstan/phpstan-strict-rules": "^2.0",
"phpunit/phpunit": "^9.6.20",
"ramsey/uuid": "^4.2",
"symfony/cache": "^5.4"
"symfony/cache": "^5.4",
"symfony/doctrine-bridge": "^5.4",
"symfony/validator": "^5.4",
"symfony/uid": "^5.4"
},
"config": {
"sort-packages": true,
Expand Down
3 changes: 3 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,9 @@ services:
tags: [phpstan.doctrine.typeDescriptor]
arguments:
uuidTypeName: Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType
-
class: PHPStan\Type\Doctrine\Descriptors\Symfony\UuidTypeDescriptor
tags: [phpstan.doctrine.typeDescriptor]

# Doctrine Collection
-
Expand Down
35 changes: 35 additions & 0 deletions src/Type/Doctrine/Descriptors/Symfony/UuidTypeDescriptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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 Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Uid\Uuid;

class UuidTypeDescriptor implements DoctrineTypeDescriptor
{

public function getType(): string
{
return UuidType::class;
}

public function getWritableToPropertyType(): Type
{
return new ObjectType(Uuid::class);
}

public function getWritableToDatabaseType(): Type
{
return new ObjectType(Uuid::class);
}

public function getDatabaseInternalType(): Type
{
return new StringType();
}

}
4 changes: 4 additions & 0 deletions tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ public function testRule(?string $objectManagerLoader): void
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$invalidSimpleArray type mapping mismatch: property can contain array<int> but database expects array<string>.',
162,
],
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$symfonyUuid type mapping mismatch: database can contain Symfony\Component\Uid\Uuid but property expects string.',
174,
],
];

$dbalVersion = InstalledVersions::getVersion('doctrine/dbal');
Expand Down
6 changes: 6 additions & 0 deletions tests/Rules/Doctrine/ORM/data/MyBrokenEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,10 @@ class MyBrokenEntity extends MyBrokenSuperclass
* @var list<string>
*/
private $validSimpleArray;

/**
* @ORM\Column(type="uuid")
* @var string
*/
private $symfonyUuid;
}

0 comments on commit 6d4cc15

Please sign in to comment.