Skip to content

Commit

Permalink
Fix phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed May 13, 2022
1 parent 405f0d4 commit 36f66f7
Show file tree
Hide file tree
Showing 21 changed files with 201 additions and 98 deletions.
46 changes: 38 additions & 8 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
parameters:
ignoreErrors:
-
message: "#^Unsafe usage of new static\\(\\)\\.$#"
count: 1
path: src/Casts/Uncastable.php

-
message: "#^Method Spatie\\\\LaravelData\\\\Data\\:\\:from\\(\\) should return static\\(Spatie\\\\LaravelData\\\\Data\\) but returns Spatie\\\\LaravelData\\\\Data\\.$#"
count: 1
path: src/Data.php

-
message: "#^Parameter \\#1 \\$callback of method Illuminate\\\\Support\\\\Collection\\<\\(int\\|string\\),mixed\\>\\:\\:mapWithKeys\\(\\) expects callable\\(mixed, \\(int\\|string\\)\\)\\: array, Closure\\(Spatie\\\\LaravelData\\\\Support\\\\DataProperty\\)\\: Illuminate\\\\Support\\\\Collection given\\.$#"
count: 1
path: src/Resolvers/DataValidationRulesResolver.php
message: "#^Readonly property Spatie\\\\LaravelData\\\\Support\\\\DataType\\:\\:\\$acceptedTypes is already assigned\\.$#"
count: 2
path: src/Support/DataType.php

-
message: "#^Readonly property Spatie\\\\LaravelData\\\\Support\\\\DataType\\:\\:\\$dataClass is already assigned\\.$#"
count: 2
path: src/Support/DataType.php

-
message: "#^Readonly property Spatie\\\\LaravelData\\\\Support\\\\DataType\\:\\:\\$isDataCollection is already assigned\\.$#"
count: 2
path: src/Support/DataType.php

-
message: "#^Readonly property Spatie\\\\LaravelData\\\\Support\\\\DataType\\:\\:\\$isDataObject is already assigned\\.$#"
count: 2
path: src/Support/DataType.php

-
message: "#^Readonly property Spatie\\\\LaravelData\\\\Support\\\\DataType\\:\\:\\$isLazy is already assigned\\.$#"
count: 2
path: src/Support/DataType.php

-
message: "#^Readonly property Spatie\\\\LaravelData\\\\Support\\\\DataType\\:\\:\\$isMixed is already assigned\\.$#"
count: 2
path: src/Support/DataType.php

-
message: "#^Readonly property Spatie\\\\LaravelData\\\\Support\\\\DataType\\:\\:\\$isNullable is already assigned\\.$#"
count: 2
path: src/Support/DataType.php

-
message: "#^Readonly property Spatie\\\\LaravelData\\\\Support\\\\DataType\\:\\:\\$isUndefinable is already assigned\\.$#"
count: 2
path: src/Support/DataType.php

4 changes: 2 additions & 2 deletions src/Casts/Uncastable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class Uncastable
{
public static function create(): static
public static function create(): self
{
return new static();
return new self();
}
}
1 change: 0 additions & 1 deletion src/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

/**
* TODO: Make all supporting data structures cachable -> we'll add caching later on
* TODO: split DataCollection in DataCollection and PaginatedDataCollection
* TODO: update the typescript transformer with new property data objects
* TODO: add support for wrapping data objects and collections within keys
*/
Expand Down
32 changes: 21 additions & 11 deletions src/DataCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Spatie\LaravelData\Concerns\IncludeableData;
use Spatie\LaravelData\Concerns\ResponsableData;
use Spatie\LaravelData\Exceptions\CannotCastData;
use Spatie\LaravelData\Exceptions\InvalidDataCollectionModification;
use Spatie\LaravelData\Exceptions\InvalidDataCollectionOperation;
use Spatie\LaravelData\Support\EloquentCasts\DataCollectionEloquentCast;
use Spatie\LaravelData\Transformers\DataCollectionTransformer;

Expand All @@ -42,18 +42,20 @@ class DataCollection implements Responsable, Arrayable, Jsonable, JsonSerializab
private Enumerable $items;

/**
* @param class-string<\Spatie\LaravelData\Data> $dataClass
* @param class-string<TValue> $dataClass
* @param array|Enumerable<array-key, TValue>|DataCollection $items
*/
public function __construct(
public readonly string $dataClass,
Enumerable|array|DataCollection $items
) {
$items = match (true) {
is_array($items) => new Collection($items),
$items instanceof DataCollection => $items->toCollection(),
default => $items
};
if(is_array($items)){
$items = new Collection($items);
}

if($items instanceof DataCollection){
$items = $items->toCollection();
}

$this->items = $items->map(
fn ($item) => $item instanceof $this->dataClass ? $item : $this->dataClass::from($item)
Expand Down Expand Up @@ -161,6 +163,10 @@ public function count(): int
*/
public function offsetExists($offset): bool
{
if (! $this->items instanceof ArrayAccess) {
throw InvalidDataCollectionOperation::create();
}

return $this->items->offsetExists($offset);
}

Expand All @@ -171,6 +177,10 @@ public function offsetExists($offset): bool
*/
public function offsetGet($offset): mixed
{
if (! $this->items instanceof ArrayAccess) {
throw InvalidDataCollectionOperation::create();
}

return $this->items->offsetGet($offset);
}

Expand All @@ -182,8 +192,8 @@ public function offsetGet($offset): mixed
*/
public function offsetSet($offset, $value): void
{
if ($this->items instanceof LazyCollection) {
throw InvalidDataCollectionModification::cannotSetItem();
if (! $this->items instanceof ArrayAccess) {
throw InvalidDataCollectionOperation::create();
}

$value = $value instanceof Data
Expand All @@ -200,8 +210,8 @@ public function offsetSet($offset, $value): void
*/
public function offsetUnset($offset): void
{
if ($this->items instanceof LazyCollection) {
throw InvalidDataCollectionModification::cannotUnSetItem();
if (! $this->items instanceof ArrayAccess) {
throw InvalidDataCollectionOperation::create();
}

$this->items->offsetUnset($offset);
Expand Down
6 changes: 3 additions & 3 deletions src/DataPipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function execute(): Collection
$this->normalizers
);

/** @var \Spatie\LaravelData\DataPipes\DataPipe $pipes */
/** @var \Spatie\LaravelData\DataPipes\DataPipe[] $pipes */
$pipes = array_map(
fn (string|DataPipe $pipe) => is_string($pipe) ? app($pipe) : $pipe,
$this->pipes
Expand All @@ -79,12 +79,12 @@ public function execute(): Collection
}
}

$properties = collect($properties);

if ($properties === null) {
throw CannotCreateDataFromValue::create($this->classString, $this->value);
}

$properties = collect($properties);

$class = $this->dataConfig->getDataClass($this->classString);

foreach ($pipes as $pipe) {
Expand Down
8 changes: 4 additions & 4 deletions src/DataPipes/ValidatePropertiesDataPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public function __construct(
) {
}

public static function onlyRequests(): static
public static function onlyRequests(): self
{
return new static(app(DataValidationRulesResolver::class), false);
return new self(app(DataValidatorResolver::class), false);
}

public static function allTypes(): static
public static function allTypes(): self
{
return new static(app(DataValidationRulesResolver::class), true);
return new self(app(DataValidatorResolver::class), true);
}

public function handle(mixed $initialValue, DataClass $class, Collection $properties): Collection
Expand Down
23 changes: 0 additions & 23 deletions src/Exceptions/InvalidDataCollectionModification.php

This file was deleted.

13 changes: 13 additions & 0 deletions src/Exceptions/InvalidDataCollectionOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\LaravelData\Exceptions;

use Exception;

class InvalidDataCollectionOperation extends Exception
{
public static function create(): self
{
return new self('Cannot execute an array operation on this type of collection');
}
}
6 changes: 3 additions & 3 deletions src/Lazy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ abstract class Lazy
{
private ?bool $defaultIncluded = null;

public static function create(Closure $value): static
public static function create(Closure $value): DefaultLazy
{
return new DefaultLazy($value);
}

public static function when(Closure $condition, Closure $value): static
public static function when(Closure $condition, Closure $value): ConditionalLazy
{
return new ConditionalLazy($condition, $value);
}

public static function whenLoaded(string $relation, Model $model, Closure $value): static
public static function whenLoaded(string $relation, Model $model, Closure $value): RelationalLazy
{
return new RelationalLazy($relation, $model, $value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/PaginatedDataCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PaginatedDataCollection implements Responsable, Arrayable, Jsonable, JsonS
private CursorPaginator|Paginator $items;

/**
* @param class-string<\Spatie\LaravelData\Data> $dataClass
* @param class-string<TValue> $dataClass
* @param CursorPaginator<TValue>|Paginator $items
*/
public function __construct(
Expand Down
2 changes: 1 addition & 1 deletion src/Resolvers/DataValidationRulesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function execute(string $class, bool $nullable = false): Collection
return $this->dataConfig->getDataClass($class)
->properties
->reject(fn (DataProperty $property) => array_key_exists($property->name, $overWrittenRules) || ! $property->validate)
->mapWithKeys(fn (DataProperty $property) => $resolver->execute($property, $nullable))
->mapWithKeys(fn (DataProperty $property) => $resolver->execute($property, $nullable)->all())
->merge($overWrittenRules);
}
}
1 change: 0 additions & 1 deletion src/Resolvers/EmptyDataResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ private function getValueForProperty(DataProperty $property): mixed
}

if ($property->type->isDataObject) {
/** @var \Spatie\LaravelData\Data $type */
return $property->type->dataClass::empty();
}

Expand Down
34 changes: 21 additions & 13 deletions src/Resolvers/NameMappersResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class NameMappersResolver
{
public static function create(array $ignoredMappers = []): static
public static function create(array $ignoredMappers = []): self
{
return new self($ignoredMappers);
}
Expand All @@ -32,9 +32,9 @@ public function execute(
private function resolveInputNameMapper(
Collection $attributes
): ?NameMapper {
/** @var \Spatie\LaravelData\Attributes\MapInputName&\Spatie\LaravelData\Attributes\MapName|null $mapper */
$mapper = $attributes->first(fn (object $attribute) => $attribute instanceof MapInputName)
?? $attributes->first(fn (object $attribute) => $attribute instanceof MapName);
/** @var \Spatie\LaravelData\Attributes\MapInputName|\Spatie\LaravelData\Attributes\MapName|null $mapper */
$mapper = $attributes->first(fn(object $attribute) => $attribute instanceof MapInputName)
?? $attributes->first(fn(object $attribute) => $attribute instanceof MapName);

if ($mapper) {
return $this->resolveMapper($mapper->input);
Expand All @@ -46,9 +46,9 @@ private function resolveInputNameMapper(
private function resolveOutputNameMapper(
Collection $attributes
): ?NameMapper {
/** @var \Spatie\LaravelData\Attributes\MapOutputName&\Spatie\LaravelData\Attributes\MapName|null $mapper */
$mapper = $attributes->first(fn (object $attribute) => $attribute instanceof MapOutputName)
?? $attributes->first(fn (object $attribute) => $attribute instanceof MapName);
/** @var \Spatie\LaravelData\Attributes\MapOutputName|\Spatie\LaravelData\Attributes\MapName|null $mapper */
$mapper = $attributes->first(fn(object $attribute) => $attribute instanceof MapOutputName)
?? $attributes->first(fn(object $attribute) => $attribute instanceof MapName);

if ($mapper) {
return $this->resolveMapper($mapper->output);
Expand All @@ -59,12 +59,7 @@ private function resolveOutputNameMapper(

private function resolveMapper(string|int $value): ?NameMapper
{
$mapper = match (true) {
is_int($value) => new ProvidedNameMapper($value),
is_a($value, NameMapper::class, true) => resolve($value),
is_string($value) => new ProvidedNameMapper($value),
default => null,
};
$mapper = $this->resolveMapperClass($value);

foreach ($this->ignoredMappers as $ignoredMapper) {
if ($mapper instanceof $ignoredMapper) {
Expand All @@ -74,4 +69,17 @@ private function resolveMapper(string|int $value): ?NameMapper

return $mapper;
}

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

if (is_a($value, NameMapper::class, true)) {
return resolve($value);
}

return new ProvidedNameMapper($value);
}
}
12 changes: 8 additions & 4 deletions src/Resolvers/PartialsTreeFromRequestResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Request;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\DataCollection;
use Spatie\LaravelData\PaginatedDataCollection;
use Spatie\LaravelData\Support\DataConfig;
use Spatie\LaravelData\Support\PartialsParser;
use Spatie\LaravelData\Support\PartialTrees;
Expand All @@ -18,7 +19,7 @@ public function __construct(
}

public function execute(
Data|DataCollection $data,
Data|DataCollection|PaginatedDataCollection $data,
Request $request,
): PartialTrees {
$includesTree = $this->partialsParser->execute(explode(',', $request->get('include', '')));
Expand Down Expand Up @@ -92,7 +93,6 @@ private function reducePartialsTree(
?array $allowedPartials,
string $methodName,
): array {
/** @var \Spatie\LaravelData\Support\DataProperty[] $properties */
$properties = $this->dataConfig->getDataClass($class)->properties;

if ($allowedPartials === null) {
Expand All @@ -110,8 +110,12 @@ private function reducePartialsTree(
continue;
}

$checkNested = $properties[$requestedPartial]?->type?->isDataObject
|| $properties[$requestedPartial]?->type?->isDataCollection;
if (! $properties->has($requestedPartial)) {
continue;
}

$checkNested = $properties[$requestedPartial]->type->isDataObject
|| $properties[$requestedPartial]->type->isDataCollection;

if ($checkNested) {
$requestedPartialsTree[$requestedPartial] = $this->{$methodName}(
Expand Down
Loading

0 comments on commit 36f66f7

Please sign in to comment.