8000 remove type hint from DomainId::equals() (#383) · msgphp/msgphp@c88013a · GitHub
[go: up one dir, main page]

Skip to content

Commit c88013a

Browse files
authored
remove type hint from DomainId::equals() (#383)
1 parent 28f9646 commit c88013a

File tree

6 files changed

+56
-30
lines changed

6 files changed

+56
-30
lines changed

docs/ddd/identifiers.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Tells if an identifier value is considered empty, thus has no known primitive va
1717

1818
---
1919

20-
### `equals(DomainId $id): bool`
20+
### `equals($other): bool`
2121

2222
Tells if an identifier strictly equals another identifier.
2323

@@ -32,7 +32,7 @@ be returned.
3232

3333
### `MsgPhp\Domain\DomainIdTrait`
3434

35-
A first-class citizen domain identifier trait compatible with any `scalar` value.
35+
A first-class citizen domain identifier trait compatible with any string or numeric value.
3636

3737
#### Basic Example
3838

@@ -57,12 +57,12 @@ $emptyId = new MyDomainId();
5757
$id->isEmpty(); // false
5858
$emptyId->isEmpty(); // true
5959

60-
$id->equals(new MyDomainId('1')); // true
61-
$emptyId->equals(new MyDomainId()); // false
62-
$emptyId->equals($emptyId); // true
63-
6460
$id->toString(); // "1"
6561
$emptyId->toString(); // ""
62+
63+
$id->equals(new MyDomainId('1')); // true
64+
$id->equals('1'); // false
65+
$id->equals(new MyDomainId('2')); // false
6666
```
6767

6868
### `MsgPhp\Domain\Infrastructure\Uuid\DomainIdTrait`

src/Domain/DomainId.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ public static function fromValue($value): self;
2323

2424
public function isEmpty(): bool;
2525

26-
public function equals(self $id): bool;
26+
/**
27+
* @param mixed $other
28+
*/
29+
public function equals($other): bool;
2730

2831
public function toString(): string;
2932
}

src/Domain/DomainIdTrait.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,31 @@ public function __toString(): string
3636
*/
3737
public static function fromValue($value): DomainId
3838
{
39-
if (null !== $value && !\is_string($value)) {
40-
$value = (string) $value;
39+
if (null === $value || \is_string($value)) {
40+
return new static($value);
41+
}
42+
if (is_numeric($value)) {
43+
return new static((string) $value);
4144
}
4245

43-
return new static($value);
46+
throw new \LogicException('Raw ID value must be of type string or number, got "'.\gettype($value).'".');
4447
}
4548

4649
public function isEmpty(): bool
4750
{
4851
return null === $this->id;
4952
}
5053

51-
public function equals(DomainId $id): bool
54+
/**
55+
* @param mixed $other
56+
*/
57+
public function equals($other): bool
5258
{
53-
if ($id === $this) {
54-
return true;
55-
}
56-
57-
if (null === $this->id || $id->isEmpty() || static::class !== \get_class($id)) {
59+
if (!$other instanceof self || static::class !== \get_class($other)) {
5860
return false;
5961
}
6062

61-
return $this->id === $id->toString();
63+
return $this->id === $other->id;
6264
}
6365

6466
public function toString(): string

src/Domain/Infrastructure/Uuid/DomainIdTrait.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,31 @@ public function __toString(): string
3636
*/
3737
public static function fromValue($value): DomainId
3838
{
39-
if (null !== $value && !$value instanceof UuidInterface) {
40-
$value = Uuid::fromString((string) $value);
39+
if (null === $value || $value instanceof UuidInterface) {
40+
return new static($value);
41+
}
42+
if (\is_string($value)) {
43+
return new static(Uuid::fromString($value));
4144
}
4245

43-
return new static($value);
46+
throw new \LogicException('Raw UUID value must be of type string, got "'.\gettype($value).'".');
4447
}
4548

4649
public function isEmpty(): bool
4750
{
4851
return false;
4952
}
5053

51-
public function equals(DomainId $id): bool
54+
/**
55+
* @param mixed $other
56+
*/
57+
public function equals($other): bool
5258
{
53-
if ($id === $this) {
54-
return true;
55-
}
56-
57-
if (static::class !== \get_class($id)) {
59+
if (!$other instanceof self || static::class !== \get_class($other)) {
5860
return false;
5961
}
6062

61-
return $this->uuid->equals(Uuid::fromString($id->toString()));
63+
return $this->uuid->equals($other->uuid);
6264
}
6365

6466
public function toString(): string

src/Domain/Tests/DomainIdTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ public function testFromValue(): void
2525
self::assertInstanceOf(TestOtherDomainId::class, TestOtherDomainId::fromValue(null));
2626
}
2727

28+
public function testFromInvalidValue(): void
29+
{
30+
$this->expectException(\LogicException::class);
31+
32+
TestDomainId::fromValue(true);
33+
}
34+
2835
public function testEmptyIdValue(): void
2936
{
3037
$this->expectException(\LogicException::class);
@@ -49,9 +56,13 @@ public function testEquals(): void
4956
self::assertTrue($id->equals(new TestDomainId('foo')));
5057
self::assertFalse($id->equals($emptyId));
5158
self::assertFalse($id->equals(new TestOtherDomainId('foo')));
59+
self::assertFalse($id->equals('foo'));
60+
self::assertFalse($id->equals(new \stdClass()));
5261
self::assertTrue($emptyId->equals($emptyId));
53-
self::assertFalse($emptyId->equals(new TestDomainId()));
62+
self::assertTrue($emptyId->equals(new TestDomainId()));
5463
self::assertFalse($emptyId->equals(new TestOtherDomainId()));
64+
self::assertFalse($emptyId->equals(''));
65+
self::assertFalse($emptyId->equals(new \stdClass()));
5566
}
5667

5768
/**

src/Domain/Tests/Infrastructure/Uuid/DomainIdTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use MsgPhp\Domain\Tests\Fixtures\TestDomainUuid;
88
use MsgPhp\Domain\Tests\Fixtures\TestOtherDomainUuid;
99
use PHPUnit\Framework\TestCase;
10-
use Ramsey\Uuid\Exception\InvalidUuidStringException;
1110
use Ramsey\Uuid\Uuid;
1211
use Ramsey\Uuid\UuidInterface;
1312

@@ -31,11 +30,18 @@ public function testFromValue(): void
3130

3231
public function testFromValueWithInvalidUuid(): void
3332
{
34-
$this->expectException(InvalidUuidStringException::class);
33+
$this->expectException(\LogicException::class);
3534

3635
TestDomainUuid::fromValue('00000000-0000-0000-0000-00000000000');
3736
}
3837

38+
public function testFromInvalidValue(): void
39+
{
40+
$this->expectException(\LogicException::class);
41+
42+
TestDomainUuid::fromValue(true);
43+
}
44+
3945
public function testIsEmpty(): void
4046
{
4147
self::assertFalse((new TestDomainUuid())->isEmpty());
@@ -50,6 +56,8 @@ public function testEquals(): void
5056
self::assertTrue($id->equals(new TestDomainUuid(Uuid::fromString('00000000-0000-0000-0000-000000000000'))));
5157
self::assertFalse($id->equals(new TestDomainUuid()));
5258
self::assertFalse($id->equals(new TestOtherDomainUuid(Uuid::fromString('00000000-0000-0000-0000-000000000000'))));
59+
self::assertFalse($id->equals('00000000-0000-0000-0000-000000000000'));
60+
self::assertFalse($id->equals(new \stdClass()));
5361
}
5462

5563
public function testToString(): void

0 commit comments

Comments
 (0)
0