Skip to content

Commit 8849c6c

Browse files
authored
Merge pull request #1 from lhellemons/bugfix/value-object-superclass-constructor
ValueObjectTrait constructor bugfix
2 parents 81f8f93 + 97ec62a commit 8849c6c

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/Value/ValueObjectTrait.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ trait ValueObjectTrait
2424
/** @var Ref[] */
2525
static private $instances = [];
2626

27-
abstract protected function __construct(...$arguments);
28-
2927
final protected static function getInstance(...$values): self
3028
{
3129
$key = calculateKey(static::class, ...$values);

test/Value/ValueObjectTraitTest.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ public function testInheritance(): void
5757
$this->assertNotSame($subclassB11,$subclassC11);
5858
}
5959

60+
public function testInheritanceWithoutParentConstructor(): void
61+
{
62+
try {
63+
SubclassWithConstructorType::fromFoo('1');
64+
$this->expectNotToPerformAssertions();
65+
} catch (\Throwable $e) {
66+
$this->fail(sprintf('Unable to instantiate value object subclass without parent constructor: %s', $e));
67+
}
68+
}
69+
6070
public function testNoMutation(): void
6171
{
6272
$testObject = FromValuesType::fromFooAndBar('foo', 1);
@@ -136,8 +146,8 @@ public function getCasesForValueTypes(): array
136146
$stdClassB = new \stdClass();
137147
$stdClassB->foo = 'bar';
138148

139-
$objectA = new Object();
140-
$objectB = new Object();
149+
$objectA = new ObjectClass();
150+
$objectB = new ObjectClass();
141151

142152
$resourceA = fopen(__DIR__ . '/test_resource', 'r');
143153
$resourceB = fopen(__DIR__ . '/test_resource', 'r');
@@ -227,7 +237,7 @@ public function getValue()
227237
}
228238
}
229239

230-
class Object {}
240+
class ObjectClass {}
231241

232242
class FromValuesType
233243
{
@@ -363,3 +373,31 @@ public function getBarC(): int
363373
return $this->barC;
364374
}
365375
}
376+
377+
378+
379+
abstract class SuperclassWithoutConstructorType
380+
{
381+
use ValueObjectTrait;
382+
}
383+
384+
class SubclassWithConstructorType extends SuperclassWithoutConstructorType
385+
{
386+
/** @var string */
387+
private $foo;
388+
389+
protected function __construct(string $foo)
390+
{
391+
$this->foo = $foo;
392+
}
393+
394+
public static function fromFoo(string $foo): self
395+
{
396+
return self::getInstance($foo);
397+
}
398+
399+
public function getFoo(): string
400+
{
401+
return $this->foo;
402+
}
403+
}

0 commit comments

Comments
 (0)