Skip to content

Commit

Permalink
Add type hints for uuid & ulid types
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed Jun 15, 2022
1 parent 78487d8 commit 3e1036f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
use Symfony\Bundle\MakerBundle\Doctrine\RelationOneToMany;
use Symfony\Bundle\MakerBundle\Doctrine\RelationOneToOne;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;

/**
* @internal
Expand Down Expand Up @@ -94,6 +96,8 @@ public function addEntityField(string $propertyName, array $columnOptions, array
$defaultValue = null;
if ('array' === $typeHint) {
$defaultValue = new Node\Expr\Array_([], ['kind' => Node\Expr\Array_::KIND_SHORT]);
} elseif (\in_array($typeHint, [Uuid::class, Ulid::class], true)) {
$typeHint = $this->addUseStatementIfNecessary($typeHint);
}

$this->addProperty(
Expand Down Expand Up @@ -1030,6 +1034,8 @@ private function getEntityTypeHint(string $doctrineType): ?string
'datetime_immutable', 'datetimetz_immutable', 'date_immutable', 'time_immutable' => '\\'.\DateTimeImmutable::class,
'dateinterval' => '\\'.\DateInterval::class,
'object' => 'object',
'uuid' => Uuid::class,
'ulid' => Ulid::class,
default => null,
};
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Util/ClassSourceManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,24 @@ public function getAddEntityFieldTests(): \Generator
],
'User_simple_object.php',
];

yield 'entity_add_uuid' => [
'User_simple.php',
'uuid',
[
'type' => 'uuid',
],
'User_simple_uuid.php',
];

yield 'entity_add_ulid' => [
'User_simple.php',
'ulid',
[
'type' => 'ulid',
],
'User_simple_ulid.php',
];
}

/**
Expand Down
35 changes: 35 additions & 0 deletions tests/Util/fixtures/add_entity_field/User_simple_ulid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Ulid;

#[ORM\Entity]
class User
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;

#[ORM\Column(type: 'ulid')]
private $ulid;

public function getId(): ?int
{
return $this->id;
}

public function getUlid(): ?Ulid
{
return $this->ulid;
}

public function setUlid(Ulid $ulid): self
{
$this->ulid = $ulid;

return $this;
}
}
35 changes: 35 additions & 0 deletions tests/Util/fixtures/add_entity_field/User_simple_uuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity]
class User
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;

#[ORM\Column(type: 'uuid')]
private $uuid;

public function getId(): ?int
{
return $this->id;
}

public function getUuid(): ?Uuid
{
return $this->uuid;
}

public function setUuid(Uuid $uuid): self
{
$this->uuid = $uuid;

return $this;
}
}

0 comments on commit 3e1036f

Please sign in to comment.