Skip to content

Commit

Permalink
Merge pull request #9 from veewee/covariant-templates
Browse files Browse the repository at this point in the history
Make ISO / Lens templates covariant
  • Loading branch information
veewee authored Jun 6, 2024
2 parents caccab1 + d663dd0 commit 18198a4
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/Iso/Iso.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use function Psl\Result\wrap;

/**
* @template S
* @template A
* @template-covariant S
* @template-covariant A
*
* @psalm-immutable
* @psalm-suppress ImpureFunctionCall
Expand Down
4 changes: 2 additions & 2 deletions src/Iso/IsoInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use VeeWee\Reflecta\Lens\LensInterface;

/**
* @template S
* @template A
* @template-covariant S
* @template-covariant A
*
* @psalm-immutable
*/
Expand Down
9 changes: 4 additions & 5 deletions src/Iso/object_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@

/**
* @template S of object
* @template A of array<string, mixed>
* @param class-string<S> $className
* @return Iso<S, A>
* @return Iso<S, array<string, mixed>>
* @psalm-pure
*/
function object_data(string $className): Iso
{
/** @var Lens<S, A> $propertiesLens */
/** @var Lens<S, array<string, mixed>> $propertiesLens */
$propertiesLens = properties();

return new Iso(
/**
* @param S $object
* @return A
* @return array<string, mixed>
*/
static fn (object $object): array => $propertiesLens->get($object),
/**
* @param A $properties
* @param array<string, mixed> $properties
* @return S
*/
static fn (array $properties): object => $propertiesLens->set(
Expand Down
4 changes: 2 additions & 2 deletions src/Lens/Lens.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use function Psl\Result\wrap;

/**
* @template S
* @template A
* @template-covariant S
* @template-covariant A
*
* @psalm-immutable
* @psalm-suppress ImpureFunctionCall
Expand Down
4 changes: 2 additions & 2 deletions src/Lens/LensInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Psl\Result\ResultInterface;

/**
* @template S
* @template A
* @template-covariant S
* @template-covariant A
*
* @psalm-immutable
*/
Expand Down
21 changes: 3 additions & 18 deletions src/Lens/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,15 @@
use function VeeWee\Reflecta\ArrayAccess\index_set;

/**
* @template S of array
* @template A
* @param array-key $index
* @return Lens<S, A>
* @return Lens<array, mixed>
* @psalm-pure
*/
function index(string|int $index): Lens
function index($index): Lens
{
/** @return Lens<S, A> */
/** @return Lens<array, mixed> */
return new Lens(
/**
* @param S $subject
* @return A
*
* @psalm-suppress MixedInferredReturnType, MixedReturnStatement
*/
static fn (array $subject): mixed => index_get($subject, $index),
/**
* @param S $subject
* @param A $value
* @return S
*
* @psalm-suppress InvalidReturnType, InvalidReturnStatement
*/
static fn (array $subject, mixed $value): array => index_set($subject, $index, $value),
);
}
7 changes: 1 addition & 6 deletions src/Lens/property.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@

/**
* @template S of object
* @template A
* @return Lens<S, A>
* @return Lens<S, mixed>
* @psalm-pure
*/
function property(string $propertyName): Lens
{
return new Lens(
/**
* @param S $subject
* @return A
*
* @psalm-suppress MixedInferredReturnType, MixedReturnStatement
*/
static fn (object $subject): mixed => property_get($subject, $propertyName),
/**
* @param S $subject
* @param A $value
* @return S
*/
static fn (object $subject, mixed $value): object => property_set($subject, $propertyName, $value),
Expand Down
2 changes: 2 additions & 0 deletions src/Reflect/property_set.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
*
* @template T of object
* @param T $object
* @psalm-param mixed $value
* @return T
*/
function property_set(object $object, string $name, mixed $value): object
{
try {
/** @var T $new */
$new = clone $object;
} catch (Throwable $previous) {
throw CloneException::impossibleToClone($object, $previous);
Expand Down
3 changes: 2 additions & 1 deletion tests/static-analyzer/Iso/compose.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ function it_knows_composed_result(Iso $iso1, Iso $iso2, Iso $iso3): Iso
* @param Iso<C,D> $iso3
* @return Iso<A,D>
*
* @psalm-suppress InvalidArgument
* @ psalm-suppress InvalidArgument -
* TODO : Invalid compose iso-param detection does not work any since contravariant templates are introduced.
*/
function it_knows_broken_composition(Iso $iso1, Iso $iso2, Iso $iso3): Iso
{
Expand Down
3 changes: 2 additions & 1 deletion tests/static-analyzer/Lens/compose.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ function it_knows_composed_result(Lens $lens1, Lens $lens2, Lens $lens3): Lens
* @param Lens<C,D> $lens3
* @return Lens<A,D>
*
* @psalm-suppress InvalidArgument
* @ psalm-suppress InvalidArgument -
* TODO : Invalid compose lens-param detection does not work any since contravariant templates are introduced.
*/
function it_knows_broken_composition(Lens $lens1, Lens $lens2, Lens $lens3): Lens
{
Expand Down

0 comments on commit 18198a4

Please sign in to comment.