Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance the serializer tests #597

Merged
merged 4 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"preset": "laravel",
"rules": {
"nullable_type_declaration_for_default_null_value": false
}
}
2 changes: 1 addition & 1 deletion src/Bootstrap/RoutesDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class RoutesDefinition
{
private array $excludedMiddleware = [];

public function __invoke(string $uriKey = null)
public function __invoke(?string $uriKey = null)
{
$prefix = $uriKey ?: '{repository}';

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/PublishAuthCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ protected function getRouteStubs(): string
return $routeStubs;
}

protected function getRemainingActionsString(array $actions = null): string
protected function getRemainingActionsString(?array $actions = null): string
{
$allActions = ['login', 'register', 'resetPassword', 'forgotPassword', 'verifyEmail'];

Expand Down
2 changes: 1 addition & 1 deletion src/Eager/Related.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Related implements JsonSerializable

public ?RelatedQuery $relatedQuery = null;

public function __construct(string $relation, EagerField $field = null)
public function __construct(string $relation, ?EagerField $field = null)
{
$this->relation = $relation;
$this->field = $field;
Expand Down
4 changes: 2 additions & 2 deletions src/Exceptions/RepositoryException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public static function missingKey(string $class = 'Repository key missing.'): se
return new self($class);
}

public static function unauthorized(string $class = null): self
public static function unauthorized(?string $class = null): self
{
return new self(__('Unauthorized to view repository :name. Check "allowRestify" policy.', [
'name' => $class,
]), code: 403);
}

public static function routeUnauthorized(string $uri = null): self
public static function routeUnauthorized(?string $uri = null): self
{
return new self(__('Unauthorized to use the route :name. Check prefix.', [
'name' => $uri,
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BelongsTo extends EagerField implements Sortable

public ?array $searchablesAttributes = null;

public function fillAttribute(RestifyRequest $request, $model, int $bulkRow = null)
public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = null)
{
/** * @var Model $relatedModel */
$relatedModel = $model->{$this->relation}()->getModel();
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BelongsToMany extends EagerField
*/
public $detachCallback;

public function __construct($relation, string $parentRepository = null)
public function __construct($relation, ?string $parentRepository = null)
{
parent::__construct($relation, $parentRepository);

Expand Down
4 changes: 2 additions & 2 deletions src/Fields/EagerField.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class EagerField extends Field

private RelatedQuery $relatedQuery;

public function __construct($attribute, string $parentRepository = null)
public function __construct($attribute, ?string $parentRepository = null)
{
parent::__construct(attribute: $attribute);

Expand Down Expand Up @@ -101,7 +101,7 @@ public function resolve($repository, $attribute = null)
}

public function getRelation(
Repository $repository = null
?Repository $repository = null
): Relation {
$repository = $repository ?? $this->parentRepository;

Expand Down
8 changes: 4 additions & 4 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class Field extends OrganicField implements JsonSerializable
* @param string|callable|null $attribute
* @param callable|null $resolveCallback
*/
public function __construct($attribute, callable|Closure $resolveCallback = null)
public function __construct($attribute, callable|Closure|null $resolveCallback = null)
{
$this->attribute = $attribute;

Expand Down Expand Up @@ -210,7 +210,7 @@ public function fillCallback(callable|Closure $callback)
*
* @return mixed|void
*/
public function fillAttribute(RestifyRequest $request, $model, int $bulkRow = null)
public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = null)
{
$this->resolveValueBeforeUpdate($request, $model);

Expand Down Expand Up @@ -258,7 +258,7 @@ public function fillAttribute(RestifyRequest $request, $model, int $bulkRow = nu
/**
* Fill the model with value from the request.
*/
protected function fillAttributeFromRequest(RestifyRequest $request, $model, $attribute, int $bulkRow = null)
protected function fillAttributeFromRequest(RestifyRequest $request, $model, $attribute, ?int $bulkRow = null)
{
$attribute = is_null($bulkRow)
? $attribute
Expand All @@ -279,7 +279,7 @@ protected function fillAttributeFromRequest(RestifyRequest $request, $model, $at
/**
* Fill the model with value from the callback.
*/
protected function fillAttributeFromCallback(RestifyRequest $request, $model, $attribute, int $bulkRow = null)
protected function fillAttributeFromCallback(RestifyRequest $request, $model, $attribute, ?int $bulkRow = null)
{
if (is_callable($cb = $this->guessBeforeFillableCallable($request))) {
$value = $request->input($attribute ?? $this->attribute);
Expand Down
8 changes: 4 additions & 4 deletions src/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class File extends Field implements DeletableContract, StorableContract
*/
public $storageCallback;

public function __construct($attribute, callable $resolveCallback = null)
public function __construct($attribute, ?callable $resolveCallback = null)
{
parent::__construct($attribute, $resolveCallback);

Expand Down Expand Up @@ -81,7 +81,7 @@ public function storeAs($storeAs): self
*
* @return $this
*/
public function resolveUsingTemporaryUrl(bool $resolveTemporaryUrl = true, CarbonInterface $expiration = null, array $options = []): self
public function resolveUsingTemporaryUrl(bool $resolveTemporaryUrl = true, ?CarbonInterface $expiration = null, array $options = []): self
{
if (! $resolveTemporaryUrl) {
return $this;
Expand Down Expand Up @@ -126,7 +126,7 @@ public function resolveUsingFullUrl(): self
/**
* Prepare the storage callback.
*/
protected function prepareStorageCallback(callable $storageCallback = null): void
protected function prepareStorageCallback(?callable $storageCallback = null): void
{
$this->storageCallback = $storageCallback ?? function ($request, $model) {
return $this->mergeExtraStorageColumns($request, [
Expand Down Expand Up @@ -227,7 +227,7 @@ protected function columnsThatShouldBeDeleted(): array
return $attributes;
}

public function fillAttribute(RestifyRequest $request, $model, int $bulkRow = null)
public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = null)
{
if (is_null($file = $request->file($this->attribute)) || ! $file->isValid()) {
return $this;
Expand Down
4 changes: 2 additions & 2 deletions src/Fields/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class HasMany extends EagerField
{
protected $canEnableRelationshipCallback;

public function __construct($relation, string $parentRepository = null)
public function __construct($relation, ?string $parentRepository = null)
{
parent::__construct($relation, $parentRepository);

Expand Down Expand Up @@ -48,7 +48,7 @@ public function resolve($repository, $attribute = null)
return $this;
}

public function fillAttribute(RestifyRequest $request, $model, int $bulkRow = null)
public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = null)
{
//
}
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class HasOne extends EagerField implements Sortable
{
use CanSort;

public function fillAttribute(RestifyRequest $request, $model, int $bulkRow = null)
public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = null)
{
//
}
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class Image extends File
{
public function __construct($attribute, callable $resolveCallback = null)
public function __construct($attribute, ?callable $resolveCallback = null)
{
parent::__construct($attribute, $resolveCallback);

Expand Down
2 changes: 1 addition & 1 deletion src/Filters/AdvancedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function validatePayload(RestifyRequest $request, AdvancedFilterPayloadDa
return $this;
}

protected function input(string $key = null, $default = null)
protected function input(?string $key = null, $default = null)
{
return data_get($this->dataObject->value, $key, $default);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/RelatedDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class RelatedDto
public string $rootKey = '';

public function __construct(
RelatedQueryCollection $related = null,
?RelatedQueryCollection $related = null,
) {
$this->related = $related ?? RelatedQueryCollection::make([]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/RelatedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function __construct(
public string $relation,
public bool $loaded = false,
public array $columns = ['*'],
RelatedQueryCollection $nested = null,
?RelatedQueryCollection $nested = null,
) {
$this->nested = $nested ?? RelatedQueryCollection::make([]);
$this->tree = $relation;
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/SortableFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function resolveFrontendColumn(): self
return $this;
}

public function syncDirection(string $direction = null): self
public function syncDirection(?string $direction = null): self
{
if (! is_null($direction) && in_array($direction, ['asc', 'desc'])) {
$this->direction = $direction;
Expand Down
8 changes: 4 additions & 4 deletions src/Http/Requests/Concerns/InteractWithRepositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ public function repository($key = null): Repository
}
}

public function repositoryWith(Model $model, string $uriKey = null): Repository
public function repositoryWith(Model $model, ?string $uriKey = null): Repository
{
$repository = $this->repository($uriKey);

return $repository::resolveWith($model);
}

public function model(string $uriKey = null): Model
public function model(?string $uriKey = null): Model
{
$repository = $this->repository($uriKey);

return $repository::newModel();
}

public function newQuery(string $uriKey = null): Builder|Relation
public function newQuery(?string $uriKey = null): Builder|Relation
{
if (! $this->isViaRepository()) {
return $this->model($uriKey)->newQuery();
Expand All @@ -88,7 +88,7 @@ public function viaQuery(): Relation
return $this->relatedEagerField()->getRelation();
}

public function modelQuery(string $repositoryId = null, string $uriKey = null): Builder|Relation
public function modelQuery(?string $repositoryId = null, ?string $uriKey = null): Builder|Relation
{
return $this->newQuery($uriKey)->whereKey(
$repositoryId ?? $this->route('repositoryId')
Expand Down
10 changes: 5 additions & 5 deletions src/Models/ActionLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ActionLog extends Model

public const ACTION_DELETED = 'Deleted';

public static function forRepositoryStored(Model $model, Authenticatable $user = null, array $dirty = null): self
public static function forRepositoryStored(Model $model, ?Authenticatable $user = null, ?array $dirty = null): self
{
return new static([
'batch_id' => (string) Str::uuid(),
Expand All @@ -69,7 +69,7 @@ public static function forRepositoryStored(Model $model, Authenticatable $user =
]);
}

public static function forRepositoryUpdated(Model $model, Authenticatable $user = null): self
public static function forRepositoryUpdated(Model $model, ?Authenticatable $user = null): self
{
return new static([
'batch_id' => (string) Str::uuid(),
Expand All @@ -91,7 +91,7 @@ public static function forRepositoryUpdated(Model $model, Authenticatable $user
]);
}

public static function forRepositoryDestroy(Model $model, Authenticatable $user = null): self
public static function forRepositoryDestroy(Model $model, ?Authenticatable $user = null): self
{
return new static([
'batch_id' => (string) Str::uuid(),
Expand All @@ -113,7 +113,7 @@ public static function forRepositoryDestroy(Model $model, Authenticatable $user
]);
}

public static function forRepositoryAction(Action $action, Model $model, Authenticatable $user = null): self
public static function forRepositoryAction(Action $action, Model $model, ?Authenticatable $user = null): self
{
return new static([
'batch_id' => (string) Str::uuid(),
Expand All @@ -139,7 +139,7 @@ public static function register(
string $name,
Model $actionable,
array $attributes = [],
Authenticatable $user = null
?Authenticatable $user = null
): self {
return new static(array_merge([
'batch_id' => (string) Str::uuid(),
Expand Down
10 changes: 5 additions & 5 deletions src/Repositories/Concerns/Testing.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
trait Testing
{
public static function route(
string|Model $path = null,
string|Model|null $path = null,
array $query = [],
Action|callable $action = null,
Action|callable|null $action = null,
): string {
if ($path instanceof Model) {
$path = $path->getKey();
Expand Down Expand Up @@ -47,19 +47,19 @@ public static function route(
/**
* @param Action $action
*/
public static function action(string $action, string|int $key = null): string
public static function action(string $action, string|int|null $key = null): string
{
return static::route($key, action: app($action));
}

public static function getter(string $getter, string|int $key = null): string
public static function getter(string $getter, string|int|null $key = null): string
{
$path = $key ? "$key/getters" : 'getters';

return static::route($path.'/'.Getter::guessUriKey(app($getter)));
}

public function dd(string $prop = null): void
public function dd(?string $prop = null): void
{
if (is_null($prop)) {
dd($this);
Expand Down
6 changes: 3 additions & 3 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ public function jsonSerialize()
return $this->serializeForShow(app(RestifyRequest::class));
}

private function modelAttributes(Request $request = null): Collection
private function modelAttributes(?Request $request = null): Collection
{
return collect(method_exists($this->resource, 'toArray') ? $this->resource->toArray() : []);
}
Expand All @@ -1071,7 +1071,7 @@ protected static function fillBulkFields(
RestifyRequest $request,
Model $model,
Collection $fields,
int $bulkRow = null
?int $bulkRow = null
) {
return $fields->map(function (Field $field) use ($request, $model, $bulkRow) {
return $field->fillAttribute($request, $model, $bulkRow);
Expand All @@ -1098,7 +1098,7 @@ public static function getDetachers(): array
return static::$detachers;
}

public function eager(EagerField $field = null): Repository
public function eager(?EagerField $field = null): Repository
{
if (! $field) {
$this->eagerState = false;
Expand Down
2 changes: 1 addition & 1 deletion src/Repositories/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function jsonSerialize(): mixed
]);
}

private function request(string $class = null): RestifyRequest
private function request(?string $class = null): RestifyRequest
{
/**
* @var RestifyRequest $request
Expand Down
Loading