From 52671f9d4975798de224bc46c25ea3ff5adbf67f Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Thu, 14 Mar 2024 14:50:38 +0800 Subject: [PATCH] fix: pint --- src/DataTablesEditor.php | 104 ++++++++---------- src/Generators/DataTablesEditorCommand.php | 30 ++--- tests/Editors/UsersDataTableEditor.php | 8 +- .../UsersWithEventsDataTableEditor.php | 8 +- tests/Feature/DataTablesEditorCreateTest.php | 12 +- tests/Feature/DataTablesEditorEditTest.php | 22 ++-- tests/Feature/DataTablesEditorRemoveTest.php | 12 +- tests/TestCase.php | 6 +- 8 files changed, 85 insertions(+), 117 deletions(-) diff --git a/src/DataTablesEditor.php b/src/DataTablesEditor.php index 3eddc19..86ec3c5 100644 --- a/src/DataTablesEditor.php +++ b/src/DataTablesEditor.php @@ -96,7 +96,6 @@ abstract class DataTablesEditor /** * Process dataTables editor action request. * - * @param Request $request * @return JsonResponse|mixed * * @throws DataTablesEditorException @@ -113,7 +112,7 @@ public function process(Request $request) return $this->{$this->action}($request); } catch (Exception $exception) { $error = config('app.debug') - ? 'Server Error: ' . $exception->getMessage() + ? 'Server Error: '.$exception->getMessage() : $this->getUseFriendlyErrorMessage(); app('log')->error($exception); @@ -133,8 +132,6 @@ protected function getUseFriendlyErrorMessage() /** * Display success data in dataTables editor format. * - * @param array $data - * @param array $errors * @param string $error * @return JsonResponse */ @@ -144,16 +141,16 @@ protected function toJson(array $data, array $errors = [], $error = '') $response = [ 'action' => $this->action, - 'data' => $data, + 'data' => $data, ]; if ($error) { - $code = 422; + $code = 422; $response['error'] = $error; } if ($errors) { - $code = 422; + $code = 422; $response['fieldErrors'] = $errors; } @@ -163,29 +160,28 @@ protected function toJson(array $data, array $errors = [], $error = '') /** * Process create action request. * - * @param Request $request * @return JsonResponse * * @throws \Exception */ public function create(Request $request) { - $model = $this->resolveModel(); + $model = $this->resolveModel(); $connection = $model->getConnection(); - $affected = []; - $errors = []; + $affected = []; + $errors = []; $connection->beginTransaction(); foreach ($request->get('data') as $data) { $this->currentData = $data; - $instance = $model->newInstance(); + $instance = $model->newInstance(); $validator = $this->getValidationFactory() - ->make( - $data, - $this->createRules(), $this->messages() + $this->createMessages(), - $this->attributes() - ); + ->make( + $data, + $this->createRules(), $this->messages() + $this->createMessages(), + $this->attributes() + ); if ($validator->fails()) { foreach ($this->formatErrors($validator) as $error) { $errors[] = $error; @@ -284,7 +280,6 @@ public function attributes() } /** - * @param Validator $validator * @return array */ protected function formatErrors(Validator $validator) @@ -293,7 +288,7 @@ protected function formatErrors(Validator $validator) collect($validator->errors())->each(function ($error, $key) use (&$errors) { $errors[] = [ - 'name' => $key, + 'name' => $key, 'status' => $error[0], ]; }); @@ -304,7 +299,6 @@ protected function formatErrors(Validator $validator) /** * Process restore action request. * - * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function restore(Request $request) @@ -317,26 +311,25 @@ public function restore(Request $request) /** * Process edit action request. * - * @param Request $request * @return JsonResponse */ public function edit(Request $request) { $connection = $this->getBuilder()->getConnection(); - $affected = []; - $errors = []; + $affected = []; + $errors = []; $connection->beginTransaction(); foreach ($request->get('data') as $key => $data) { $this->currentData = $data; - $model = $this->getBuilder()->findOrFail($key); + $model = $this->getBuilder()->findOrFail($key); $validator = $this->getValidationFactory() - ->make( - $data, - $this->editRules($model), $this->messages() + $this->editMessages(), - $this->attributes() - ); + ->make( + $data, + $this->editRules($model), $this->messages() + $this->editMessages(), + $this->attributes() + ); if ($validator->fails()) { foreach ($this->formatErrors($validator) as $error) { $errors[] = $error; @@ -395,7 +388,6 @@ protected function getBuilder() /** * Get edit action validation rules. * - * @param Model $model * @return array */ public function editRules(Model $model) @@ -418,7 +410,6 @@ protected function editMessages() /** * Process force delete action request. * - * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse * * @throws \Exception @@ -433,7 +424,6 @@ public function forceDelete(Request $request) /** * Process remove action request. * - * @param Request $request * @return JsonResponse * * @throws \Exception @@ -441,20 +431,20 @@ public function forceDelete(Request $request) public function remove(Request $request) { $connection = $this->getBuilder()->getConnection(); - $affected = []; - $errors = []; + $affected = []; + $errors = []; $connection->beginTransaction(); foreach ($request->get('data') as $key => $data) { $this->currentData = $data; - $model = $this->getBuilder()->findOrFail($key); + $model = $this->getBuilder()->findOrFail($key); $validator = $this->getValidationFactory() - ->make( - $data, - $this->removeRules($model), $this->messages() + $this->removeMessages(), - $this->attributes() - ); + ->make( + $data, + $this->removeRules($model), $this->messages() + $this->removeMessages(), + $this->attributes() + ); if ($validator->fails()) { foreach ($this->formatErrors($validator) as $error) { $errors[] = $error['status']; @@ -502,7 +492,6 @@ public function remove(Request $request) /** * Get remove action validation rules. * - * @param Model $model * @return array */ public function removeRules(Model $model) @@ -525,8 +514,6 @@ protected function removeMessages() /** * Get remove query exception message. * - * @param QueryException $exception - * @param Model $model * @return string */ protected function removeExceptionMessage(QueryException $exception, Model $model) @@ -573,22 +560,21 @@ public function unguard($state = true) /** * Handle uploading of file. * - * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function upload(Request $request) { - $field = $request->input('uploadField'); + $field = $request->input('uploadField'); $storage = $this->getDisk(); try { - $rules = $this->uploadRules(); + $rules = $this->uploadRules(); $fieldRules = ['upload' => $rules[$field] ?? []]; $this->validate($request, $fieldRules, $this->messages(), $this->attributes()); $uploadedFile = $request->file('upload'); - $id = $this->storeUploadedFile($field, $uploadedFile); + $id = $this->storeUploadedFile($field, $uploadedFile); if (method_exists($this, 'uploaded')) { $id = $this->uploaded($id); @@ -596,16 +582,16 @@ public function upload(Request $request) return response()->json([ 'action' => $this->action, - 'data' => [], - 'files' => [ + 'data' => [], + 'files' => [ 'files' => [ $id => [ - 'filename' => $id, + 'filename' => $id, 'original_name' => $uploadedFile->getClientOriginalName(), - 'size' => $uploadedFile->getSize(), - 'directory' => $this->getUploadDirectory(), - 'disk' => $this->disk, - 'url' => $storage->url($id), + 'size' => $uploadedFile->getSize(), + 'directory' => $this->getUploadDirectory(), + 'disk' => $this->disk, + 'url' => $storage->url($id), ], ], ], @@ -615,11 +601,11 @@ public function upload(Request $request) ]); } catch (ValidationException $exception) { return response()->json([ - 'action' => $this->action, - 'data' => [], + 'action' => $this->action, + 'data' => [], 'fieldErrors' => [ [ - 'name' => $field, + 'name' => $field, 'status' => str_replace('upload', $field, $exception->errors()['upload'][0]), ], ], @@ -639,12 +625,11 @@ public function uploadRules() /** * @param string $field - * @param UploadedFile $uploadedFile * @return string */ protected function getUploadedFilename($field, UploadedFile $uploadedFile) { - return date('Ymd_His') . '_' . $uploadedFile->getClientOriginalName(); + return date('Ymd_His').'_'.$uploadedFile->getClientOriginalName(); } /** @@ -665,7 +650,6 @@ protected function getDisk() /** * @param string $field - * @param UploadedFile $uploadedFile * @return false|string */ protected function storeUploadedFile($field, UploadedFile $uploadedFile) diff --git a/src/Generators/DataTablesEditorCommand.php b/src/Generators/DataTablesEditorCommand.php index afcea38..d694057 100644 --- a/src/Generators/DataTablesEditorCommand.php +++ b/src/Generators/DataTablesEditorCommand.php @@ -35,7 +35,6 @@ class DataTablesEditorCommand extends GeneratorCommand * Build the class with the given name. * * @param string $name - * @return string * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ @@ -48,15 +47,12 @@ protected function buildClass($name): string /** * Replace model name. - * - * @param string $stub - * @return string */ protected function replaceModel(string &$stub): string { $model = explode('\\', $this->getModel()); $model = array_pop($model); - $stub = str_replace('ModelName', $model, $stub); + $stub = str_replace('ModelName', $model, $stub); return $stub; } @@ -66,20 +62,19 @@ protected function replaceModel(string &$stub): string */ protected function getModel(): string { - $name = $this->getNameInput(); - $rootNamespace = $this->laravel->getNamespace(); - $model = $this->option('model') || $this->option('model-namespace'); + $name = $this->getNameInput(); + $rootNamespace = $this->laravel->getNamespace(); + $model = $this->option('model') || $this->option('model-namespace'); $modelNamespace = $this->option('model-namespace') ? $this->option('model-namespace') : $this->laravel['config']->get('datatables-buttons.namespace.model'); return $model - ? ($modelNamespace ?? $rootNamespace) . '\\' . Str::singular($name) - : $rootNamespace . '\\Models\\User'; + ? ($modelNamespace ?? $rootNamespace).'\\'.Str::singular($name) + : $rootNamespace.'\\Models\\User'; } /** * Replace model import. * - * @param string $stub * @return $this */ protected function replaceModelImport(string &$stub): DataTablesEditorCommand @@ -93,21 +88,16 @@ protected function replaceModelImport(string &$stub): DataTablesEditorCommand /** * Get the stub file for the generator. - * - * @return string */ protected function getStub(): string { $path = $this->laravel['config']->get('datatables-buttons.stub'); - return $path ? base_path($path) . '/editor.stub' : __DIR__ . '/stubs/editor.stub'; + return $path ? base_path($path).'/editor.stub' : __DIR__.'/stubs/editor.stub'; } /** * Replace the filename. - * - * @param string $stub - * @return string */ protected function replaceFilename(string &$stub): string { @@ -122,7 +112,6 @@ protected function replaceFilename(string &$stub): string * Parse the name and format according to the root namespace. * * @param string $name - * @return string */ protected function qualifyClass($name): string { @@ -140,17 +129,16 @@ protected function qualifyClass($name): string $name .= $this->type; } - return $this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name; + return $this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name; } /** * Get the default namespace for the class. * * @param string $rootNamespace - * @return string */ protected function getDefaultNamespace($rootNamespace): string { - return $rootNamespace . '\\' . $this->laravel['config']->get('datatables-buttons.namespace.base', 'DataTables'); + return $rootNamespace.'\\'.$this->laravel['config']->get('datatables-buttons.namespace.base', 'DataTables'); } } diff --git a/tests/Editors/UsersDataTableEditor.php b/tests/Editors/UsersDataTableEditor.php index 19ade98..15d81fd 100644 --- a/tests/Editors/UsersDataTableEditor.php +++ b/tests/Editors/UsersDataTableEditor.php @@ -20,28 +20,26 @@ public function createRules() { return [ 'email' => 'required|email', - 'name' => 'required', + 'name' => 'required', ]; } /** * Get edit action validation rules. * - * @param Model $model * @return array */ public function editRules(Model $model) { return [ - 'email' => 'sometimes|required|email|' . Rule::unique('users')->ignore($model->getKey()), - 'name' => 'sometimes|required', + 'email' => 'sometimes|required|email|'.Rule::unique('users')->ignore($model->getKey()), + 'name' => 'sometimes|required', ]; } /** * Get remove action validation rules. * - * @param Model $model * @return array */ public function removeRules(Model $model) diff --git a/tests/Editors/UsersWithEventsDataTableEditor.php b/tests/Editors/UsersWithEventsDataTableEditor.php index 6bb02b9..d8d6546 100644 --- a/tests/Editors/UsersWithEventsDataTableEditor.php +++ b/tests/Editors/UsersWithEventsDataTableEditor.php @@ -20,7 +20,7 @@ public function createRules() { return [ 'email' => 'required|email', - 'name' => 'required', + 'name' => 'required', ]; } @@ -32,14 +32,13 @@ public function created($model, $data) /** * Get edit action validation rules. * - * @param Model $model * @return array */ public function editRules(Model $model) { return [ - 'email' => 'sometimes|required|email|' . Rule::unique('users')->ignore($model->getKey()), - 'name' => 'sometimes|required', + 'email' => 'sometimes|required|email|'.Rule::unique('users')->ignore($model->getKey()), + 'name' => 'sometimes|required', ]; } @@ -56,7 +55,6 @@ public function saved($model, $data) /** * Get remove action validation rules. * - * @param Model $model * @return array */ public function removeRules(Model $model) diff --git a/tests/Feature/DataTablesEditorCreateTest.php b/tests/Feature/DataTablesEditorCreateTest.php index 235784e..95f3f3a 100644 --- a/tests/Feature/DataTablesEditorCreateTest.php +++ b/tests/Feature/DataTablesEditorCreateTest.php @@ -12,9 +12,9 @@ public function it_can_process_create_request() { $response = $this->postJson('users', [ 'action' => 'create', - 'data' => [ + 'data' => [ 0 => [ - 'name' => 'Taylor', + 'name' => 'Taylor', 'email' => 'taylor@laravel.com', ], ], @@ -34,9 +34,9 @@ public function it_allows_created_callback_and_returns_the_modified_model() { $response = $this->postJson('usersWithEvents', [ 'action' => 'create', - 'data' => [ + 'data' => [ 0 => [ - 'name' => 'Taylor', + 'name' => 'Taylor', 'email' => 'taylor@laravel.com', ], ], @@ -58,9 +58,9 @@ public function it_can_validate_invalid_inputs() { $response = $this->postJson('users', [ 'action' => 'create', - 'data' => [ + 'data' => [ [ - 'name' => '', + 'name' => '', 'email' => 'taylor', ], ], diff --git a/tests/Feature/DataTablesEditorEditTest.php b/tests/Feature/DataTablesEditorEditTest.php index 3f3f63f..62a3f26 100644 --- a/tests/Feature/DataTablesEditorEditTest.php +++ b/tests/Feature/DataTablesEditorEditTest.php @@ -14,9 +14,9 @@ public function it_can_process_edit_request() $response = $this->postJson('users', [ 'action' => 'edit', - 'data' => [ + 'data' => [ 1 => [ - 'name' => 'Jeffrey', + 'name' => 'Jeffrey', 'email' => 'jefrrey@laravel.com', ], ], @@ -37,9 +37,9 @@ public function it_allows_updated_callback_and_returns_the_modified_model() $response = $this->postJson('usersWithEvents', [ 'action' => 'edit', - 'data' => [ + 'data' => [ 1 => [ - 'name' => 'Jeffrey', + 'name' => 'Jeffrey', 'email' => 'jefrrey@laravel.com', ], ], @@ -62,9 +62,9 @@ public function it_can_validate_invalid_inputs() $response = $this->postJson('users', [ 'action' => 'edit', - 'data' => [ + 'data' => [ 1 => [ - 'name' => '', + 'name' => '', 'email' => 'taylor', ], ], @@ -89,7 +89,7 @@ public function it_can_update_partial_data() $response = $this->postJson('users', [ 'action' => 'edit', - 'data' => [ + 'data' => [ 1 => [ 'name' => 'Jeffrey', ], @@ -108,18 +108,18 @@ public function it_can_process_bulk_update_request() { $this->createUser(); $this->createUser([ - 'name' => 'Jeffrey', + 'name' => 'Jeffrey', 'email' => 'jeffrey@laravel.com', ]); $response = $this->postJson('users', [ 'action' => 'edit', - 'data' => [ + 'data' => [ 1 => [ - 'name' => 'Arjay', + 'name' => 'Arjay', ], 2 => [ - 'name' => 'Arjay', + 'name' => 'Arjay', ], ], ]); diff --git a/tests/Feature/DataTablesEditorRemoveTest.php b/tests/Feature/DataTablesEditorRemoveTest.php index 7098204..9035679 100644 --- a/tests/Feature/DataTablesEditorRemoveTest.php +++ b/tests/Feature/DataTablesEditorRemoveTest.php @@ -14,9 +14,9 @@ public function it_can_process_remove_request() $response = $this->postJson('users', [ 'action' => 'remove', - 'data' => [ + 'data' => [ 1 => [ - 'name' => 'Taylor', + 'name' => 'Taylor', 'email' => 'taylor@laravel.com', ], ], @@ -37,7 +37,7 @@ public function it_can_process_bulk_remove_request() { $this->createUser(); $this->createUser([ - 'name' => 'Jeffrey', + 'name' => 'Jeffrey', 'email' => 'jeffrey@laravel.com', ]); @@ -46,13 +46,13 @@ public function it_can_process_bulk_remove_request() $response = $this->postJson('users', [ 'action' => 'remove', - 'data' => [ + 'data' => [ 1 => [ - 'name' => 'Taylor', + 'name' => 'Taylor', 'email' => 'taylor@laravel.com', ], 2 => [ - 'name' => 'Jeffrey', + 'name' => 'Jeffrey', 'email' => 'jefrrey@laravel.com', ], ], diff --git a/tests/TestCase.php b/tests/TestCase.php index 1dddd51..1bdf253 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -53,9 +53,9 @@ protected function getEnvironmentSetUp($app) $app['config']->set('app.debug', true); $app['config']->set('database.default', 'sqlite'); $app['config']->set('database.connections.sqlite', [ - 'driver' => 'sqlite', + 'driver' => 'sqlite', 'database' => ':memory:', - 'prefix' => '', + 'prefix' => '', ]); } @@ -68,7 +68,7 @@ protected function createUser($attributes = []) { if (! $attributes) { $attributes = [ - 'name' => 'Taylor', + 'name' => 'Taylor', 'email' => 'taylor@laravel.com', ]; }