Skip to content

Commit

Permalink
fix: PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
autaut03 committed Nov 3, 2023
1 parent e7776c9 commit 9c8834e
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public function __construct(
) {}

/**
* @param TypeAdapterFactory<TypeAdapter<mixed, mixed>> $factory
* @template A of TypeAdapter<mixed, mixed>
*
* @param TypeAdapterFactory<A> $factory
*/
public function addFactory(TypeAdapterFactory $factory): self
{
Expand All @@ -32,7 +34,9 @@ public function addMapper(object $adapter): self
}

/**
* @param TypeAdapterFactory<TypeAdapter<mixed, mixed>> $factory
* @template A of TypeAdapter<mixed, mixed>
*
* @param TypeAdapterFactory<A> $factory
*/
public function addFactoryLast(TypeAdapterFactory $factory): self
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ public function __construct(
private readonly Collection $properties,
) {}

/**
* @param T $value
*/
public function serialize(mixed $value): mixed
{
return MultipleMappingException::map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace GoodPhp\Serialization\TypeAdapter\Primitive\MapperMethods\MapperMethod;

use GoodPhp\Reflection\Reflection\MethodReflection;
use GoodPhp\Reflection\Reflection\Methods\HasMethods;
use GoodPhp\Reflection\Type\Type;
use GoodPhp\Serialization\TypeAdapter\Primitive\MapperMethods\Acceptance\AcceptanceStrategy;
use GoodPhp\Serialization\TypeAdapter\Primitive\MapperMethods\Acceptance\BaseTypeEqualsAcceptanceStrategy;
Expand Down Expand Up @@ -40,6 +41,12 @@ public function createFrom(object $adapter, MethodReflection $method, MapFrom $m
);
}

/**
* @template AdapterType of object
*
* @param AdapterType $adapter
* @param MethodReflection<AdapterType, HasMethods<AdapterType>> $method
*/
public function create(
object $adapter,
MethodReflection $method,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GoodPhp\Reflection\Reflection\Attributes\Attributes;
use GoodPhp\Reflection\Reflection\FunctionParameterReflection;
use GoodPhp\Reflection\Reflection\MethodReflection;
use GoodPhp\Reflection\Reflection\Methods\HasMethods;
use GoodPhp\Reflection\Type\NamedType;
use GoodPhp\Reflection\Type\Type;
use GoodPhp\Serialization\Serializer;
Expand All @@ -14,8 +15,15 @@
use TypeError;
use Webmozart\Assert\Assert;

/**
* @template AdapterType of object
*/
final class InstanceMapperMethod implements MapperMethod
{
/**
* @param AdapterType $adapter
* @param MethodReflection<AdapterType, HasMethods<AdapterType>> $method
*/
public function __construct(
private readonly MethodReflection $method,
private readonly object $adapter,
Expand Down Expand Up @@ -59,7 +67,8 @@ public function invoke(mixed $value, Type $type, Attributes $attributes, Seriali
throw $e;
}

throw new UnexpectedTypeException($value, $this->method->parameters()->first()->type());
/* @phpstan-ignore-next-line argument.type */
throw new UnexpectedTypeException($value, $this->method->parameters()->firstOrFail()->type());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@
namespace GoodPhp\Serialization\TypeAdapter\Primitive\MapperMethods\MapperMethod;

use GoodPhp\Reflection\Reflection\MethodReflection;
use GoodPhp\Reflection\Reflection\Methods\HasMethods;
use GoodPhp\Serialization\TypeAdapter\Primitive\MapperMethods\MapFrom;
use GoodPhp\Serialization\TypeAdapter\Primitive\MapperMethods\MapTo;

interface MapperMethodFactory
{
/**
* @template AdapterType of object
*
* @param AdapterType $adapter
* @param MethodReflection<AdapterType, HasMethods<AdapterType>> $method
*/
public function createTo(
object $adapter,
MethodReflection $method,
MapTo $mapTo,
): MapperMethod;

/**
* @template AdapterType of object
*
* @param AdapterType $adapter
* @param MethodReflection<AdapterType, HasMethods<AdapterType>> $method
*/
public function createFrom(
object $adapter,
MethodReflection $method,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@
use GoodPhp\Serialization\TypeAdapter\Primitive\PrimitiveTypeAdapter;
use Webmozart\Assert\Assert;

/**
* @template T
*
* @implements PrimitiveTypeAdapter<T>
*/
final class MapperMethodsPrimitiveTypeAdapter implements PrimitiveTypeAdapter
{
/**
* @param PrimitiveTypeAdapter<T>|null $fallbackDelegate
*/
public function __construct(
private readonly ?MapperMethod $toMapper,
private readonly ?MapperMethod $fromMapper,
Expand All @@ -27,15 +35,23 @@ public function __construct(

public function serialize(mixed $value): mixed
{
return $this->toMapper ?
$this->toMapper->invoke($value, $this->type, $this->attributes, $this->serializer, $this->skipPast) :
$this->fallbackDelegate->serialize($value);
if (!$this->toMapper) {
Assert::notNull($this->fallbackDelegate);

return $this->fallbackDelegate->serialize($value);
}

return $this->toMapper->invoke($value, $this->type, $this->attributes, $this->serializer, $this->skipPast);
}

public function deserialize(mixed $value): mixed
{
return $this->fromMapper ?
$this->fromMapper->invoke($value, $this->type, $this->attributes, $this->serializer, $this->skipPast) :
$this->fallbackDelegate->deserialize($value);
if (!$this->fromMapper) {
Assert::notNull($this->fallbackDelegate);

return $this->fallbackDelegate->deserialize($value);
}

return $this->fromMapper->invoke($value, $this->type, $this->attributes, $this->serializer, $this->skipPast);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Webmozart\Assert\Assert;

/**
* @implements TypeAdapterFactory<MapperMethodsPrimitiveTypeAdapter>
* @implements TypeAdapterFactory<MapperMethodsPrimitiveTypeAdapter<mixed>>
*/
final class MapperMethodsPrimitiveTypeAdapterFactory implements TypeAdapterFactory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ public function create(object $adapter): MapperMethodsPrimitiveTypeAdapterFactor
->map(fn (MethodReflection $method) => $this->mapperMethodFactory->createTo(
$adapter,
$method,
/* @phpstan-ignore-next-line argument.type */
$method->attributes()->sole(MapTo::class),
)),
$reflection->methods()
->filter(fn (MethodReflection $method) => $method->attributes()->has(MapFrom::class))
->map(fn (MethodReflection $method) => $this->mapperMethodFactory->createFrom(
$adapter,
$method,
/* @phpstan-ignore-next-line argument.type */
$method->attributes()->sole(MapFrom::class)
)),
);
Expand Down
7 changes: 2 additions & 5 deletions src/TypeAdapter/Primitive/PhpStandard/ValueEnumMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ public function to(ValueEnum $value): string|int
}

/**
* @template TEnumValue of string|int
*
* @param TEnumValue $value
* @param NamedType $type
* @param NamedType $type
*
* @return ValueEnum<TEnumValue>
* @return ValueEnum<string|int>
*/
#[MapFrom(PrimitiveTypeAdapter::class, new BaseTypeAcceptedByAcceptanceStrategy(ValueEnum::class))]
public function from(string|int $value, Type $type): ValueEnum
Expand Down
2 changes: 1 addition & 1 deletion src/TypeAdapter/Primitive/PrimitiveTypeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @template T
*
* @implements TypeAdapter<T, mixed>
* @extends TypeAdapter<T, mixed>
*/
interface PrimitiveTypeAdapter extends TypeAdapter
{
Expand Down
2 changes: 1 addition & 1 deletion src/TypeAdapter/TypeAdapterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
interface TypeAdapterFactory
{
/**
* @param class-string<T> $typeAdapterType
* @param class-string<TypeAdapter<mixed, mixed>> $typeAdapterType
*
* @return T|null
*/
Expand Down

0 comments on commit 9c8834e

Please sign in to comment.