Skip to content

Commit

Permalink
Merge pull request #70 from sasezaki/annotate_template_hydrate
Browse files Browse the repository at this point in the history
Add template annotation into hydrate() method & HydrateEvent
  • Loading branch information
weierophinney authored Aug 2, 2021
2 parents 5fe3d0e + 802eeb0 commit ed5d9ca
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/Aggregate/HydrateEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/**
* Event triggered when the {@see AggregateHydrator} hydrates
* data into an object
*
* @template T of object
*/
class HydrateEvent extends Event
{
Expand All @@ -19,14 +21,18 @@ class HydrateEvent extends Event
*/
protected $name = self::EVENT_HYDRATE;

/** @var object */
/**
* @var object
* @psalm-var T
*/
protected $hydratedObject;

/** @var mixed[] Data being used to hydrate the $hydratedObject */
protected $hydrationData;

/**
* @param mixed[] $hydrationData Data being used to hydrate the $hydratedObject
* @psalm-param T $hydratedObject
*/
public function __construct(object $target, object $hydratedObject, array $hydrationData)
{
Expand All @@ -38,12 +44,17 @@ public function __construct(object $target, object $hydratedObject, array $hydra

/**
* Retrieves the object that is being hydrated
*
* @psalm-return T
*/
public function getHydratedObject(): object
{
return $this->hydratedObject;
}

/**
* @psalm-param T $hydratedObject
*/
public function setHydratedObject(object $hydratedObject): void
{
$this->hydratedObject = $hydratedObject;
Expand Down
3 changes: 3 additions & 0 deletions src/HydrationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ interface HydrationInterface
* @return object The implementation should return an object of any type.
* By purposely omitting the return type from the signature,
* implementations may choose to specify a more specific type.
* @psalm-param T $object
* @psalm-return T
* @template T of object
*/
public function hydrate(array $data, object $object);
}
1 change: 1 addition & 0 deletions test/Aggregate/HydratorListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function testOnHydrate(): void
->will($this->returnValue($hydrated));
$event->expects($this->once())->method('setHydratedObject')->with($hydrated);

/** @psalm-suppress MixedArgumentTypeCoercion */
$this->assertSame($hydrated, $this->listener->onHydrate($event));
}

Expand Down
10 changes: 5 additions & 5 deletions test/ClassMethodsHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ public function testCanExtractFromMethodsWithOptionalParameters(): void
*/
public function testCanHydratedPromiscuousInstances(): void
{
/** @var ClassMethodsCamelCase $classMethodsCamelCase */
$classMethodsCamelCase = $this->hydrator->hydrate(
$classMethodsCamelCase = $this->hydrator->hydrate(
['fooBar' => 'baz-tab'],
new ClassMethodsCamelCase()
);
/** @var ClassMethodsCamelCaseMissing $classMethodsCamelCaseMissing */
$classMethodsCamelCaseMissing = $this->hydrator->hydrate(
['fooBar' => 'baz-tab'],
new ClassMethodsCamelCaseMissing()
);
/** @var ArraySerializable $arraySerializable */
$arraySerializable = $this->hydrator->hydrate(['fooBar' => 'baz-tab'], new ArraySerializable());
$arraySerializable = $this->hydrator->hydrate(
['fooBar' => 'baz-tab'],
new ArraySerializable()
);

$this->assertSame('baz-tab', $classMethodsCamelCase->getFooBar());
$this->assertSame('baz-tab', $classMethodsCamelCaseMissing->getFooBar());
Expand Down

0 comments on commit ed5d9ca

Please sign in to comment.