diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index eb4a0a6..0403887 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -14,7 +14,7 @@ jobs: strategy: fail-fast: true matrix: - php: [8.1, 8.2] + php: [8.2, 8.3] stability: [prefer-stable] name: PHP ${{ matrix.php }} - ${{ matrix.stability }} diff --git a/.github/workflows/pint.yml b/.github/workflows/pint.yml new file mode 100644 index 0000000..f078347 --- /dev/null +++ b/.github/workflows/pint.yml @@ -0,0 +1,20 @@ +name: PHP Linting +on: + pull_request: + push: + branches: + - master +jobs: + phplint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: "laravel-pint" + uses: aglipanci/laravel-pint-action@2.0.0 + with: + preset: laravel + verboseMode: true + - uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: "fix: pint" + diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index 68eede7..97dd9ef 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -27,8 +27,7 @@ jobs: strategy: matrix: - php-version: - - "8.1" + php: [8.2, 8.3] steps: - name: "Checkout code" diff --git a/UPGADE.md b/UPGADE.md new file mode 100644 index 0000000..c5ce3a6 --- /dev/null +++ b/UPGADE.md @@ -0,0 +1,22 @@ +# UPGRADE GUIDE + +## Upgrade from 10.x to 11.x + +1. Update the composer.json file and change the version of the package to `^11.0`: + +```json +"require": { + "yajra/laravel-datatables-buttons": "^11.0" +} +``` + +2. Run `composer update` to update the package. + +3. If you are using a custom class of laravel-excel to export the data, you need to update the collection method and include the return type `Collection|LazyCollection` in the method signature. + +```php +public function collection(): Collection|LazyCollection +{ + return $this->collection; +} +``` diff --git a/composer.json b/composer.json index 6b986dc..a9dbc7d 100644 --- a/composer.json +++ b/composer.json @@ -1,65 +1,78 @@ { - "name": "yajra/laravel-datatables-buttons", - "description": "Laravel DataTables Buttons Plugin.", - "keywords": [ - "laravel", - "datatables", - "buttons", - "jquery" - ], - "license": "MIT", - "authors": [ - { - "name": "Arjay Angeles", - "email": "aqangeles@gmail.com" - } - ], - "require": { - "php": "^8.1", - "yajra/laravel-datatables-oracle": "^10", - "yajra/laravel-datatables-html": "^10", - "illuminate/console": "^10" - }, - "require-dev": { - "maatwebsite/excel": "^3.1.46", - "larastan/larastan": "^2.4", - "orchestra/testbench": "^8", - "rap2hpoutre/fast-excel": "^5.1", - "barryvdh/laravel-snappy": "^1.0.1" - }, - "autoload": { - "psr-4": { - "Yajra\\DataTables\\": "src/" - } - }, - "autoload-dev": { - "psr-4": { - "Yajra\\DataTables\\Buttons\\Tests\\": "tests/" - } - }, - "extra": { - "branch-alias": { - "dev-master": "10.0-dev" + "name": "yajra/laravel-datatables-buttons", + "description": "Laravel DataTables Buttons Plugin.", + "keywords": [ + "laravel", + "datatables", + "buttons", + "jquery" + ], + "license": "MIT", + "authors": [ + { + "name": "Arjay Angeles", + "email": "aqangeles@gmail.com" + } + ], + "require": { + "php": "^8.2", + "yajra/laravel-datatables-oracle": "^11", + "yajra/laravel-datatables-html": "^11", + "illuminate/console": "^11" }, - "laravel": { - "providers": [ - "Yajra\\DataTables\\ButtonsServiceProvider" - ] - } - }, - "suggest": { - "yajra/laravel-datatables-export": "Exporting of dataTables (excel, csv and PDF) via livewire and queue worker.", - "maatwebsite/excel": "Exporting of dataTables (excel, csv and PDF) using maatwebsite package.", - "rap2hpoutre/fast-excel": "Faster exporting of dataTables using fast-excel package.", - "dompdf/dompdf": "Allows exporting of dataTable to PDF using the DomPDF.", - "barryvdh/laravel-snappy": "Allows exporting of dataTable to PDF using the print view." - }, - "minimum-stability": "dev", - "prefer-stable": true, - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/yajra" - } - ] + "require-dev": { + "larastan/larastan": "^2.9.2", + "orchestra/testbench": "^9", + "laravel/pint": "^1.14", + "rector/rector": "^1.0.2", + "maatwebsite/excel": "^3.1.55", + "rap2hpoutre/fast-excel": "^5.4" + }, + "autoload": { + "psr-4": { + "Yajra\\DataTables\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Yajra\\DataTables\\Buttons\\Tests\\": "tests/" + } + }, + "extra": { + "branch-alias": { + "dev-master": "11.0-dev" + }, + "laravel": { + "providers": [ + "Yajra\\DataTables\\ButtonsServiceProvider" + ] + } + }, + "suggest": { + "yajra/laravel-datatables-export": "Exporting of dataTables (excel, csv and PDF) via livewire and queue worker.", + "maatwebsite/excel": "Exporting of dataTables (excel, csv and PDF) using maatwebsite package.", + "rap2hpoutre/fast-excel": "Faster exporting of dataTables using fast-excel package.", + "dompdf/dompdf": "Allows exporting of dataTable to PDF using the DomPDF.", + "barryvdh/laravel-snappy": "Allows exporting of dataTable to PDF using the print view." + }, + "scripts": { + "test": "./vendor/bin/phpunit", + "pint": "./vendor/bin/pint", + "rector": "./vendor/bin/rector", + "stan": "./vendor/bin/phpstan analyse --memory-limit=2G --ansi --no-progress --no-interaction --configuration=phpstan.neon.dist", + "pr": [ + "@pint", + "@rector", + "@stan", + "@test" + ] + }, + "minimum-stability": "dev", + "prefer-stable": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/yajra" + } + ] } diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..ca7ca05 --- /dev/null +++ b/rector.php @@ -0,0 +1,22 @@ +paths([ + __DIR__.'/src', + __DIR__.'/tests', + ]); + + // register a single rule + $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); + + // define sets of rules + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_82, + ]); +}; diff --git a/src/ButtonsServiceProvider.php b/src/ButtonsServiceProvider.php index 23ea9c9..cdb273c 100644 --- a/src/ButtonsServiceProvider.php +++ b/src/ButtonsServiceProvider.php @@ -12,8 +12,6 @@ class ButtonsServiceProvider extends ServiceProvider { /** * Bootstrap the application events. - * - * @return void */ public function boot(): void { @@ -54,8 +52,6 @@ protected function registerCommands(): void /** * Register the service provider. - * - * @return void */ public function register(): void { diff --git a/src/Exports/DataTablesCollectionExport.php b/src/Exports/DataTablesCollectionExport.php index 0719820..1e2f743 100644 --- a/src/Exports/DataTablesCollectionExport.php +++ b/src/Exports/DataTablesCollectionExport.php @@ -12,30 +12,18 @@ abstract class DataTablesCollectionExport implements FromCollection, WithHeading { use Exportable; - /** - * @var Collection|LazyCollection - */ - protected $collection; - - /** - * @param Collection|LazyCollection|null $collection - */ - public function __construct($collection = null) + protected LazyCollection|Collection $collection; + + public function __construct(Collection|LazyCollection|null $collection = null) { $this->collection = $collection ?? new Collection; } - /** - * @return Collection|LazyCollection - */ - public function collection() + public function collection(): Collection|LazyCollection { return $this->collection; } - /** - * @return array - */ public function headings(): array { /** @var array $first */ diff --git a/src/Generators/DataTablesHtmlCommand.php b/src/Generators/DataTablesHtmlCommand.php index 5406ca0..95db37b 100644 --- a/src/Generators/DataTablesHtmlCommand.php +++ b/src/Generators/DataTablesHtmlCommand.php @@ -37,7 +37,6 @@ class DataTablesHtmlCommand extends DataTablesMakeCommand * Build the class with the given name. * * @param string $name - * @return string * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ @@ -48,18 +47,16 @@ protected function buildClass($name): string $stub = $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); $this->replaceBuilder($stub) - ->replaceColumns($stub) - ->replaceButtons($stub) - ->replaceDOM($stub) - ->replaceTableId($stub); + ->replaceColumns($stub) + ->replaceButtons($stub) + ->replaceDOM($stub) + ->replaceTableId($stub); return $stub; } /** * Get the stub file for the generator. - * - * @return string */ protected function getStub(): string { @@ -72,7 +69,6 @@ protected function getStub(): string * Parse the name and format according to the root namespace. * * @param string $name - * @return string */ protected function qualifyClass($name): string { @@ -92,6 +88,6 @@ protected function qualifyClass($name): string $name = preg_replace('#datatablehtml$#i', 'DataTableHtml', $name); } - return $this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name; + return $this->getDefaultNamespace(trim((string) $rootNamespace, '\\')).'\\'.$name; } } diff --git a/src/Generators/DataTablesMakeCommand.php b/src/Generators/DataTablesMakeCommand.php index 2a4c2d8..0a96770 100644 --- a/src/Generators/DataTablesMakeCommand.php +++ b/src/Generators/DataTablesMakeCommand.php @@ -61,7 +61,6 @@ public function handle() * Build the class with the given name. * * @param string $name - * @return string * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ @@ -70,32 +69,28 @@ protected function buildClass($name): string $stub = parent::buildClass($name); $this->replaceModelImport($stub) - ->replaceModel($stub) - ->replaceBuilder($stub) - ->replaceColumns($stub) - ->replaceButtons($stub) - ->replaceDOM($stub) - ->replaceTableId($stub) - ->replaceAction($stub) - ->replaceFilename($stub); + ->replaceModel($stub) + ->replaceBuilder($stub) + ->replaceColumns($stub) + ->replaceButtons($stub) + ->replaceDOM($stub) + ->replaceTableId($stub) + ->replaceAction($stub) + ->replaceFilename($stub); return $stub; } /** * Get DataTable class base name without the suffix. - * - * @return string */ protected function getDataTableBaseName(): string { - return (string) preg_replace('#datatable$#i', '', $this->getNameInput()); + return (string) preg_replace('#datatable$#i', '', (string) $this->getNameInput()); } /** * Prepare model name from input. - * - * @return string */ protected function prepareModelName(): string { @@ -105,7 +100,6 @@ protected function prepareModelName(): string /** * Replace the filename. * - * @param string $stub * @return $this */ protected function replaceFilename(string &$stub): static @@ -117,9 +111,6 @@ protected function replaceFilename(string &$stub): static /** * Replace the action. - * - * @param string $stub - * @return static */ protected function replaceAction(string &$stub): static { @@ -130,8 +121,6 @@ protected function replaceAction(string &$stub): static /** * Set the action view to be used. - * - * @return string */ protected function getAction(): string { @@ -148,7 +137,6 @@ protected function getAction(): string /** * Replace columns. * - * @param string $stub * @return $this */ protected function replaceTableId(string &$stub): static @@ -161,7 +149,6 @@ protected function replaceTableId(string &$stub): static /** * Replace dom. * - * @param string $stub * @return $this */ protected function replaceDOM(string &$stub): static @@ -177,7 +164,6 @@ protected function replaceDOM(string &$stub): static /** * Replace buttons. * - * @param string $stub * @return $this */ protected function replaceButtons(string &$stub): static @@ -189,8 +175,6 @@ protected function replaceButtons(string &$stub): static /** * Get the columns to be used. - * - * @return string */ protected function getButtons(): string { @@ -209,10 +193,6 @@ protected function getButtons(): string /** * Parse array from definition. - * - * @param string $definition - * @param int $indentation - * @return string */ protected function parseButtons(string $definition, int $indentation = 24): string { @@ -239,7 +219,6 @@ protected function parseButtons(string $definition, int $indentation = 24): stri /** * Replace columns. * - * @param string $stub * @return $this */ protected function replaceColumns(string &$stub): static @@ -251,8 +230,6 @@ protected function replaceColumns(string &$stub): static /** * Get the columns to be used. - * - * @return string */ protected function getColumns(): string { @@ -278,10 +255,6 @@ protected function getColumns(): string /** * Parse array from definition. - * - * @param array|string $definition - * @param int $indentation - * @return string */ protected function parseColumns(array|string $definition, int $indentation = 12): string { @@ -338,14 +311,13 @@ protected function qualifyClass($name) $name = preg_replace('#datatable$#i', 'DataTable', $name); } - return $this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name; + return $this->getDefaultNamespace(trim((string) $rootNamespace, '\\')).'\\'.$name; } /** * Get the default namespace for the class. * * @param string $rootNamespace - * @return string */ protected function getDefaultNamespace($rootNamespace): string { @@ -354,9 +326,6 @@ protected function getDefaultNamespace($rootNamespace): string /** * Replace model name. - * - * @param string $stub - * @return static */ protected function replaceModel(string &$stub): static { @@ -369,16 +338,12 @@ protected function replaceModel(string &$stub): static /** * Get model name to use. - * - * @return string */ protected function getModel(): string { /** @var string $modelFromOption */ $modelFromOption = $this->option('model'); - $modelNamespaceFromOption = $this->option('model-namespace') - ? $this->option('model-namespace') - : config('datatables-buttons.namespace.model'); + $modelNamespaceFromOption = $this->option('model-namespace') ?: config('datatables-buttons.namespace.model'); $name = $modelFromOption ?: $this->prepareModelName(); $modelNamespace = $modelNamespaceFromOption ?: $this->laravel->getNamespace(); @@ -393,7 +358,6 @@ protected function getModel(): string /** * Replace model import. * - * @param string $stub * @return $this */ protected function replaceModelImport(string &$stub): static @@ -405,8 +369,6 @@ protected function replaceModelImport(string &$stub): static /** * Get the stub file for the generator. - * - * @return string */ protected function getStub(): string { diff --git a/src/Generators/DataTablesScopeCommand.php b/src/Generators/DataTablesScopeCommand.php index 78ee636..c0aa5fd 100644 --- a/src/Generators/DataTablesScopeCommand.php +++ b/src/Generators/DataTablesScopeCommand.php @@ -31,7 +31,6 @@ class DataTablesScopeCommand extends GeneratorCommand * Get the default namespace for the class. * * @param string $rootNamespace - * @return string */ protected function getDefaultNamespace($rootNamespace): string { @@ -40,8 +39,6 @@ protected function getDefaultNamespace($rootNamespace): string /** * Get the stub file for the generator. - * - * @return string */ protected function getStub(): string { diff --git a/src/Html/DataTableHtml.php b/src/Html/DataTableHtml.php index d09d7c8..92456f4 100644 --- a/src/Html/DataTableHtml.php +++ b/src/Html/DataTableHtml.php @@ -25,13 +25,11 @@ public static function make(): Builder } /** - * @param string $method - * @param mixed $parameters * @return \Yajra\DataTables\Html\Builder * * @throws \Exception */ - public function __call(string $method, $parameters) + public function __call(string $method, mixed $parameters) { if (method_exists($this->getHtmlBuilder(), $method)) { return $this->getHtmlBuilder()->{$method}(...$parameters); diff --git a/src/Services/DataTable.php b/src/Services/DataTable.php index 07a7d84..56aa1cd 100644 --- a/src/Services/DataTable.php +++ b/src/Services/DataTable.php @@ -31,43 +31,31 @@ abstract class DataTable implements DataTableButtons * DataTables print preview view. * * @phpstan-var view-string - * - * @var string */ protected string $printPreview = 'datatables::print'; /** * Name of the dataTable variable. - * - * @var string */ protected string $dataTableVariable = 'dataTable'; /** * List of columns to be excluded from export. - * - * @var array */ protected array $excludeFromExport = []; /** * List of columns to be excluded from printing. - * - * @var array */ protected array $excludeFromPrint = []; /** * List of columns to be exported. - * - * @var string|array */ protected string|array $exportColumns = '*'; /** * List of columns to be printed. - * - * @var string|array */ protected string|array $printColumns = '*'; @@ -80,8 +68,6 @@ abstract class DataTable implements DataTableButtons /** * Html builder. - * - * @var \Yajra\DataTables\Html\Builder|null */ protected ?Builder $htmlBuilder = null; @@ -94,15 +80,11 @@ abstract class DataTable implements DataTableButtons /** * Export filename. - * - * @var string */ protected string $filename = ''; /** * Custom attributes set on the class. - * - * @var array */ protected array $attributes = []; @@ -124,20 +106,13 @@ abstract class DataTable implements DataTableButtons * Available button actions. When calling an action, the value will be used * as the function name (so it should be available) * If you want to add or disable an action, overload and modify this property. - * - * @var array */ protected array $actions = ['print', 'csv', 'excel', 'pdf']; - /** - * @var \Yajra\DataTables\Utilities\Request|null - */ protected ?Request $request = null; /** * Flag to use fast-excel package for export. - * - * @var bool */ protected bool $fastExcel = false; @@ -146,8 +121,6 @@ abstract class DataTable implements DataTableButtons * Note: Disabling this flag can improve you export time. * Enabled by default to emulate the same output * with laravel-excel. - * - * @var bool */ protected bool $fastExcelCallback = true; @@ -160,22 +133,16 @@ abstract class DataTable implements DataTableButtons /** * CSV export typewriter. - * - * @var string */ protected string $csvWriter = 'Csv'; /** * Excel export typewriter. - * - * @var string */ protected string $excelWriter = 'Xlsx'; /** * PDF export typewriter. - * - * @var string */ protected string $pdfWriter = 'Dompdf'; @@ -196,15 +163,12 @@ public function __construct() * * @phpstan-param view-string|null $view * - * @param string|null $view - * @param array $data - * @param array $mergeData * @return mixed */ - public function render(string $view = null, array $data = [], array $mergeData = []) + public function render(?string $view = null, array $data = [], array $mergeData = []) { if ($this->request()->ajax() && $this->request()->wantsJson()) { - return app()->call([$this, 'ajax']); + return app()->call($this->ajax(...)); } /** @var string $action */ @@ -224,8 +188,6 @@ public function render(string $view = null, array $data = [], array $mergeData = /** * Get DataTables Request instance. - * - * @return \Yajra\DataTables\Utilities\Request */ public function request(): Request { @@ -238,8 +200,6 @@ public function request(): Request /** * Display ajax response. - * - * @return \Illuminate\Http\JsonResponse */ public function ajax(): JsonResponse { @@ -283,8 +243,6 @@ public function printPreview(): Renderable /** * Get mapped columns versus final decorated output. - * - * @return array */ protected function getDataForPrint(): array { @@ -295,8 +253,6 @@ protected function getDataForPrint(): array /** * Get printable columns. - * - * @return array|\Illuminate\Support\Collection */ protected function printColumns(): array|Collection { @@ -305,8 +261,6 @@ protected function printColumns(): array|Collection /** * Get filtered print columns definition from html builder. - * - * @return \Illuminate\Support\Collection */ protected function getPrintColumnsFromBuilder(): Collection { @@ -315,8 +269,6 @@ protected function getPrintColumnsFromBuilder(): Collection /** * Get filtered export columns definition from html builder. - * - * @return \Illuminate\Support\Collection */ protected function getExportColumnsFromBuilder(): Collection { @@ -325,8 +277,6 @@ protected function getExportColumnsFromBuilder(): Collection /** * Get columns definition from html builder. - * - * @return \Illuminate\Support\Collection */ protected function getColumnsFromBuilder(): Collection { @@ -345,8 +295,6 @@ public function html() /** * Get DataTables Html Builder instance. - * - * @return \Yajra\DataTables\Html\Builder */ public function builder(): Builder { @@ -363,24 +311,16 @@ public function builder(): Builder /** * Map ajax response to columns definition. - * - * @param array|Collection $columns - * @param string $type - * @return array */ - protected function mapResponseToColumns($columns, string $type): array + protected function mapResponseToColumns(array|\Illuminate\Support\Collection $columns, string $type): array { $transformer = new DataArrayTransformer; - return array_map(function ($row) use ($columns, $type, $transformer) { - return $transformer->transform($row, $columns, $type); - }, $this->getAjaxResponseData()); + return array_map(fn ($row) => $transformer->transform($row, $columns, $type), $this->getAjaxResponseData()); } /** * Get decorated data as defined in datatables ajax response. - * - * @return array */ protected function getAjaxResponseData(): array { @@ -390,7 +330,7 @@ protected function getAjaxResponseData(): array ]); /** @var JsonResponse $response */ - $response = app()->call([$this, 'ajax']); + $response = app()->call($this->ajax(...)); /** @var array{data: array} $data */ $data = $response->getData(true); @@ -398,9 +338,6 @@ protected function getAjaxResponseData(): array return $data['data']; } - /** - * @return \Yajra\DataTables\Html\Builder - */ protected function getHtmlBuilder(): Builder { $builder = $this->html(); @@ -414,7 +351,6 @@ protected function getHtmlBuilder(): Builder /** * Add html builder callback hook. * - * @param callable $callback * @return $this */ public function withHtml(callable $callback): static @@ -427,7 +363,6 @@ public function withHtml(callable $callback): static /** * Add callback before sending the response. * - * @param callable $callback * @return $this */ public function before(callable $callback): static @@ -440,7 +375,6 @@ public function before(callable $callback): static /** * Add callback after the response was processed. * - * @param callable $callback * @return $this */ public function response(callable $callback): static @@ -505,8 +439,6 @@ protected function buildExcelFile() /** * Get export filename. - * - * @return string */ public function getFilename(): string { @@ -516,7 +448,6 @@ public function getFilename(): string /** * Set export filename. * - * @param string $filename * @return $this */ public function setFilename(string $filename): static @@ -528,8 +459,6 @@ public function setFilename(string $filename): static /** * Get filename for export. - * - * @return string */ protected function filename(): string { @@ -538,8 +467,6 @@ protected function filename(): string /** * Get mapped columns versus final decorated output. - * - * @return array */ protected function getDataForExport(): array { @@ -560,9 +487,6 @@ protected function exportColumns(): Collection /** * Convert array to collection of Column class. - * - * @param array $columns - * @return Collection */ private function toColumnsCollection(array $columns): Collection { @@ -570,7 +494,7 @@ private function toColumnsCollection(array $columns): Collection foreach ($columns as $column) { if (isset($column['data'])) { - $column['title'] = $column['title'] ?? $column['data']; + $column['title'] ??= $column['data']; $collection->push(new Column($column)); } else { $data = []; @@ -616,7 +540,7 @@ public function csv() */ public function pdf() { - if ('snappy' == config('datatables-buttons.pdf_generator', 'snappy')) { + if (config('datatables-buttons.pdf_generator', 'snappy') == 'snappy') { return $this->snappyPdf(); } @@ -627,7 +551,6 @@ public function pdf() /** * PDF version of the table using print preview blade template. * - * @return \Illuminate\Http\Response * * @throws \Yajra\DataTables\Exceptions\Exception */ @@ -652,7 +575,6 @@ public function snappyPdf(): Response /** * Add basic array query scopes. * - * @param \Yajra\DataTables\Contracts\DataTableScope $scope * @return $this */ public function addScope(DataTableScope $scope): static @@ -665,7 +587,6 @@ public function addScope(DataTableScope $scope): static /** * Push multiples scopes to array query scopes. * - * @param array $scopes * @return $this */ public function addScopes(array $scopes): static @@ -678,8 +599,6 @@ public function addScopes(array $scopes): static /** * Set a custom class attribute. * - * @param array|string $key - * @param mixed|null $value * @return $this */ public function with(array|string $key, mixed $value = null): static @@ -696,7 +615,6 @@ public function with(array|string $key, mixed $value = null): static /** * Dynamically retrieve the value of an attribute. * - * @param string $key * @return mixed|null */ public function __get(string $key) @@ -723,24 +641,16 @@ protected function applyScopes( /** * Determine if the DataTable has scopes. - * - * @param array $scopes - * @param bool $validateAll - * @return bool */ protected function hasScopes(array $scopes, bool $validateAll = false): bool { - $filteredScopes = array_filter($this->scopes, function ($scope) use ($scopes) { - return in_array(get_class($scope), $scopes); - }); + $filteredScopes = array_filter($this->scopes, fn ($scope) => in_array($scope::class, $scopes)); return $validateAll ? count($filteredScopes) === count($scopes) : ! empty($filteredScopes); } /** * Get default builder parameters. - * - * @return array */ protected function getBuilderParameters(): array { @@ -750,10 +660,6 @@ protected function getBuilderParameters(): array return $defaults; } - /** - * @param array|\Illuminate\Support\Collection $collection - * @return \Illuminate\Support\LazyCollection - */ protected function convertToLazyCollection(array|Collection $collection): LazyCollection { if (is_array($collection)) { @@ -763,9 +669,6 @@ protected function convertToLazyCollection(array|Collection $collection): LazyCo return $collection->lazy(); } - /** - * @return \Closure - */ public function fastExcelCallback(): Closure { return function ($row) { @@ -782,8 +685,6 @@ public function fastExcelCallback(): Closure } /** - * @return \Rap2hpoutre\FastExcel\FastExcel - * * @throws \Yajra\DataTables\Exceptions\Exception */ protected function buildFastExcelFile(): FastExcel diff --git a/src/Transformers/DataArrayTransformer.php b/src/Transformers/DataArrayTransformer.php index 24b6b4d..bd393e5 100644 --- a/src/Transformers/DataArrayTransformer.php +++ b/src/Transformers/DataArrayTransformer.php @@ -11,10 +11,7 @@ class DataArrayTransformer /** * Transform row data by column's definition. * - * @param array $row * @param array|Collection $columns - * @param string $type - * @return array */ public function transform(array $row, array|Collection $columns, string $type = 'printable'): array { @@ -28,10 +25,7 @@ public function transform(array $row, array|Collection $columns, string $type = /** * Transform row column by collection. * - * @param array $row * @param Collection $columns - * @param string $type - * @return array */ protected function buildColumnByCollection(array $row, Collection $columns, string $type = 'printable'): array { @@ -49,7 +43,7 @@ protected function buildColumnByCollection(array $row, Collection $columns, stri if ($type == 'exportable') { $title = $this->decodeContent($title); - $data = is_array($data) ? json_encode($data) : $this->decodeContent($data); + $data = is_array($data) ? json_encode($data, JSON_THROW_ON_ERROR) : $this->decodeContent($data); } if (isset($column->exportRender)) { @@ -66,9 +60,6 @@ protected function buildColumnByCollection(array $row, Collection $columns, stri /** * Decode content to a readable text value. - * - * @param mixed $data - * @return mixed */ protected function decodeContent(mixed $data): mixed { diff --git a/tests/DataTableServiceTest.php b/tests/DataTableServiceTest.php index 0afcebb..6f82807 100644 --- a/tests/DataTableServiceTest.php +++ b/tests/DataTableServiceTest.php @@ -5,6 +5,7 @@ use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Http\Response; use Illuminate\Support\Collection; +use PHPUnit\Framework\Attributes\Test; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Yajra\DataTables\Buttons\Tests\DataTables\UsersDataTable; use Yajra\DataTables\Buttons\Tests\Models\User; @@ -14,7 +15,7 @@ class DataTableServiceTest extends TestCase { use DatabaseTransactions; - /** @test */ + #[Test] public function it_can_handle_ajax_request(): void { $response = $this->getAjax('/users'); @@ -26,7 +27,7 @@ public function it_can_handle_ajax_request(): void ]); } - /** @test */ + #[Test] public function it_returns_view_on_normal_get_request(): void { $response = $this->get('users'); @@ -35,7 +36,7 @@ public function it_returns_view_on_normal_get_request(): void $response->assertSeeText('LaravelDataTables'); } - /** @test */ + #[Test] public function it_can_return_a_csv_file(): void { $response = $this->get('users?action=csv'); @@ -43,7 +44,7 @@ public function it_can_return_a_csv_file(): void $this->assertInstanceOf(BinaryFileResponse::class, $response->baseResponse); } - /** @test */ + #[Test] public function it_can_return_a_xls_file(): void { $response = $this->get('users?action=excel'); @@ -51,7 +52,7 @@ public function it_can_return_a_xls_file(): void $this->assertInstanceOf(BinaryFileResponse::class, $response->baseResponse); } - /** @test */ + #[Test] public function it_can_return_a_pdf_file(): void { $response = $this->get('users?action=pdf'); @@ -59,7 +60,7 @@ public function it_can_return_a_pdf_file(): void $this->assertInstanceOf(Response::class, $response->baseResponse); } - /** @test */ + #[Test] public function it_allows_before_response_callback(): void { $response = $this->getAjax('users/before'); @@ -69,7 +70,7 @@ public function it_allows_before_response_callback(): void $this->assertEquals($row['name'].'X', $row['nameX']); } - /** @test */ + #[Test] public function it_allows_response_callback(): void { $response = $this->getAjax('users/response'); @@ -84,23 +85,17 @@ protected function setUp(): void parent::setUp(); $router = $this->app['router']; - $router->get('/users', function (UsersDataTable $dataTable) { - return $dataTable->render('tests::users'); - }); - - $router->get('/users/before', function (UsersDataTable $dataTable) { - return $dataTable->before(function (EloquentDataTable $dataTable) { - $dataTable->addColumn('nameX', fn (User $user) => $user->name.'X'); - })->render('tests::users'); - }); - - $router->get('/users/response', function (UsersDataTable $dataTable) { - return $dataTable->response(function (Collection $data) { - $data['recordsTotal'] = 2; - $data['recordsFiltered'] = 1; - - return $data; - })->render('tests::users'); - }); + $router->get('/users', fn (UsersDataTable $dataTable) => $dataTable->render('tests::users')); + + $router->get('/users/before', fn (UsersDataTable $dataTable) => $dataTable->before(function (EloquentDataTable $dataTable) { + $dataTable->addColumn('nameX', fn (User $user) => $user->name.'X'); + })->render('tests::users')); + + $router->get('/users/response', fn (UsersDataTable $dataTable) => $dataTable->response(function (Collection $data) { + $data['recordsTotal'] = 2; + $data['recordsFiltered'] = 1; + + return $data; + })->render('tests::users')); } } diff --git a/tests/DataTables/UsersDataTable.php b/tests/DataTables/UsersDataTable.php index 66e8e29..0a05c98 100644 --- a/tests/DataTables/UsersDataTable.php +++ b/tests/DataTables/UsersDataTable.php @@ -10,9 +10,6 @@ class UsersDataTable extends DataTable { - /** - * @throws \Yajra\DataTables\Exceptions\Exception - */ public function dataTable(Builder $query): EloquentDataTable { return (new EloquentDataTable($query)) @@ -27,12 +24,12 @@ public function query(User $user): Builder public function html(): \Yajra\DataTables\Html\Builder { return $this->builder() - ->setTableId('users-table') - ->minifiedAjax() - ->columns([ - Column::make('id'), - Column::make('name'), - ]); + ->setTableId('users-table') + ->minifiedAjax() + ->columns([ + Column::make('id'), + Column::make('name'), + ]); } protected function filename(): string diff --git a/tests/TestCase.php b/tests/TestCase.php index 094d576..d063f36 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,7 +2,6 @@ namespace Yajra\DataTables\Buttons\Tests; -use Barryvdh\Snappy\ServiceProvider; use Illuminate\Database\Schema\Blueprint; use Illuminate\Testing\TestResponse; use Orchestra\Testbench\TestCase as BaseTestCase; @@ -111,7 +110,6 @@ protected function getPackageProviders($app): array DataTablesServiceProvider::class, ButtonsServiceProvider::class, HtmlServiceProvider::class, - ServiceProvider::class, ]; }