From cbe40650574b0ff19012b20841bd43e3bb8f3987 Mon Sep 17 00:00:00 2001 From: Lupacescu Eduard Date: Sun, 10 May 2020 12:18:46 +0000 Subject: [PATCH] Apply fixes from StyleCI --- routes/api.php | 3 +- src/Repositories/Repository.php | 40 +++++++++---------- src/Restify.php | 11 +++-- src/Services/Search/GlobalSearch.php | 1 - .../Search/RepositorySearchService.php | 2 +- .../GlobalSearchControllerTest.php | 2 - .../RepositoryIndexControllerTest.php | 4 +- .../RepositoryShowControllerTest.php | 6 +-- .../RepositoryUpdateControllerTest.php | 1 - .../Fixtures/Post/PostAuthorizeRepository.php | 5 +-- .../PostWithUnauthorizedFieldsRepository.php | 2 +- 11 files changed, 35 insertions(+), 42 deletions(-) diff --git a/routes/api.php b/routes/api.php index ddfc6fcf2..1d93040ff 100644 --- a/routes/api.php +++ b/routes/api.php @@ -9,8 +9,7 @@ use Illuminate\Support\Facades\Route; // Global Search... -Route::get('/search', '\\' . GlobalSearchController::class); - +Route::get('/search', '\\'.GlobalSearchController::class); // API CRUD Route::get('/{repository}', '\\'.RepositoryIndexController::class); diff --git a/src/Repositories/Repository.php b/src/Repositories/Repository.php index 926daef70..e0ede5084 100644 --- a/src/Repositories/Repository.php +++ b/src/Repositories/Repository.php @@ -223,8 +223,8 @@ public function collectFields(RestifyRequest $request) if ($this instanceof Mergeable) { $fillable = collect($this->resource->getFillable()) - ->filter(fn($attribute) => $fields->contains('attribute', $attribute) === false) - ->map(fn($attribute) => Field::new($attribute)); + ->filter(fn ($attribute) => $fields->contains('attribute', $attribute) === false) + ->map(fn ($attribute) => Field::new($attribute)); $fields = $fields->merge($fillable); } @@ -235,14 +235,14 @@ public function collectFields(RestifyRequest $request) private function indexFields(RestifyRequest $request): Collection { return $this->collectFields($request) - ->filter(fn(Field $field) => !$field->isHiddenOnIndex($request, $this)) + ->filter(fn (Field $field) => ! $field->isHiddenOnIndex($request, $this)) ->values(); } private function showFields(RestifyRequest $request): Collection { return $this->collectFields($request) - ->filter(fn(Field $field) => !$field->isHiddenOnShow($request, $this)) + ->filter(fn (Field $field) => ! $field->isHiddenOnShow($request, $this)) ->values(); } @@ -342,10 +342,10 @@ public static function routes(Router $router, $attributes, $wrap = false) public function resolveShowAttributes(RestifyRequest $request) { $fields = $this->showFields($request) - ->filter(fn(Field $field) => $field->authorize($request)) - ->each(fn(Field $field) => $field->resolveForShow($this)) - ->map(fn(Field $field) => $field->serializeToValue($request)) - ->mapWithKeys(fn($value) => $value) + ->filter(fn (Field $field) => $field->authorize($request)) + ->each(fn (Field $field) => $field->resolveForShow($this)) + ->map(fn (Field $field) => $field->serializeToValue($request)) + ->mapWithKeys(fn ($value) => $value) ->all(); if ($this instanceof Mergeable) { @@ -363,7 +363,7 @@ public function resolveShowAttributes(RestifyRequest $request) return false; } - if (!$field->authorize($request)) { + if (! $field->authorize($request)) { return false; } @@ -384,10 +384,10 @@ public function resolveIndexAttributes($request) { // Resolve the show method, and attach the value to the array $fields = $this->indexFields($request) - ->filter(fn(Field $field) => $field->authorize($request)) - ->each(fn(Field $field) => $field->resolveForIndex($this)) - ->map(fn(Field $field) => $field->serializeToValue($request)) - ->mapWithKeys(fn($value) => $value) + ->filter(fn (Field $field) => $field->authorize($request)) + ->each(fn (Field $field) => $field->resolveForIndex($this)) + ->map(fn (Field $field) => $field->serializeToValue($request)) + ->mapWithKeys(fn ($value) => $value) ->all(); if ($this instanceof Mergeable) { @@ -405,7 +405,7 @@ public function resolveIndexAttributes($request) return false; } - if (!$field->authorize($request)) { + if (! $field->authorize($request)) { return false; } @@ -451,7 +451,7 @@ public function resolveRelationships($request): array /** * @var AbstractPaginator $paginator */ $paginator = $this->resource->{$relation}()->paginate($request->get('relatablePerPage') ?? (static::$defaultRelatablePerPage ?? RestifySearchable::DEFAULT_RELATABLE_PER_PAGE)); - $withs[$relation] = $paginator->getCollection()->map(fn(Model $item) => [ + $withs[$relation] = $paginator->getCollection()->map(fn (Model $item) => [ 'attributes' => $item->toArray(), ]); } @@ -499,7 +499,7 @@ public function index(RestifyRequest $request) return static::resolveWith($value); })->filter(function (self $repository) use ($request) { return $repository->authorizedToShow($request); - })->values()->map(fn(self $repository) => $repository->serializeForIndex($request)); + })->values()->map(fn (self $repository) => $repository->serializeForIndex($request)); return $this->response([ 'meta' => RepositoryCollection::meta($paginator->toArray()), @@ -522,7 +522,7 @@ public function store(RestifyRequest $request) $this->resource->save(); - $this->storeFields($request)->each(fn(Field $field) => $field->invokeAfter($request, $this->resource)); + $this->storeFields($request)->each(fn (Field $field) => $field->invokeAfter($request, $this->resource)); }); static::stored($this->resource, $request); @@ -620,7 +620,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->getShowId($request)), + 'id' => $this->when($this->resource->id, fn () => $this->getShowId($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), @@ -633,7 +633,7 @@ public function serializeForIndex(RestifyRequest $request): array return $this->filter([ 'id' => $this->when($id = $this->getShowId($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->resolveRelationships($request)), $related), 'meta' => $this->when(value($meta = $this->resolveIndexMeta($request)), $meta), ]); @@ -676,6 +676,6 @@ protected static function fillFields(RestifyRequest $request, Model $model, Coll public static function uriTo(Model $model) { - return Restify::path() . '/' . static::uriKey() . '/' . $model->getKey(); + return Restify::path().'/'.static::uriKey().'/'.$model->getKey(); } } diff --git a/src/Restify.php b/src/Restify.php index 68571c709..b1f089609 100644 --- a/src/Restify.php +++ b/src/Restify.php @@ -97,10 +97,10 @@ public static function repositoriesFrom($directory) $repositories = []; foreach ((new Finder)->in($directory)->files() as $repository) { - $repository = $namespace . str_replace( + $repository = $namespace.str_replace( ['/', '.php'], ['\\', ''], - Str::after($repository->getPathname(), app_path() . DIRECTORY_SEPARATOR) + Str::after($repository->getPathname(), app_path().DIRECTORY_SEPARATOR) ); if (is_subclass_of($repository, Repository::class) && (new ReflectionClass($repository))->isInstantiable()) { @@ -122,7 +122,7 @@ public static function repositoriesFrom($directory) public static function path($plus = null) { if (isset($plus)) { - return config('restify.base', '/restify-api') . '/' . $plus; + return config('restify.base', '/restify-api').'/'.$plus; } else { return config('restify.base', '/restify-api'); } @@ -162,8 +162,8 @@ public static function exceptionHandler($callback) public static function globallySearchableRepositories(RestifyRequest $request) { return collect(static::$repositories) - ->filter(fn($repository) => $repository::authorizedToUseRepository($request)) - ->filter(fn($repository) => $repository::$globallySearchable) + ->filter(fn ($repository) => $repository::authorizedToUseRepository($request)) + ->filter(fn ($repository) => $repository::$globallySearchable) ->sortBy(static::sortResourcesWith()) ->all(); } @@ -174,5 +174,4 @@ public static function sortResourcesWith() return $resource::label(); }; } - } diff --git a/src/Services/Search/GlobalSearch.php b/src/Services/Search/GlobalSearch.php index 0aa131cb4..182e0312c 100644 --- a/src/Services/Search/GlobalSearch.php +++ b/src/Services/Search/GlobalSearch.php @@ -80,4 +80,3 @@ protected function getSearchResults() return collect($results)->sortKeys()->all(); } } - diff --git a/src/Services/Search/RepositorySearchService.php b/src/Services/Search/RepositorySearchService.php index 593733fd1..1cbf6dbf1 100644 --- a/src/Services/Search/RepositorySearchService.php +++ b/src/Services/Search/RepositorySearchService.php @@ -173,6 +173,6 @@ public function setOrder($query, $param) protected function applyIndexQuery(RestifyRequest $request, Repository $repository) { - return fn($query) => $repository::indexQuery($request, $query); + return fn ($query) => $repository::indexQuery($request, $query); } } diff --git a/tests/Controllers/GlobalSearchControllerTest.php b/tests/Controllers/GlobalSearchControllerTest.php index 7ec4b6674..a64be8eb3 100644 --- a/tests/Controllers/GlobalSearchControllerTest.php +++ b/tests/Controllers/GlobalSearchControllerTest.php @@ -20,7 +20,6 @@ public function test_global_search_returns_matches() factory(User::class)->create(['name' => 'First user']); factory(User::class)->create(['name' => 'Second user']); - $response = $this ->withoutExceptionHandling() ->getJson('/restify-api/search?search=Second'); @@ -39,7 +38,6 @@ public function test_global_search_filter_out_unauthorized_repositories() factory(Post::class)->create(); factory(User::class)->create(); - $response = $this ->withoutExceptionHandling() ->getJson('/restify-api/search?search=1'); diff --git a/tests/Controllers/RepositoryIndexControllerTest.php b/tests/Controllers/RepositoryIndexControllerTest.php index 87e8764cc..442dec760 100644 --- a/tests/Controllers/RepositoryIndexControllerTest.php +++ b/tests/Controllers/RepositoryIndexControllerTest.php @@ -3,8 +3,8 @@ namespace Binaryk\LaravelRestify\Tests\Controllers; use Binaryk\LaravelRestify\Contracts\RestifySearchable; -use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository; use Binaryk\LaravelRestify\Tests\Fixtures\Post\Post; +use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository; use Binaryk\LaravelRestify\Tests\IntegrationTest; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -124,7 +124,7 @@ public function test_index_unmergeable_repository_containes_only_explicitly_defi 'attributes' => [ 'user_id', 'title', - 'description' + 'description', ], ]], ]); diff --git a/tests/Controllers/RepositoryShowControllerTest.php b/tests/Controllers/RepositoryShowControllerTest.php index eb232e8db..84ad5b51a 100644 --- a/tests/Controllers/RepositoryShowControllerTest.php +++ b/tests/Controllers/RepositoryShowControllerTest.php @@ -50,7 +50,7 @@ public function test_show_will_take_into_consideration_show_callback() { $_SERVER['postAuthorize.can.see.title'] = true; - factory(Post::class)->create(['title' => 'Eduard',]); + factory(Post::class)->create(['title' => 'Eduard']); $response = $this->getJson('/restify-api/post-authorizes/1'); @@ -59,7 +59,7 @@ public function test_show_will_take_into_consideration_show_callback() public function test_show_unmergeable_repository_containes_only_explicitly_defined_fields() { - factory(Post::class)->create(['title' => 'Eduard',]); + factory(Post::class)->create(['title' => 'Eduard']); $response = $this->getJson('/restify-api/posts/1') ->assertJsonStructure([ @@ -78,7 +78,7 @@ public function test_show_unmergeable_repository_containes_only_explicitly_defin public function test_show_mergeable_repository_containes_model_attributes_and_local_fields() { - factory(Post::class)->create(['title' => 'Eduard',]); + factory(Post::class)->create(['title' => 'Eduard']); $this->getJson('/restify-api/posts-mergeable/1') ->assertJsonStructure([ diff --git a/tests/Controllers/RepositoryUpdateControllerTest.php b/tests/Controllers/RepositoryUpdateControllerTest.php index 365907af2..af2db9316 100644 --- a/tests/Controllers/RepositoryUpdateControllerTest.php +++ b/tests/Controllers/RepositoryUpdateControllerTest.php @@ -113,4 +113,3 @@ public function test_will_not_update_readonly_fields() $this->assertNull($r->json('data.attributes.image')); } } - diff --git a/tests/Fixtures/Post/PostAuthorizeRepository.php b/tests/Fixtures/Post/PostAuthorizeRepository.php index c07234041..69a6c8183 100644 --- a/tests/Fixtures/Post/PostAuthorizeRepository.php +++ b/tests/Fixtures/Post/PostAuthorizeRepository.php @@ -12,15 +12,14 @@ class PostAuthorizeRepository extends Repository public static $globallySearchable = false; - public function fields(RestifyRequest $request) { return [ Field::new('user_id'), Field::new('title') - ->canSee(fn() => $_SERVER['postAuthorize.can.see.title']) - ->showCallback(fn($value) => strtoupper($value)), + ->canSee(fn () => $_SERVER['postAuthorize.can.see.title']) + ->showCallback(fn ($value) => strtoupper($value)), Field::new('description')->storingRules('required')->messages([ 'required' => 'Description field is required', diff --git a/tests/Fixtures/Post/PostWithUnauthorizedFieldsRepository.php b/tests/Fixtures/Post/PostWithUnauthorizedFieldsRepository.php index 80db98271..80db10f9d 100644 --- a/tests/Fixtures/Post/PostWithUnauthorizedFieldsRepository.php +++ b/tests/Fixtures/Post/PostWithUnauthorizedFieldsRepository.php @@ -19,7 +19,7 @@ public function fields(RestifyRequest $request) return [ Field::new('user_id'), - Field::new('title')->canUpdate(fn() => $_SERVER['posts.authorizable.title']), + Field::new('title')->canUpdate(fn () => $_SERVER['posts.authorizable.title']), ]; } }