From 8ae2424f1bc602f98cd939fe7aa957a24ba6d6a7 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Wed, 23 Dec 2020 13:31:22 +0000 Subject: [PATCH] Apply fixes from StyleCI --- src/Fields/BelongsToMany.php | 6 +- src/Repositories/PivotsCollection.php | 2 +- src/Repositories/Repository.php | 80 +++++++++---------- .../RepositoryAttachControllerTest.php | 14 ++-- .../RepositoryIndexControllerTest.php | 2 +- tests/Fixtures/Company/CompanyRepository.php | 2 +- 6 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index dfa1eec9a..4d1adf896 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -31,8 +31,8 @@ class BelongsToMany extends EagerField public function __construct($attribute, $relation, $parentRepository) { - if (!is_a(app($parentRepository), Repository::class)) { - abort(500, "Invalid parent repository [{$parentRepository}]. Expended instance of " . Repository::class); + if (! is_a(app($parentRepository), Repository::class)) { + abort(500, "Invalid parent repository [{$parentRepository}]. Expended instance of ".Repository::class); } parent::__construct($attribute); @@ -55,7 +55,7 @@ public function resolve($repository, $attribute = null) ->allowToShow(app(Request::class)) ->withPivots( PivotsCollection::make($this->pivotFields) - ->map(fn(Field $field) => clone $field) + ->map(fn (Field $field) => clone $field) ->resolveFromPivot($item->pivot) ) ->eagerState(); diff --git a/src/Repositories/PivotsCollection.php b/src/Repositories/PivotsCollection.php index 7fffb3244..4ef91e176 100644 --- a/src/Repositories/PivotsCollection.php +++ b/src/Repositories/PivotsCollection.php @@ -11,7 +11,7 @@ class PivotsCollection extends Collection public function resolveFromPivot(Pivot $pivot): self { return $this->map(function (Field $field) use ($pivot) { - return $field->resolveCallback(fn() => $pivot->{$field->attribute}); + return $field->resolveCallback(fn () => $pivot->{$field->attribute}); }); } } diff --git a/src/Repositories/Repository.php b/src/Repositories/Repository.php index 328904acf..8efbbefab 100644 --- a/src/Repositories/Repository.php +++ b/src/Repositories/Repository.php @@ -258,7 +258,7 @@ public static function newModel(): Model public static function query(RestifyRequest $request) { - if (!$request->isViaRepository()) { + if (! $request->isViaRepository()) { return static::newModel()->query(); } @@ -425,16 +425,16 @@ public function resolveShowAttributes(RestifyRequest $request) { $fields = $this->collectFields($request) ->forShow($request, $this) - ->filter(fn(Field $field) => $field->authorize($request)) + ->filter(fn (Field $field) => $field->authorize($request)) ->when( $this->isEagerState(), function ($items) { - return $items->filter(fn(Field $field) => !$field instanceof EagerField); + return $items->filter(fn (Field $field) => ! $field instanceof EagerField); } ) - ->each(fn(Field $field) => $field->resolveForShow($this)) - ->map(fn(Field $field) => $field->serializeToValue($request)) - ->mapWithKeys(fn($value) => $value) + ->each(fn (Field $field) => $field->resolveForShow($this)) + ->map(fn (Field $field) => $field->serializeToValue($request)) + ->mapWithKeys(fn ($value) => $value) ->all(); if ($this instanceof Mergeable) { @@ -452,7 +452,7 @@ function ($items) { return false; } - if (!$field->authorize($request)) { + if (! $field->authorize($request)) { return false; } @@ -475,16 +475,16 @@ public function resolveIndexAttributes($request) $fields = $this ->collectFields($request) ->forIndex($request, $this) - ->filter(fn(Field $field) => $field->authorize($request)) + ->filter(fn (Field $field) => $field->authorize($request)) ->when( $this->eagerState, function ($items) { - return $items->filter(fn(Field $field) => !$field instanceof EagerField); + return $items->filter(fn (Field $field) => ! $field instanceof EagerField); } ) - ->each(fn(Field $field) => $field->resolveForIndex($this)) - ->map(fn(Field $field) => $field->serializeToValue($request)) - ->mapWithKeys(fn($value) => $value) + ->each(fn (Field $field) => $field->resolveForIndex($this)) + ->map(fn (Field $field) => $field->serializeToValue($request)) + ->mapWithKeys(fn ($value) => $value) ->all(); if ($this instanceof Mergeable) { @@ -502,7 +502,7 @@ function ($items) { return false; } - if (!$field->authorize($request)) { + if (! $field->authorize($request)) { return false; } @@ -530,10 +530,10 @@ public function resolveShowPivots(RestifyRequest $request): array } return $pivots - ->filter(fn(Field $field) => $field->authorize($request)) - ->each(fn(Field $field) => $field->resolve($this)) - ->map(fn(Field $field) => $field->serializeToValue($request)) - ->mapWithKeys(fn($value) => $value) + ->filter(fn (Field $field) => $field->authorize($request)) + ->each(fn (Field $field) => $field->resolve($this)) + ->map(fn (Field $field) => $field->serializeToValue($request)) + ->mapWithKeys(fn ($value) => $value) ->all(); } @@ -575,16 +575,16 @@ public function resolveRelationships($request): array : $this->resource->{$relation}(); collect([ - Builder::class => fn() => $withs->put($relation, (static::$relatedCast)::fromBuilder($request, $paginator, $this)), + Builder::class => fn () => $withs->put($relation, (static::$relatedCast)::fromBuilder($request, $paginator, $this)), - Relation::class => fn() => $withs->put($relation, (static::$relatedCast)::fromRelation($request, $paginator, $this)), + Relation::class => fn () => $withs->put($relation, (static::$relatedCast)::fromRelation($request, $paginator, $this)), - Collection::class => fn() => $withs->put($relation, $paginator), + Collection::class => fn () => $withs->put($relation, $paginator), - Model::class => fn() => fn() => $withs->put($relation, $paginator), + Model::class => fn () => fn () => $withs->put($relation, $paginator), - ])->first(fn($fn, $class) => $paginator instanceof $class, - fn() => fn() => $withs->put($relation, $paginator) + ])->first(fn ($fn, $class) => $paginator instanceof $class, + fn () => fn () => $withs->put($relation, $paginator) )(); }); @@ -638,14 +638,14 @@ public function index(RestifyRequest $request) [ 'meta' => $this->when( $meta = $this->resolveIndexMainMeta( - $request, $models = $items->map(fn(self $repository) => $repository->resource), RepositoryCollection::meta($paginator->toArray()) + $request, $models = $items->map(fn (self $repository) => $repository->resource), RepositoryCollection::meta($paginator->toArray()) ), $meta ), 'links' => $this->when( $links = $this->resolveIndexLinks($request, $models, RepositoryCollection::paginationLinks($paginator->toArray())), $links ), - 'data' => $items->map(fn(self $repository) => $repository->serializeForIndex($request)), + 'data' => $items->map(fn (self $repository) => $repository->serializeForIndex($request)), ] ) ); @@ -695,7 +695,7 @@ public function store(RestifyRequest $request) } } - $fields->each(fn(Field $field) => $field->invokeAfter($request, $this->resource)); + $fields->each(fn (Field $field) => $field->invokeAfter($request, $this->resource)); }); static::stored($this->resource, $request); @@ -726,7 +726,7 @@ public function storeBulk(RepositoryStoreBulkRequest $request) $this->resource->save(); - $fields->each(fn(Field $field) => $field->invokeAfter($request, $this->resource)); + $fields->each(fn (Field $field) => $field->invokeAfter($request, $this->resource)); return $this->resource; }); @@ -753,7 +753,7 @@ public function update(RestifyRequest $request, $repositoryId) return $fields; })->each( - fn(Field $field) => $field->invokeAfter($request, $this->resource) + fn (Field $field) => $field->invokeAfter($request, $this->resource) ); return $this->response() @@ -782,7 +782,7 @@ public function attach(RestifyRequest $request, $repositoryId, Collection $pivot $eagerField = $this->authorizeBelongsToMany($request)->belongsToManyField($request); DB::transaction(function () use ($request, $pivots, $eagerField) { - $fields = $eagerField->collectPivotFields()->filter(fn($pivotField) => $request->has($pivotField->attribute))->values(); + $fields = $eagerField->collectPivotFields()->filter(fn ($pivotField) => $request->has($pivotField->attribute))->values(); $pivots->map(function ($pivot) use ($request, $fields, $eagerField) { static::validatorForAttach($request)->validate(); @@ -815,7 +815,7 @@ public function detach(RestifyRequest $request, $repositoryId, Collection $pivot $deleted = DB::transaction(function () use ($pivots, $eagerField, $request) { return $pivots - ->map(fn($pivot) => $eagerField->authorizeToDetach($request, $pivot) && $pivot->delete()); + ->map(fn ($pivot) => $eagerField->authorizeToDetach($request, $pivot) && $pivot->delete()); }); return $this->response() @@ -847,18 +847,18 @@ public function allowToUpdate(RestifyRequest $request, $payload = null): self public function allowToAttach(RestifyRequest $request, Collection $attachers): self { - $methodGuesser = 'attach' . Str::studly($request->relatedRepository); + $methodGuesser = 'attach'.Str::studly($request->relatedRepository); - $attachers->each(fn($model) => $this->authorizeToAttach($request, $methodGuesser, $model)); + $attachers->each(fn ($model) => $this->authorizeToAttach($request, $methodGuesser, $model)); return $this; } public function allowToDetach(RestifyRequest $request, Collection $attachers): self { - $methodGuesser = 'detach' . Str::studly($request->relatedRepository); + $methodGuesser = 'detach'.Str::studly($request->relatedRepository); - $attachers->each(fn($model) => $this->authorizeToDetach($request, $methodGuesser, $model)); + $attachers->each(fn ($model) => $this->authorizeToDetach($request, $methodGuesser, $model)); return $this; } @@ -948,7 +948,7 @@ public function response($content = '', $status = 200, array $headers = []): Res public function serializeForShow(RestifyRequest $request): array { return $this->filter([ - 'id' => $this->when(optional($this->resource)->id, fn() => $this->getId($request)), + 'id' => $this->when(optional($this->resource)->id, fn () => $this->getId($request)), 'type' => $this->when($type = $this->getType($request), $type), 'attributes' => $request->isShowRequest() ? $this->resolveShowAttributes($request) : $this->resolveIndexAttributes($request), 'relationships' => $this->when(value($related = $this->resolveRelationships($request)), $related), @@ -962,7 +962,7 @@ public function serializeForIndex(RestifyRequest $request): array return $this->filter([ 'id' => $this->when($id = $this->getId($request), $id), 'type' => $this->when($type = $this->getType($request), $type), - 'attributes' => $this->when((bool)$attrs = $this->resolveIndexAttributes($request), $attrs), + 'attributes' => $this->when((bool) $attrs = $this->resolveIndexAttributes($request), $attrs), 'relationships' => $this->when(value($related = $this->resolveIndexRelationships($request)), $related), 'meta' => $this->when(value($meta = $this->resolveIndexMeta($request)), $meta), 'pivots' => $this->when(value($pivots = $this->resolveIndexPivots($request)), $pivots), @@ -976,7 +976,7 @@ protected function getType(RestifyRequest $request): ?string protected function getId(RestifyRequest $request): ?string { - if (!static::$id) { + if (! static::$id) { return null; } @@ -1005,7 +1005,7 @@ private function modelAttributes(Request $request = null): Collection */ protected static function fillFields(RestifyRequest $request, Model $model, Collection $fields) { - return $fields->map(fn(Field $field) => $field->fillAttribute($request, $model)); + return $fields->map(fn (Field $field) => $field->fillAttribute($request, $model)); } protected static function fillBulkFields(RestifyRequest $request, Model $model, Collection $fields, int $bulkRow = null) @@ -1017,12 +1017,12 @@ protected static function fillBulkFields(RestifyRequest $request, Model $model, public static function uriTo(Model $model) { - return Str::replaceFirst('//', '/', Restify::path() . '/' . static::uriKey() . '/' . $model->getKey()); + return Str::replaceFirst('//', '/', Restify::path().'/'.static::uriKey().'/'.$model->getKey()); } public function availableFilters(RestifyRequest $request) { - return collect($this->filter($this->filters($request)))->each(fn(Filter $filter) => $filter->authorizedToSee($request)) + return collect($this->filter($this->filters($request)))->each(fn (Filter $filter) => $filter->authorizedToSee($request)) ->values(); } diff --git a/tests/Controllers/RepositoryAttachControllerTest.php b/tests/Controllers/RepositoryAttachControllerTest.php index c99ad83a4..967f53a4f 100644 --- a/tests/Controllers/RepositoryAttachControllerTest.php +++ b/tests/Controllers/RepositoryAttachControllerTest.php @@ -15,7 +15,7 @@ public function test_attach_a_user_to_a_company() $user = $this->mockUsers(2)->first(); $company = factory(Company::class)->create(); - $response = $this->postJson('companies/' . $company->id . '/attach/users', [ + $response = $this->postJson('companies/'.$company->id.'/attach/users', [ 'users' => $user->id, 'is_admin' => true, ])->assertStatus(201); @@ -32,7 +32,7 @@ public function test_pivot_field_validation() $user = $this->mockUsers(2)->first(); $company = factory(Company::class)->create(); - $this->postJson('companies/' . $company->id . '/attach/users', [ + $this->postJson('companies/'.$company->id.'/attach/users', [ 'users' => $user->id, ]) ->assertStatus(400); @@ -47,7 +47,7 @@ public function test_pivot_field_present_when_show() $company->users()->attach($this->mockUsers()->first()->id); }); - $response = $this->getJson('companies/' . $company->id . '?related=users') + $response = $this->getJson('companies/'.$company->id.'?related=users') ->assertOk(); $this->assertSame( @@ -67,7 +67,7 @@ public function test_attach_multiple_users_to_a_company() $usersFromCompany = $this->getJson('users?viaRepository=companies&viaRepositoryId=1&viaRelationship=users'); $this->assertCount(0, $usersFromCompany->json('data')); - $response = $this->postJson('companies/' . $company->id . '/attach/users', [ + $response = $this->postJson('companies/'.$company->id.'/attach/users', [ 'users' => [1, 2], 'is_admin' => true, ]) @@ -91,7 +91,7 @@ public function test_after_attach_a_user_to_company_number_of_users_increased() $this->getJson('users?viaRepository=companies&viaRepositoryId=1&viaRelationship=users') ->assertJsonCount(0, 'data'); - $this->postJson('companies/' . $company->id . '/attach/users', [ + $this->postJson('companies/'.$company->id.'/attach/users', [ 'users' => $user->id, 'is_admin' => true, ]); @@ -112,7 +112,7 @@ public function test_policy_to_attach_a_user_to_a_company() $_SERVER['allow_attach_users'] = false; - $this->postJson('companies/' . $company->id . '/attach/users', [ + $this->postJson('companies/'.$company->id.'/attach/users', [ 'users' => $user->id, 'is_admin' => true, ]) @@ -120,7 +120,7 @@ public function test_policy_to_attach_a_user_to_a_company() $_SERVER['allow_attach_users'] = true; - $this->postJson('companies/' . $company->id . '/attach/users', [ + $this->postJson('companies/'.$company->id.'/attach/users', [ 'users' => $user->id, 'is_admin' => true, ]) diff --git a/tests/Controllers/RepositoryIndexControllerTest.php b/tests/Controllers/RepositoryIndexControllerTest.php index 56cc7e0aa..67672daf3 100644 --- a/tests/Controllers/RepositoryIndexControllerTest.php +++ b/tests/Controllers/RepositoryIndexControllerTest.php @@ -125,7 +125,7 @@ public function test_repository_with_deep_relations() CompanyRepository::partialMock() ->expects('related') ->andReturn([ - 'users.posts' + 'users.posts', ]); tap(factory(Company::class)->create(), function (Company $company) { diff --git a/tests/Fixtures/Company/CompanyRepository.php b/tests/Fixtures/Company/CompanyRepository.php index a172d6596..3188f9900 100644 --- a/tests/Fixtures/Company/CompanyRepository.php +++ b/tests/Fixtures/Company/CompanyRepository.php @@ -28,7 +28,7 @@ public function fields(RestifyRequest $request) BelongsToMany::make('users', 'users', UserRepository::class)->withPivot( Field::make('is_admin')->rules('required') - )->canDetach(fn($request, $pivot) => $_SERVER['roles.canDetach.users']), + )->canDetach(fn ($request, $pivot) => $_SERVER['roles.canDetach.users']), ]; } }