Skip to content

Commit

Permalink
More test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Jan 24, 2024
1 parent d70d4d5 commit 46e2d19
Show file tree
Hide file tree
Showing 30 changed files with 422 additions and 170 deletions.
3 changes: 2 additions & 1 deletion src/Attributes/MapName.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace Spatie\LaravelData\Attributes;

use Attribute;
use Spatie\LaravelData\Mappers\NameMapper;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)]
class MapName
{
public function __construct(public string|int $input, public string|int|null $output = null)
public function __construct(public string|int|NameMapper $input, public string|int|NameMapper|null $output = null)
{
$this->output ??= $this->input;
}
Expand Down
8 changes: 0 additions & 8 deletions src/Concerns/BaseData.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,6 @@ public static function prepareForPipeline(array $properties): array
return $properties;
}

public function getMorphClass(): string
{
/** @var class-string<BaseDataContract> $class */
$class = static::class;

return app(DataConfig::class)->morphMap->getDataClassAlias($class) ?? $class;
}

public function __sleep(): array
{
$dataClass = app(DataConfig::class)->getDataClass(static::class);
Expand Down
25 changes: 25 additions & 0 deletions src/Concerns/EnumerableMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
trait EnumerableMethods
{
/**
* @deprecated In v5, use a regular Laravel collection instead
*
* @param callable(TValue, TKey): TValue $through
*
* @return static
Expand All @@ -27,6 +29,8 @@ public function through(callable $through): static
}

/**
* @deprecated In v5, use a regular Laravel collection instead
*
* @param callable(TValue, TKey): TValue $map
*
* @return static
Expand All @@ -37,6 +41,8 @@ public function map(callable $map): static
}

/**
* @deprecated In v5, use a regular Laravel collection instead
*
* @param callable(TValue): bool $filter
*
* @return static
Expand All @@ -51,6 +57,8 @@ public function filter(callable $filter): static
}

/**
* @deprecated In v5, use a regular Laravel collection instead
*
* @param callable(TValue): bool $filter
*
* @return static
Expand All @@ -65,6 +73,8 @@ public function reject(callable $filter): static
}

/**
* @deprecated In v5, use a regular Laravel collection instead
*
* @template TFirstDefault
*
* @param null| (callable(TValue,TKey): bool) $callback
Expand All @@ -78,6 +88,8 @@ public function first(callable|null $callback = null, $default = null)
}

/**
* @deprecated In v5, use a regular Laravel collection instead
*
* @template TLastDefault
*
* @param null| (callable(TValue,TKey): bool) $callback
Expand All @@ -91,6 +103,8 @@ public function last(callable|null $callback = null, $default = null)
}

/**
* @deprecated In v5, use a regular Laravel collection instead
*
* @param callable(TValue, TKey): mixed $callback
*
* @return static
Expand All @@ -103,6 +117,8 @@ public function each(callable $callback): static
}

/**
* @deprecated In v5, use a regular Laravel collection instead
*
* @return static<int, TValue>
*/
public function values(): static
Expand All @@ -114,6 +130,9 @@ public function values(): static
return $cloned;
}

/**
* @deprecated In v5, use a regular Laravel collection instead
*/
public function where(string $key, mixed $operator = null, mixed $value = null): static
{
$cloned = clone $this;
Expand All @@ -124,6 +143,8 @@ public function where(string $key, mixed $operator = null, mixed $value = null):
}

/**
* @deprecated In v5, use a regular Laravel collection instead
*
* @template TReduceInitial
* @template TReduceReturnType
*
Expand All @@ -138,6 +159,8 @@ public function reduce(callable $callback, mixed $initial = null)
}

/**
* @deprecated In v5, use a regular Laravel collection instead
*
* @param (callable(TValue, TKey): bool)|string|null $key
* @param mixed $operator
* @param mixed $value
Expand All @@ -153,6 +176,8 @@ public function sole(callable|string|null $key = null, mixed $operator = null, m
}

/**
*
*
* @param DataCollection $collection
*
* @return static
Expand Down
2 changes: 0 additions & 2 deletions src/Contracts/BaseData.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,4 @@ public static function normalizers(): array;
public static function prepareForPipeline(array $properties): array;

public static function pipeline(): DataPipeline;

public function getMorphClass(): string;
}
37 changes: 0 additions & 37 deletions src/Exceptions/DataMissingFeature.php

This file was deleted.

22 changes: 0 additions & 22 deletions src/Exceptions/InvalidDataClassMapper.php

This file was deleted.

5 changes: 0 additions & 5 deletions src/LaravelDataServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ public function packageRegistered(): void
fn () => $this->app->make(DataStructureCache::class)->getConfig() ?? DataConfig::createFromConfig(config('data'))
);

/** @psalm-suppress UndefinedInterfaceMethod */
$this->app->beforeResolving(BaseData::class, function ($class, $parameters, $app) {
if ($app->has($class)) {
return;
}

$app->bind(
$class,
fn ($container) => $class::from($container['request'])
Expand Down
4 changes: 0 additions & 4 deletions src/Mappers/CamelCaseMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ class CamelCaseMapper implements NameMapper
{
public function map(int|string $name): string|int
{
if (! is_string($name)) {
return $name;
}

return Str::camel($name);
}
}
5 changes: 0 additions & 5 deletions src/Mappers/ProvidedNameMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@ public function map(int|string $name): string|int
{
return $this->name;
}

public function inverse(): NameMapper
{
return $this;
}
}
4 changes: 0 additions & 4 deletions src/Mappers/SnakeCaseMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ class SnakeCaseMapper implements NameMapper
{
public function map(int|string $name): string|int
{
if (! is_string($name)) {
return $name;
}

return Str::snake($name);
}
}
4 changes: 0 additions & 4 deletions src/Mappers/StudlyCaseMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ class StudlyCaseMapper implements NameMapper
{
public function map(int|string $name): string|int
{
if (! is_string($name)) {
return $name;
}

return Str::studly($name);
}
}
4 changes: 0 additions & 4 deletions src/Resolvers/DataCollectableFromSomethingResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ public function execute(

$normalizedItems = $this->normalizeItems($items, $dataClass, $creationContext);

if(! $intoType->type instanceof NamedType) {
throw new Exception('Cannot collect into a union or intersection type');
}

return match ($intoType->kind) {
DataTypeKind::DataArray => $this->normalizeToArray($normalizedItems),
DataTypeKind::DataEnumerable => new $intoType->type->name($this->normalizeToArray($normalizedItems)),

Check failure on line 57 in src/Resolvers/DataCollectableFromSomethingResolver.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Spatie\LaravelData\Support\Types\Type::$name.

Check failure on line 57 in src/Resolvers/DataCollectableFromSomethingResolver.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Spatie\LaravelData\Support\Types\Type::$name.
Expand Down
8 changes: 6 additions & 2 deletions src/Resolvers/NameMappersResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected function resolveOutputNameMapper(
return null;
}

protected function resolveMapper(string|int $value): ?NameMapper
protected function resolveMapper(string|int|NameMapper $value): ?NameMapper
{
$mapper = $this->resolveMapperClass($value);

Expand All @@ -70,12 +70,16 @@ protected function resolveMapper(string|int $value): ?NameMapper
return $mapper;
}

protected function resolveMapperClass(int|string $value): NameMapper
protected function resolveMapperClass(int|string|NameMapper $value): NameMapper
{
if (is_int($value)) {
return new ProvidedNameMapper($value);
}

if($value instanceof NameMapper){
return $value;
}

if (is_a($value, NameMapper::class, true)) {
return resolve($value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Resolvers/RequestQueryStringPartialsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected function validateSegments(
): ?array {
$allowed = $type->getAllowedPartials($dataClass);

$segment = $partialSegments[0];
$segment = $partialSegments[0] ?? null;

if ($segment instanceof AllPartialSegment) {
if ($allowed === null || $allowed === ['*']) {
Expand Down
45 changes: 0 additions & 45 deletions src/Support/Annotations/DataCollectableAnnotationReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ class DataCollectableAnnotationReader
/** @var array<string, Context> */
protected static array $contexts = [];

public static function create(): self
{
return new self();
}

/** @return array<string, DataCollectableAnnotation> */
public function getForClass(ReflectionClass $class): array
{
Expand Down Expand Up @@ -172,46 +167,6 @@ protected function resolveDataClass(
return null;
}

protected function resolveCollectionClass(
ReflectionProperty|ReflectionClass|ReflectionMethod $reflection,
string $class
): ?string {
if (str_contains($class, '|')) {
foreach (explode('|', $class) as $explodedClass) {
if ($foundClass = $this->resolveCollectionClass($reflection, $explodedClass)) {
return $foundClass;
}
}

return null;
}

if ($class === 'array') {
return $class;
}

$class = ltrim($class, '\\');

if (is_a($class, BaseDataCollectable::class, true)
|| is_a($class, Enumerable::class, true)
|| is_a($class, AbstractPaginator::class, true)
|| is_a($class, CursorPaginator::class, true)
) {
return $class;
}

$class = $this->resolveFcqn($reflection, $class);

if (is_a($class, BaseDataCollectable::class, true)
|| is_a($class, Enumerable::class, true)
|| is_a($class, AbstractPaginator::class, true)
|| is_a($class, CursorPaginator::class, true)) {
return $class;
}

return null;
}

protected function resolveFcqn(
ReflectionProperty|ReflectionClass|ReflectionMethod $reflection,
string $class
Expand Down
9 changes: 1 addition & 8 deletions src/Support/DataClassMorphMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,8 @@ public function add(
/**
* @param array<string, class-string<BaseData>> $map
*/
public function merge(array|DataClassMorphMap $map): self
public function merge(array $map): self
{
if ($map instanceof DataClassMorphMap) {
$map->map = array_merge($this->map, $map->map);
$map->reversedMap = array_merge($this->reversedMap, $map->reversedMap);

return $this;
}

foreach ($map as $alias => $class) {
$this->add($alias, $class);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Support/Factories/DataClassFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ protected function resolveMethods(
): Collection {
return collect($reflectionClass->getMethods())
->filter(fn (ReflectionMethod $reflectionMethod) => str_starts_with($reflectionMethod->name, 'from') || str_starts_with($reflectionMethod->name, 'collect'))
->reject(fn (ReflectionMethod $reflectionMethod) => in_array($reflectionMethod->name, ['from', 'collect', 'collection']))
->reject(fn (ReflectionMethod $reflectionMethod) => in_array($reflectionMethod->name, ['from', 'collect', 'collection'])
|| $reflectionMethod->isStatic() === false
|| $reflectionMethod->isPublic() === false
)
->mapWithKeys(
fn (ReflectionMethod $reflectionMethod) => [$reflectionMethod->name => $this->methodFactory->build($reflectionMethod, $reflectionClass)],
);
Expand Down
Loading

0 comments on commit 46e2d19

Please sign in to comment.