Skip to content

Commit

Permalink
Simplify Type internal codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Nov 18, 2023
1 parent cb073b6 commit 6c21785
Showing 1 changed file with 6 additions and 24 deletions.
30 changes: 6 additions & 24 deletions src/Serializer/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace League\Csv\Serializer;

use DateTimeInterface;

use ReflectionClass;
use ReflectionNamedType;
use ReflectionParameter;
Expand Down Expand Up @@ -43,7 +42,7 @@ enum Type: string
case Array = 'array';
case Iterable = 'iterable';
case Enum = 'enum';
case Date = 'date';
case Date = DateTimeInterface::class;

public function equals(mixed $value): bool
{
Expand Down Expand Up @@ -99,14 +98,6 @@ public static function list(ReflectionParameter|ReflectionProperty $reflectionPr
$reflectionProperty instanceof ReflectionProperty => 'The property `'.$reflectionProperty->getName().'` must be typed.',
});

return self::typeList($reflectionType);
}

/**
* @return list<array{0:Type, 1: ReflectionNamedType}>
*/
private static function typeList(ReflectionType $reflectionType): array
{
$foundTypes = static function (array $res, ReflectionType $reflectionType) {
if (!$reflectionType instanceof ReflectionNamedType) {
return $res;
Expand All @@ -120,20 +111,11 @@ private static function typeList(ReflectionType $reflectionType): array
return $res;
};

if ($reflectionType instanceof ReflectionNamedType) {
$type = self::tryFromName($reflectionType->getName());
if (null !== $type) {
return [[$type, $reflectionType]];
}

return [];
}

if ($reflectionType instanceof ReflectionUnionType) {
return array_reduce($reflectionType->getTypes(), $foundTypes, []);
}

return [];
return match (true) {
$reflectionType instanceof ReflectionNamedType => $foundTypes([], $reflectionType),
$reflectionType instanceof ReflectionUnionType => array_reduce($reflectionType->getTypes(), $foundTypes, []),
default => [],
};
}

public static function tryFromReflectionType(ReflectionType $type): ?self
Expand Down

0 comments on commit 6c21785

Please sign in to comment.