From e518916a32e6431a45cc49a95b2b002f04003103 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Mon, 30 Nov 2020 13:22:29 +0000 Subject: [PATCH] Apply fixes from StyleCI --- src/Fields/Field.php | 2 +- src/Repositories/Repository.php | 71 ++++++++++++------------- tests/Fields/BelongsToFieldTest.php | 8 +-- tests/Fields/BelongsToManyFieldTest.php | 8 +-- tests/Fields/HasManyTest.php | 30 +++++------ tests/Fields/HasOneFieldTest.php | 4 +- tests/Fields/MorphToManyFieldTest.php | 2 +- 7 files changed, 62 insertions(+), 63 deletions(-) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 7bd89c403..88f7899ca 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -251,7 +251,7 @@ protected function fillAttributeFromRequest(RestifyRequest $request, $model, $at ? $attribute : "{$bulkRow}.{$attribute}"; - if(! ($request->exists($attribute) || $request->input($attribute))) { + if (! ($request->exists($attribute) || $request->input($attribute))) { return; } diff --git a/src/Repositories/Repository.php b/src/Repositories/Repository.php index d3ed73352..dff1b3cd3 100644 --- a/src/Repositories/Repository.php +++ b/src/Repositories/Repository.php @@ -10,7 +10,6 @@ use Binaryk\LaravelRestify\Fields\Field; use Binaryk\LaravelRestify\Fields\FieldCollection; use Binaryk\LaravelRestify\Filter; -use Binaryk\LaravelRestify\Http\Requests\RepositoryIndexRequest; use Binaryk\LaravelRestify\Http\Requests\RepositoryShowRequest; use Binaryk\LaravelRestify\Http\Requests\RepositoryStoreBulkRequest; use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; @@ -404,16 +403,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->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->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) { @@ -454,16 +453,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) { @@ -516,28 +515,28 @@ public function resolveRelationships($request): array if (! $this->isEagerState() && $request instanceof RepositoryShowRequest) { $this->collectFields($request) ->forEager($request, $this) - ->filter(fn(EagerField $field) => $field->isShownOnShow($request, $this)) - ->each(fn(EagerField $field) => $withs->put($field->attribute, $field->resolve($this)->value)); + ->filter(fn (EagerField $field) => $field->isShownOnShow($request, $this)) + ->each(fn (EagerField $field) => $withs->put($field->attribute, $field->resolve($this)->value)); } collect(str_getcsv($request->input('related'))) - ->filter(fn($relation) => in_array($relation, static::getRelated())) + ->filter(fn ($relation) => in_array($relation, static::getRelated())) ->each(function ($relation) use ($request, $withs) { $paginator = $this->resource->relationLoaded($relation) ? $this->resource->{$relation} : $this->resource->{$relation}(); collect([ - Builder::class => fn() => $withs->put($relation, $paginator->take($request->input('relatablePerPage') ?? (static::$defaultRelatablePerPage ?? RestifySearchable::DEFAULT_RELATABLE_PER_PAGE))->get()), + Builder::class => fn () => $withs->put($relation, $paginator->take($request->input('relatablePerPage') ?? (static::$defaultRelatablePerPage ?? RestifySearchable::DEFAULT_RELATABLE_PER_PAGE))->get()), - Relation::class => fn() => $withs->put($relation, $paginator->take($request->input('relatablePerPage') ?? (static::$defaultRelatablePerPage ?? RestifySearchable::DEFAULT_RELATABLE_PER_PAGE))->get()), + Relation::class => fn () => $withs->put($relation, $paginator->take($request->input('relatablePerPage') ?? (static::$defaultRelatablePerPage ?? RestifySearchable::DEFAULT_RELATABLE_PER_PAGE))->get()), - 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() => $withs->put($relation, $paginator) + ])->first(fn ($fn, $class) => $paginator instanceof $class, + fn () => $withs->put($relation, $paginator) )(); }); @@ -591,14 +590,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)), ] ) ); @@ -648,7 +647,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); @@ -679,7 +678,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; }); @@ -706,7 +705,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() @@ -735,7 +734,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(); @@ -768,7 +767,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() @@ -800,18 +799,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; } @@ -896,7 +895,7 @@ public function response($content = '', $status = 200, array $headers = []): Res public function serializeForShow(RestifyRequest $request): array { return $this->filter([ - 'id' => $this->when($this->resource->id, fn() => $this->getId($request)), + 'id' => $this->when($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), @@ -909,7 +908,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), ]); @@ -951,7 +950,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) @@ -963,12 +962,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/Fields/BelongsToFieldTest.php b/tests/Fields/BelongsToFieldTest.php index c9e9e721d..24f6ffb65 100644 --- a/tests/Fields/BelongsToFieldTest.php +++ b/tests/Fields/BelongsToFieldTest.php @@ -42,7 +42,7 @@ public function test_present_on_relations() 'user_id' => factory(User::class), ]); - $this->get(PostWithUserRepository::uriKey() . "/$post->id") + $this->get(PostWithUserRepository::uriKey()."/$post->id") ->assertJsonStructure([ 'data' => [ 'relationships' => [ @@ -61,7 +61,7 @@ public function test_unauthorized_see_relationship() tap(factory(Post::class)->create([ 'user_id' => factory(User::class), ]), function ($post) { - $this->get(PostWithUserRepository::uriKey() . "/{$post->id}") + $this->get(PostWithUserRepository::uriKey()."/{$post->id}") ->assertForbidden(); }); } @@ -145,7 +145,7 @@ public function test_field_used_when_updating() 'user_id' => factory(User::class), ]), function ($post) { $newOwner = factory(User::class)->create(); - $this->put(PostWithUserRepository::uriKey() . "/{$post->id}", [ + $this->put(PostWithUserRepository::uriKey()."/{$post->id}", [ 'title' => 'Can change post owner.', 'user' => $newOwner->id, ])->assertOk(); @@ -165,7 +165,7 @@ public function test_unauthorized_via_policy_when_updating() ]), function ($post) { $firstOwnerId = $post->user->id; $newOwner = factory(User::class)->create(); - $this->put(PostWithUserRepository::uriKey() . "/{$post->id}", [ + $this->put(PostWithUserRepository::uriKey()."/{$post->id}", [ 'title' => 'Can change post owner.', 'user' => $newOwner->id, ])->assertForbidden(); diff --git a/tests/Fields/BelongsToManyFieldTest.php b/tests/Fields/BelongsToManyFieldTest.php index 4b3a85af7..77a6d3868 100644 --- a/tests/Fields/BelongsToManyFieldTest.php +++ b/tests/Fields/BelongsToManyFieldTest.php @@ -30,7 +30,7 @@ public function test_displays_on_relationships_show() ); }); - $this->get(CompanyWithUsersRepository::uriKey() . "/{$company->id}") + $this->get(CompanyWithUsersRepository::uriKey()."/{$company->id}") ->assertJsonStructure([ 'data' => [ 'relationships' => [ @@ -50,14 +50,14 @@ public function test_can_hide_relationships() $_SERVER['hide_users_from_show'] = true; - $this->get(CompanyWithUsersRepository::uriKey() . "/{$company->id}") + $this->get(CompanyWithUsersRepository::uriKey()."/{$company->id}") ->assertJsonStructure([ 'data' => [], ])->assertJsonMissing([ [ 'relationships' => [ 'users' => [], - ]] + ], ], ]); } @@ -77,7 +77,7 @@ public function test_ignored_when_storing() [ 'relationships' => [ 'users' => [], - ]] + ], ], ]); } } diff --git a/tests/Fields/HasManyTest.php b/tests/Fields/HasManyTest.php index 7601ed36d..ab7c02121 100644 --- a/tests/Fields/HasManyTest.php +++ b/tests/Fields/HasManyTest.php @@ -42,7 +42,7 @@ public function test_present_on_relations() 'user_id' => factory(User::class), ]); - $this->get(UserWithPosts::uriKey() . "/$post->id") + $this->get(UserWithPosts::uriKey()."/$post->id") ->assertJsonStructure([ 'data' => [ 'relationships' => [ @@ -58,7 +58,7 @@ public function test_paginated_on_relation() $this->mockPosts($user->id, 22); }); - $this->get(UserWithPosts::uriKey() . "/{$user->id}?relatablePerPage=20") + $this->get(UserWithPosts::uriKey()."/{$user->id}?relatablePerPage=20") ->assertJsonCount(20, 'data.relationships.posts'); } @@ -71,7 +71,7 @@ public function test_unauthorized_see_relationship_posts() $this->mockPosts($user->id, 20); }); - $this->get(UserWithPosts::uriKey() . "/$user->id") + $this->get(UserWithPosts::uriKey()."/$user->id") ->assertForbidden(); } @@ -103,7 +103,7 @@ public function test_can_display_other_pages() HasMany::make('posts', 'posts', PostRepository::class), ]); - $this->get(UserWithPosts::uriKey() . "/{$u->id}/posts?perPage=5") + $this->get(UserWithPosts::uriKey()."/{$u->id}/posts?perPage=5") ->assertJsonCount(5, 'data'); } @@ -127,7 +127,7 @@ public function test_can_apply_filters() HasMany::make('posts', 'posts', PostRepository::class), ]); - $this->get(UserWithPosts::uriKey() . "/{$u->id}/posts?title=wew") + $this->get(UserWithPosts::uriKey()."/{$u->id}/posts?title=wew") ->assertJsonCount(1, 'data'); } @@ -151,12 +151,12 @@ public function test_filter_unauthorized_posts() HasMany::make('posts', 'posts', PostRepository::class), ]); - $this->get(UserWithPosts::uriKey() . "/{$u->id}/posts") + $this->get(UserWithPosts::uriKey()."/{$u->id}/posts") ->assertJsonCount(0, 'data'); $_SERVER['restify.post.allowRestify'] = false; - $this->get(UserWithPosts::uriKey() . "/{$u->id}/posts") + $this->get(UserWithPosts::uriKey()."/{$u->id}/posts") ->assertForbidden(); } @@ -179,7 +179,7 @@ public function test_can_store() HasMany::make('posts', 'posts', PostRepository::class), ]); - $this->post(UserWithPosts::uriKey() . "/{$u->id}/posts", [ + $this->post(UserWithPosts::uriKey()."/{$u->id}/posts", [ 'title' => 'Test', ])->assertCreated(); @@ -200,7 +200,7 @@ public function test_can_show() HasMany::make('posts', 'posts', PostRepository::class), ]); - $this->get(UserWithPosts::uriKey() . "/{$userId}/posts/{$post->id}", [ + $this->get(UserWithPosts::uriKey()."/{$userId}/posts/{$post->id}", [ 'title' => 'Test', ])->assertJsonStructure([ 'data' => ['attributes'], @@ -214,7 +214,7 @@ public function test_unauthorized_show() $post = $this->mockPosts($userId = $this->mockUsers()->first()->id, 1)->first(); - $this->get(UserWithPosts::uriKey() . "/{$userId}/posts/{$post->id}", [ + $this->get(UserWithPosts::uriKey()."/{$userId}/posts/{$post->id}", [ 'title' => 'Test', ])->assertForbidden(); } @@ -227,7 +227,7 @@ public function test_404_post_from_different_owner() $this->mockPosts($userId = $this->mockUsers()->first()->id, 1)->first(); $secondPost = $this->mockPosts($secondUserId = $this->mockUsers()->first()->id, 1)->first(); - $this->get(UserWithPosts::uriKey() . "/{$userId}/posts/{$secondPost->id}") + $this->get(UserWithPosts::uriKey()."/{$userId}/posts/{$secondPost->id}") ->assertNotFound(); } @@ -238,7 +238,7 @@ public function test_change_post() $post = $this->mockPosts($userId = $this->mockUsers()->first()->id, 1)->first(); - $this->post(UserWithPosts::uriKey() . "/{$userId}/posts/{$post->id}", [ + $this->post(UserWithPosts::uriKey()."/{$userId}/posts/{$post->id}", [ 'title' => 'Test', ])->assertOk(); @@ -254,7 +254,7 @@ public function test_delete_post() $this->assertDatabaseCount('posts', 1); - $this->delete(UserWithPosts::uriKey() . "/{$userId}/posts/{$post->id}", [ + $this->delete(UserWithPosts::uriKey()."/{$userId}/posts/{$post->id}", [ 'title' => 'Test', ])->assertNoContent(); @@ -270,7 +270,7 @@ public function test_unauthorized_delete_post() $this->assertDatabaseCount('posts', 1); - $this->delete(UserWithPosts::uriKey() . "/{$userId}/posts/{$post->id}", [ + $this->delete(UserWithPosts::uriKey()."/{$userId}/posts/{$post->id}", [ 'title' => 'Test', ])->assertForbidden(); @@ -280,7 +280,7 @@ public function test_unauthorized_delete_post() public function test_it_validates_fields_when_storing_related() { $userId = $this->mockUsers()->first()->id; - $this->post(UserWithPosts::uriKey() . "/{$userId}/posts", [ + $this->post(UserWithPosts::uriKey()."/{$userId}/posts", [ /*'title' => 'Wew',*/ ])->assertStatus(400); } diff --git a/tests/Fields/HasOneFieldTest.php b/tests/Fields/HasOneFieldTest.php index 344a78e94..c41c6e21f 100644 --- a/tests/Fields/HasOneFieldTest.php +++ b/tests/Fields/HasOneFieldTest.php @@ -41,7 +41,7 @@ public function test_present_on_relations() 'user_id' => factory(User::class), ]); - $this->get(UserWithPostRepository::uriKey() . "/$post->id") + $this->get(UserWithPostRepository::uriKey()."/$post->id") ->assertJsonStructure([ 'data' => [ 'relationships' => [ @@ -60,7 +60,7 @@ public function test_unauthorized_see_relationship() tap(factory(Post::class)->create([ 'user_id' => factory(User::class), ]), function ($post) { - $this->get(UserWithPostRepository::uriKey() . "/{$post->id}") + $this->get(UserWithPostRepository::uriKey()."/{$post->id}") ->assertForbidden(); }); } diff --git a/tests/Fields/MorphToManyFieldTest.php b/tests/Fields/MorphToManyFieldTest.php index 37331d4d6..d3f841ae0 100644 --- a/tests/Fields/MorphToManyFieldTest.php +++ b/tests/Fields/MorphToManyFieldTest.php @@ -30,7 +30,7 @@ public function test_morph_to_many_displays_in_relationships() ); }); - $this->get(UserWithRolesRepository::uriKey() . "/$user->id") + $this->get(UserWithRolesRepository::uriKey()."/$user->id") ->assertJsonStructure([ 'data' => [ 'relationships' => [