From 317b4e913b85db78b6d9e09e7a083df97f322fdd Mon Sep 17 00:00:00 2001 From: Arthur Kirkosa Date: Tue, 20 Aug 2024 14:41:02 +0300 Subject: [PATCH] fix(Bulk Update): Only Validate Data for Current Item in Loop (#612) * fix(Bulk Update): Only Validate Data for Current Item in Loop * Fix styling * Run pint * Fix styling --------- Co-authored-by: arthurkirkosa --- src/Commands/PublishAuthCommand.php | 2 +- src/Fields/Concerns/Attachable.php | 10 +++---- .../Auth/ForgotPasswordController.php | 2 +- .../RepositoryUpdateBulkController.php | 2 +- src/Http/Controllers/RestResponse.php | 6 ++-- src/LaravelRestifyServiceProvider.php | 2 +- src/Repositories/ValidatingTrait.php | 2 +- src/Restify.php | 4 +-- src/RestifyApplicationServiceProvider.php | 2 +- .../Search/RepositorySearchService.php | 2 +- src/Traits/AuthorizableModels.php | 8 +++--- src/Traits/AuthorizesRequests.php | 2 +- src/Traits/InteractWithSQLight.php | 2 +- src/Traits/InteractWithSearch.php | 2 +- tests/Actions/FieldActionTest.php | 6 ++-- tests/Actions/PerformActionControllerTest.php | 2 +- .../Index/IndexRelatedFeatureTest.php | 4 +-- .../RepositoryAttachControllerTest.php | 2 +- tests/Feature/ActionLogTest.php | 2 +- tests/Feature/Filters/MatchFilterTest.php | 2 +- tests/Fields/FieldTest.php | 28 +++++++++---------- tests/Fields/FileTest.php | 2 +- tests/Fixtures/User/SampleUser.php | 2 +- tests/Fixtures/User/User.php | 2 +- tests/Unit/AdvancedFilterTest.php | 2 +- tests/Unit/MatchableFilterTest.php | 2 +- 26 files changed, 52 insertions(+), 52 deletions(-) diff --git a/src/Commands/PublishAuthCommand.php b/src/Commands/PublishAuthCommand.php index 5f789d92..88dab483 100644 --- a/src/Commands/PublishAuthCommand.php +++ b/src/Commands/PublishAuthCommand.php @@ -68,7 +68,7 @@ public function checkDirectory(string $path): self protected function copyDirectory(string $path, string $stubDirectory, string $format, ?array $actions = []): self { - $filesystem = new Filesystem(); + $filesystem = new Filesystem; collect($filesystem->allFiles(__DIR__.$stubDirectory)) ->filter(function (SplFileInfo $file) use ($actions) { diff --git a/src/Fields/Concerns/Attachable.php b/src/Fields/Concerns/Attachable.php index 0295bc4b..d57dc308 100644 --- a/src/Fields/Concerns/Attachable.php +++ b/src/Fields/Concerns/Attachable.php @@ -89,7 +89,7 @@ public function authorizeToAttach(RestifyRequest $request) ); if (! $this->authorizedToAttach($request, $pivot)) { - throw new AuthorizationException(); + throw new AuthorizationException; } }); @@ -106,7 +106,7 @@ public function authorizeToSync(RestifyRequest $request) ); if (! $this->authorizedToSync($request, $pivot)) { - throw new AuthorizationException(); + throw new AuthorizationException; } }); @@ -123,7 +123,7 @@ public function authorizedToDetach(RestifyRequest $request, Pivot $pivot): bool public function authorizeToDetach(RestifyRequest $request, Pivot $pivot) { if (! $this->authorizedToDetach($request, $pivot)) { - throw new AuthorizationException(); + throw new AuthorizationException; } return $this; @@ -156,8 +156,8 @@ public function initializePivot(RestifyRequest $request, $relationship, $related if ($relationship->withTimestamps) { $pivot->forceFill([ - $relationship->createdAt() => new DateTime(), - $relationship->updatedAt() => new DateTime(), + $relationship->createdAt() => new DateTime, + $relationship->updatedAt() => new DateTime, ]); } diff --git a/src/Http/Controllers/Auth/ForgotPasswordController.php b/src/Http/Controllers/Auth/ForgotPasswordController.php index 0d0e3616..49d8424a 100644 --- a/src/Http/Controllers/Auth/ForgotPasswordController.php +++ b/src/Http/Controllers/Auth/ForgotPasswordController.php @@ -29,7 +29,7 @@ public function __invoke(Request $request) $request->input('url') ?? config('restify.auth.password_reset_url') ); - (new AnonymousNotifiable())->route('mail', $user->email)->notify(new ForgotPasswordNotification($url)); + (new AnonymousNotifiable)->route('mail', $user->email)->notify(new ForgotPasswordNotification($url)); return ok(__('Reset password link sent to your email.')); } diff --git a/src/Http/Controllers/RepositoryUpdateBulkController.php b/src/Http/Controllers/RepositoryUpdateBulkController.php index 9d242198..e6009681 100644 --- a/src/Http/Controllers/RepositoryUpdateBulkController.php +++ b/src/Http/Controllers/RepositoryUpdateBulkController.php @@ -21,7 +21,7 @@ public function __invoke(RepositoryUpdateBulkRequest $request) $repository = $request->repositoryWith($model); return $repository - ->allowToUpdateBulk($request) + ->allowToUpdateBulk($request, $item) ->updateBulk( $request, $id, diff --git a/src/Http/Controllers/RestResponse.php b/src/Http/Controllers/RestResponse.php index 4387401e..fbe3ac1b 100644 --- a/src/Http/Controllers/RestResponse.php +++ b/src/Http/Controllers/RestResponse.php @@ -252,8 +252,8 @@ public function __call($func, $args) public function respond($response = null): JsonResponse { if (! func_num_args()) { - $response = new \stdClass(); - $response->data = new \stdClass(); + $response = new \stdClass; + $response->data = new \stdClass; foreach (static::$RESPONSE_DEFAULT_ATTRIBUTES as $property) { if (isset($this->{$property})) { @@ -537,7 +537,7 @@ public function model(Model $model): self public static function created() { - return (new self())->code(201); + return (new self)->code(201); } public static function index(AbstractPaginator|Paginator $paginator, array $meta = []): JsonResponse diff --git a/src/LaravelRestifyServiceProvider.php b/src/LaravelRestifyServiceProvider.php index dcf2cbac..57201e7e 100644 --- a/src/LaravelRestifyServiceProvider.php +++ b/src/LaravelRestifyServiceProvider.php @@ -62,7 +62,7 @@ public function packageRegistered(): void // Register the main class to use with the facade $this->app->singleton('laravel-restify', function () { - return new Restify(); + return new Restify; }); } diff --git a/src/Repositories/ValidatingTrait.php b/src/Repositories/ValidatingTrait.php index 5f7c9d46..213c40d9 100644 --- a/src/Repositories/ValidatingTrait.php +++ b/src/Repositories/ValidatingTrait.php @@ -171,7 +171,7 @@ public static function validatorForUpdateBulk(RestifyRequest $request, $resource })->toArray(); return Validator::make( - $plainPayload ?? $request->all(), + [$plainPayload] ?? $request->all(), $on->getUpdatingBulkRules($request), $messages )->after(function ($validator) use ($request) { diff --git a/src/Restify.php b/src/Restify.php index 95d2ffce..a3555eac 100644 --- a/src/Restify.php +++ b/src/Restify.php @@ -132,7 +132,7 @@ public static function repositories(array $repositories) (new BootRepository($repository))->boot(); }); - return new static(); + return new static; } /** @@ -149,7 +149,7 @@ public static function repositoriesFrom(string $directory, string $namespace): v return; } - foreach ((new Finder())->in($directory)->files() as $repository) { + foreach ((new Finder)->in($directory)->files() as $repository) { $repository = $namespace.str_replace( ['/', '.php'], ['\\', ''], diff --git a/src/RestifyApplicationServiceProvider.php b/src/RestifyApplicationServiceProvider.php index 4531befb..beee2059 100644 --- a/src/RestifyApplicationServiceProvider.php +++ b/src/RestifyApplicationServiceProvider.php @@ -133,7 +133,7 @@ protected function routes(): void protected function singleton(): void { if (! App::runningUnitTests()) { - $this->app->singletonIf(RelatedDto::class, fn ($app) => new RelatedDto()); + $this->app->singletonIf(RelatedDto::class, fn ($app) => new RelatedDto); } } } diff --git a/src/Services/Search/RepositorySearchService.php b/src/Services/Search/RepositorySearchService.php index aecee850..5f3fbf24 100644 --- a/src/Services/Search/RepositorySearchService.php +++ b/src/Services/Search/RepositorySearchService.php @@ -219,6 +219,6 @@ protected function applyFilters(RestifyRequest $request, Repository $repository, public static function make(): static { - return new static(); + return new static; } } diff --git a/src/Traits/AuthorizableModels.php b/src/Traits/AuthorizableModels.php index ef31f33e..4e661b81 100644 --- a/src/Traits/AuthorizableModels.php +++ b/src/Traits/AuthorizableModels.php @@ -145,7 +145,7 @@ public function authorizeToSync(Request $request, $method, Collection $keys): bo public function authorizeToDetach(Request $request, $method, $model) { if (! static::authorizable()) { - throw new AuthorizationException(); + throw new AuthorizationException; } $authorized = method_exists(Gate::getPolicyFor($this->model()), $method) @@ -153,7 +153,7 @@ public function authorizeToDetach(Request $request, $method, $model) : false; if ($authorized === false) { - throw new AuthorizationException(); + throw new AuthorizationException; } } @@ -194,7 +194,7 @@ public function authorizedToDelete(Request $request): bool public function authorizeTo(Request $request, iterable|string $ability): void { if ($this->authorizedTo($request, $ability) === false) { - throw new AuthorizationException(); + throw new AuthorizationException; } } @@ -213,6 +213,6 @@ public function authorizedTo(Request $request, iterable|string $ability): bool public static function isRepositoryContext(): bool { - return new static() instanceof Repository; + return new static instanceof Repository; } } diff --git a/src/Traits/AuthorizesRequests.php b/src/Traits/AuthorizesRequests.php index 1e81ef1b..c0bb26ce 100644 --- a/src/Traits/AuthorizesRequests.php +++ b/src/Traits/AuthorizesRequests.php @@ -21,7 +21,7 @@ public static function auth($callback) { static::$authUsing = $callback; - return new static(); + return new static; } /** diff --git a/src/Traits/InteractWithSQLight.php b/src/Traits/InteractWithSQLight.php index 618a6e6a..4031024b 100644 --- a/src/Traits/InteractWithSQLight.php +++ b/src/Traits/InteractWithSQLight.php @@ -19,7 +19,7 @@ public static function resolveConnection($connection = null) public static function bootSushi() { - $instance = (new static()); + $instance = (new static); $cacheFileName = 'sushi-'.Str::kebab(str_replace('\\', '', static::class)).'.sqlite'; $cacheDirectory = realpath(config('sushi.cache-path', storage_path('framework/cache'))); $cachePath = $cacheDirectory.'/'.$cacheFileName; diff --git a/src/Traits/InteractWithSearch.php b/src/Traits/InteractWithSearch.php index 52d4e1c8..dd5ab2f8 100644 --- a/src/Traits/InteractWithSearch.php +++ b/src/Traits/InteractWithSearch.php @@ -113,7 +113,7 @@ public static function collectFilters($type): Collection return $type instanceof Filter ? tap($type, fn ($filter) => $filter->column = $filter->column ?? $column) - : tap(new $base(), function (Filter $filter) use ($column, $type) { + : tap(new $base, function (Filter $filter) use ($column, $type) { $filter->type = $type ? $type : 'value'; $filter->column = $column; }); diff --git a/tests/Actions/FieldActionTest.php b/tests/Actions/FieldActionTest.php index 7240344d..e9d63b7f 100644 --- a/tests/Actions/FieldActionTest.php +++ b/tests/Actions/FieldActionTest.php @@ -16,7 +16,7 @@ class FieldActionTest extends IntegrationTestCase #[Test] public function can_use_actionable_field(): void { - $action = new class() extends Action + $action = new class extends Action { public bool $showOnShow = true; @@ -56,7 +56,7 @@ public function handle(RestifyRequest $request, Post $post) #[Test] public function can_use_actionable_field_on_bulk_store(): void { - $action = new class() extends Action + $action = new class extends Action { public bool $showOnShow = true; @@ -101,7 +101,7 @@ public function handle(RestifyRequest $request, Post $post, int $row) #[Test] public function can_use_actionable_field_on_bulk_update(): void { - $action = new class() extends Action + $action = new class extends Action { public bool $showOnShow = true; diff --git a/tests/Actions/PerformActionControllerTest.php b/tests/Actions/PerformActionControllerTest.php index 2fe4f20f..9d8c9cec 100644 --- a/tests/Actions/PerformActionControllerTest.php +++ b/tests/Actions/PerformActionControllerTest.php @@ -64,7 +64,7 @@ public function test_could_perform_action_using_all(): void PostRepository::partialMock() ->shouldReceive('actions') ->andReturn([ - new class() extends Action + new class extends Action { public static $uriKey = 'publish'; diff --git a/tests/Controllers/Index/IndexRelatedFeatureTest.php b/tests/Controllers/Index/IndexRelatedFeatureTest.php index 0ff6bbe7..de301270 100644 --- a/tests/Controllers/Index/IndexRelatedFeatureTest.php +++ b/tests/Controllers/Index/IndexRelatedFeatureTest.php @@ -34,7 +34,7 @@ protected function setUp(): void { parent::setUp(); - $this->app->singletonIf(RelatedDto::class, fn ($app) => new RelatedDto()); + $this->app->singletonIf(RelatedDto::class, fn ($app) => new RelatedDto); } public function test_can_retrieve_nested_relationships(): void @@ -45,7 +45,7 @@ public function test_can_retrieve_nested_relationships(): void 'owner', 'users' => HasMany::make('users', UserRepository::class), 'extraData' => fn () => ['country' => 'Romania'], - 'extraMeta' => new InvokableExtraMeta(), + 'extraMeta' => new InvokableExtraMeta, ]); UserRepository::partialMock() diff --git a/tests/Controllers/RepositoryAttachControllerTest.php b/tests/Controllers/RepositoryAttachControllerTest.php index dcc3a5fb..6332a959 100644 --- a/tests/Controllers/RepositoryAttachControllerTest.php +++ b/tests/Controllers/RepositoryAttachControllerTest.php @@ -186,7 +186,7 @@ public function test_many_to_many_field_can_intercept_attach_method(): void return true; }) - ->attachCallback(new AttachInvokable()), + ->attachCallback(new AttachInvokable), ]); $this->postJson(CompanyRepository::route("$company->id/attach/users"), [ diff --git a/tests/Feature/ActionLogTest.php b/tests/Feature/ActionLogTest.php index bf229781..d5f17246 100644 --- a/tests/Feature/ActionLogTest.php +++ b/tests/Feature/ActionLogTest.php @@ -105,7 +105,7 @@ public function test_can_create_log_for_repository_custom_action(): void $user = User::factory()->create(); - $action = new class() extends Action + $action = new class extends Action { public static $uriKey = 'test action'; }; diff --git a/tests/Feature/Filters/MatchFilterTest.php b/tests/Feature/Filters/MatchFilterTest.php index c5ee0158..1457c811 100644 --- a/tests/Feature/Filters/MatchFilterTest.php +++ b/tests/Feature/Filters/MatchFilterTest.php @@ -16,7 +16,7 @@ class MatchFilterTest extends IntegrationTestCase { public function test_matchable_filter_has_key(): void { - $filter = new class() extends MatchFilter + $filter = new class extends MatchFilter { public ?string $column = 'approved_at'; }; diff --git a/tests/Fields/FieldTest.php b/tests/Fields/FieldTest.php index f0e4545c..a5f068a0 100644 --- a/tests/Fields/FieldTest.php +++ b/tests/Fields/FieldTest.php @@ -82,7 +82,7 @@ public function test_fields_has_default_value() $field->resolveForIndex((object) []); $field->resolveForShow((object) []); $field->resolve((object) []); - $value = $field->serializeToValue(new RestifyRequest()); + $value = $field->serializeToValue(new RestifyRequest); $this->assertEquals('Title', data_get($field->jsonSerialize(), 'value')); $this->assertEquals('Title', $value['title']); @@ -96,7 +96,7 @@ public function test_field_can_have_custom_store_callback(): void 'title' => 'Request value.', ]); - $model = new class() extends Model + $model = new class extends Model { protected $fillable = ['title']; }; @@ -115,7 +115,7 @@ public function test_field_keep_its_value_if_request_empty(): void { $request = new RepositoryStoreRequest([], []); - $model = new class() extends Model + $model = new class extends Model { protected $fillable = ['title']; }; @@ -137,7 +137,7 @@ public function test_field_can_have_custom_update_callback(): void 'title' => 'Request title.', ]); - $model = new class() extends Model + $model = new class extends Model { protected $fillable = ['title']; }; @@ -155,7 +155,7 @@ public function test_field_fill_callback_has_high_priority(): void { $request = new RepositoryStoreRequest([], []); - $model = new class() extends Model + $model = new class extends Model { protected $fillable = ['title']; }; @@ -165,7 +165,7 @@ public function test_field_fill_callback_has_high_priority(): void ->value(function () { return 'from append callback'; }) - ->fillCallback(new InvokableFill()) + ->fillCallback(new InvokableFill) ->storeCallback(function () { return 'from store callback'; }) @@ -193,7 +193,7 @@ public function test_field_fill_from_request() 'title' => 'title from request', ]); - $model = new class() extends Model + $model = new class extends Model { protected $fillable = ['title']; }; @@ -221,7 +221,7 @@ public function test_append_overwrite_the_request_value() 'title' => 'title from request', ]); - $model = new class() extends Model + $model = new class extends Model { protected $fillable = ['title']; }; @@ -249,7 +249,7 @@ public function test_field_after_store_called(): void 'title' => 'After store title', ]); - $model = new class() extends Model + $model = new class extends Model { protected $table = 'posts'; @@ -257,7 +257,7 @@ public function test_field_after_store_called(): void }; /** * @var Field $field */ - $field = Field::new('title')->afterStore(new InvokableAfterStore()); + $field = Field::new('title')->afterStore(new InvokableAfterStore); $field->fillAttribute($request, $model); @@ -268,7 +268,7 @@ public function test_field_after_store_called(): void public function test_field_after_update_called() { - $model = new class() extends Model + $model = new class extends Model { protected $table = 'posts'; @@ -330,7 +330,7 @@ public function test_fill_field_using_label_key() 'custom_title' => 'title from request', ]); - $model = new class() extends Model + $model = new class extends Model { protected $fillable = ['title']; }; @@ -351,7 +351,7 @@ public function test_field_can_be_filled_from_the_append_value() 'title' => 'Title from the request.', ]); - $model = new class() extends Model + $model = new class extends Model { protected $table = 'posts'; @@ -383,7 +383,7 @@ public function test_field_can_be_filled_from_the_append_callback() 'title' => 'Title from the request.', ]); - $model = new class() extends Model + $model = new class extends Model { protected $table = 'posts'; diff --git a/tests/Fields/FileTest.php b/tests/Fields/FileTest.php index b404b6b0..cbf8b04a 100644 --- a/tests/Fields/FileTest.php +++ b/tests/Fields/FileTest.php @@ -19,7 +19,7 @@ public function test_can_correctly_fill_the_main_attribute_and_store_file(): voi Storage::fake(); Storage::fake('public'); - $model = new User(); + $model = new User; $field = AvatarFile::make('avatar'); $field->storeAs(function () { return 'avatar.jpg'; diff --git a/tests/Fixtures/User/SampleUser.php b/tests/Fixtures/User/SampleUser.php index c6e361a7..f5e19269 100644 --- a/tests/Fixtures/User/SampleUser.php +++ b/tests/Fixtures/User/SampleUser.php @@ -17,7 +17,7 @@ public function getEmail() public function createToken($name, array $scopes = []) { - return new class() + return new class { public $accessToken = 'token'; }; diff --git a/tests/Fixtures/User/User.php b/tests/Fixtures/User/User.php index fcbfdd94..500a743e 100644 --- a/tests/Fixtures/User/User.php +++ b/tests/Fixtures/User/User.php @@ -67,7 +67,7 @@ public function getEmail(): string public function createToken($name, array $scopes = []): object { - return new class() + return new class { public $accessToken = 'token'; }; diff --git a/tests/Unit/AdvancedFilterTest.php b/tests/Unit/AdvancedFilterTest.php index 5723493d..4e2a6d69 100644 --- a/tests/Unit/AdvancedFilterTest.php +++ b/tests/Unit/AdvancedFilterTest.php @@ -14,7 +14,7 @@ class AdvancedFilterTest extends IntegrationTestCase { public function test_advanced_filters_can_serialize(): void { - $filter = new class() extends AdvancedFilter + $filter = new class extends AdvancedFilter { public static $uriKey = 'status-filter'; diff --git a/tests/Unit/MatchableFilterTest.php b/tests/Unit/MatchableFilterTest.php index 12079d40..91c8069b 100644 --- a/tests/Unit/MatchableFilterTest.php +++ b/tests/Unit/MatchableFilterTest.php @@ -10,7 +10,7 @@ class MatchableFilterTest extends IntegrationTestCase { public function test_matchable_filter_has_key(): void { - $filter = new class() extends MatchFilter + $filter = new class extends MatchFilter { public ?string $column = 'approved_at'; };