diff --git a/src/Components/BooleanFilter.php b/src/Components/BooleanFilter.php index 683c9df..c791170 100644 --- a/src/Components/BooleanFilter.php +++ b/src/Components/BooleanFilter.php @@ -2,6 +2,7 @@ namespace Luckykenlin\LivewireTables\Components; +use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; @@ -83,9 +84,9 @@ public function apply(Request $request, Builder $builder, mixed $value): Builder /** * Render filter view * - * @return View + * @return View|Factory */ - public function render(): View + public function render(): View|Factory { return view($this->view, [ 'uriKey' => $this->uriKey, diff --git a/src/Components/DeleteModel.php b/src/Components/DeleteModel.php index 2c894f8..b4d3008 100644 --- a/src/Components/DeleteModel.php +++ b/src/Components/DeleteModel.php @@ -2,6 +2,7 @@ namespace Luckykenlin\LivewireTables\Components; +use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; use Livewire\Component; @@ -21,9 +22,9 @@ public function mount($itemId) } /** - * @return View + * @return View|Factory */ - public function render(): View + public function render(): View|Factory { return view('livewire-tables::' . config('livewire-tables.theme') . '.includes.delete-button'); } diff --git a/src/Components/Filter.php b/src/Components/Filter.php index 44e2aa8..001464a 100644 --- a/src/Components/Filter.php +++ b/src/Components/Filter.php @@ -2,6 +2,7 @@ namespace Luckykenlin\LivewireTables\Components; +use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; @@ -17,7 +18,7 @@ abstract class Filter * Unique key for filters component. * * @var string - */ + */ public string $uriKey = ''; /** @@ -51,15 +52,15 @@ public function __construct(?string $uriKey = '') abstract public function apply(Request $request, Builder $builder, mixed $value): Builder; /** - * @return View + * @return View|Factory */ - abstract protected function render(): View; + abstract protected function render(): View|Factory; /** * @param string $view * @return Filter */ - public function view(string $view): static + public function view(string $view): Filter { $this->view = $view; @@ -70,7 +71,7 @@ public function view(string $view): static * @param string $uriKey * @return Filter */ - public function setUriKey(string $uriKey): static + public function setUriKey(string $uriKey): Filter { $this->uriKey = $uriKey; @@ -111,9 +112,9 @@ public function getLabel(): string /** * @param string $label - * @return BooleanFilter + * @return Filter */ - public function label(string $label): static + public function label(string $label): Filter { $this->label = $label; diff --git a/src/Components/MultiSelectFilter.php b/src/Components/MultiSelectFilter.php index c2ec227..0d48b8c 100644 --- a/src/Components/MultiSelectFilter.php +++ b/src/Components/MultiSelectFilter.php @@ -2,6 +2,7 @@ namespace Luckykenlin\LivewireTables\Components; +use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; @@ -80,7 +81,7 @@ public function apply(Request $request, Builder $builder, mixed $value): Builder public function displayValue(array $value): string { return collect($this->options) - ->filter(function ($item, $key) use ($value) { + ->filter(function ($key) use ($value) { return in_array($key, $value); }) ->implode(', '); @@ -88,9 +89,9 @@ public function displayValue(array $value): string /** * @param array $options - * @return SelectFilter + * @return MultiSelectFilter */ - public function options(array $options): static + public function options(array $options): MultiSelectFilter { $this->options = $options; @@ -100,9 +101,9 @@ public function options(array $options): static /** * Render filter view * - * @return View + * @return View|Factory */ - public function render(): View + public function render(): View|Factory { return view($this->view, [ 'uriKey' => $this->uriKey, diff --git a/src/Components/SelectFilter.php b/src/Components/SelectFilter.php index 83e7143..64de1e2 100644 --- a/src/Components/SelectFilter.php +++ b/src/Components/SelectFilter.php @@ -2,6 +2,7 @@ namespace Luckykenlin\LivewireTables\Components; +use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; @@ -87,7 +88,7 @@ public function displayValue($value): string * @param array $options * @return SelectFilter */ - public function options(array $options): static + public function options(array $options): SelectFilter { $this->options = $options; @@ -97,9 +98,9 @@ public function options(array $options): static /** * Render filter view * - * @return View + * @return View|Factory */ - public function render(): View + public function render(): View|Factory { return view($this->view, [ 'uriKey' => $this->uriKey, diff --git a/src/Helper.php b/src/Helper.php index 3c92f08..58425e2 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -11,7 +11,7 @@ class Helper */ public static function getTableNameFromBuilder(Builder $builder): string { - return $builder?->getModel()?->getTable(); + return $builder->getModel()->getTable(); } /** diff --git a/src/LivewireTables.php b/src/LivewireTables.php index b38105b..e268911 100644 --- a/src/LivewireTables.php +++ b/src/LivewireTables.php @@ -3,6 +3,7 @@ namespace Luckykenlin\LivewireTables; use Illuminate\Contracts\Pagination\LengthAwarePaginator; +use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; @@ -89,7 +90,7 @@ abstract class LivewireTables extends Component */ public function boot(): void { - $this->emptyMessage = $this->emptyMessage ?? config('livewire-tables.empty_message', 'Whoops! No results.'); + $this->emptyMessage = empty($this->emptyMessage) ? config('livewire-tables.empty_message', 'Whoops! No results.') : $this->emptyMessage; } public function booted(): void @@ -128,9 +129,9 @@ public function filters(): array /** * Render table. * - * @return View + * @return View|Factory */ - public function render(): View + public function render(): View|Factory { return view($this->view(), [ 'columns' => $this->columns(), @@ -151,15 +152,15 @@ protected function view(): string /** * Get table data. * - * @return LengthAwarePaginator|Collection + * @return Collection|LengthAwarePaginator|array */ - protected function rows(): LengthAwarePaginator|Collection + protected function rows(): Collection|LengthAwarePaginator|array { - $this->applyFilter($this->builder); + $this->applyFilter(); - $this->applySearch($this->builder); + $this->applySearch(); - $this->applySorting($this->builder); + $this->applySorting(); if ($this->paginationEnabled) { return $this->builder->paginate(perPage: $this->perPage, pageName: $this->pageName()); diff --git a/src/Traits/Filterable.php b/src/Traits/Filterable.php index 3c68e32..3c87650 100644 --- a/src/Traits/Filterable.php +++ b/src/Traits/Filterable.php @@ -27,20 +27,23 @@ trait Filterable /** * Trick of search. * - * @param Builder $builder * @return Builder */ - protected function applyFilter(Builder $builder): Builder + protected function applyFilter(): Builder { collect($this->filters()) - ->each(function ($filter) use ($builder) { + ->each(function ($filter) { tap( $this->getFilterValue($filter), - fn ($value) => filled($value) && $filter->apply(request(), $builder, $value) + function ($value) use ($filter) { + if (filled($value)) { + $filter->apply(request(), $this->builder, $value); + } + } ); }); - return $builder; + return $this->builder; } /** @@ -51,7 +54,7 @@ protected function applyFilter(Builder $builder): Builder */ protected function getFilterValue(Filter $filter): mixed { - return $this->filters[$filter->getUriKey()] ?? null; + return $this->filters[$filter->getUriKey()]; } /** @@ -71,7 +74,7 @@ public function updatedFilters(): void */ protected function checkFilters(): void { - foreach ($this->filters as $uriKey => $filter) { + foreach ($this->filters as $uriKey => $_) { if (filled($this->filters[$uriKey])) { continue; } @@ -95,7 +98,7 @@ public function resetFilters(): void */ public function hasFilters(): bool { - return count($this->filters()); + return count($this->filters()) > 0; } /** @@ -105,7 +108,7 @@ public function hasFilters(): bool */ public function removeFilter($uriKey): void { - if (! isset($this->filters[$uriKey])) { + if (!isset($this->filters[$uriKey])) { return; } @@ -120,7 +123,7 @@ public function removeFilter($uriKey): void public function countFilters(): int { return collect($this->filters) - ->reject(fn ($value) => $value === '') + ->reject(fn($value) => $value === '') ->count(); } diff --git a/src/Traits/Pagination.php b/src/Traits/Pagination.php index 39b57a5..125f2a0 100644 --- a/src/Traits/Pagination.php +++ b/src/Traits/Pagination.php @@ -10,24 +10,18 @@ trait Pagination /** * Theme of pagination. - * - * @var string */ - public string $paginationTheme; + public $paginationTheme; /** * Amount of items to show per page. - * - * @var int */ - public int $perPage; + public $perPage; /** * The options to limit the amount of results per page. - * - * @var array */ - public array $perPageOptions; + public $perPageOptions; /** * Show the per page select. diff --git a/src/Traits/Relation.php b/src/Traits/Relation.php index eaab487..b0826ff 100644 --- a/src/Traits/Relation.php +++ b/src/Traits/Relation.php @@ -28,9 +28,9 @@ protected function getColumn(string $field): Column * Get column by field. * * @param string $attribute - * @return Column + * @return Column|null */ - protected function getColumnByAttribute(string $attribute): Column + protected function getColumnByAttribute(string $attribute): Column|null { return collect($this->columns()) ->where('attribute', $attribute) diff --git a/src/Traits/Searchable.php b/src/Traits/Searchable.php index 6d4a077..d8f5365 100644 --- a/src/Traits/Searchable.php +++ b/src/Traits/Searchable.php @@ -38,16 +38,15 @@ trait Searchable /** * Trick of search. * - * @param Builder $builder * @return Builder */ - protected function applySearch(Builder $builder): Builder + protected function applySearch(): Builder { if (trim($this->search) === '' || ! $this->showSearch) { - return $builder; + return $this->builder; } - $builder->where(function ($builder) { + $this->builder->where(function ($builder) { collect($this->columns()) ->reject(fn ($column) => ! $column->isSearchable()) ->each(function (Column $column) use ($builder) { @@ -73,7 +72,7 @@ protected function applySearch(Builder $builder): Builder }); }); - return $builder; + return $this->builder; } /** diff --git a/src/Traits/Sortable.php b/src/Traits/Sortable.php index 727995c..f930d17 100644 --- a/src/Traits/Sortable.php +++ b/src/Traits/Sortable.php @@ -42,17 +42,16 @@ trait Sortable /** * Trick of sorting. * - * @param Builder $builder * @return Builder */ - protected function applySorting(Builder $builder): Builder + protected function applySorting(): Builder { if ($this->sortingEnabled === false) { - return $builder; + return $this->builder; } if (! empty($this->defaultSortColumn) && ! count($this->sorts)) { - return $builder->orderBy($this->defaultSortColumn, $this->defaultSortDirection); + return $this->builder->orderBy($this->defaultSortColumn, $this->defaultSortDirection); } foreach ($this->sorts as $attribute => $direction) { @@ -69,13 +68,13 @@ protected function applySorting(Builder $builder): Builder // If the column has a sort callback, just use that if ($column->hasSortCallback()) { // call sort callback - ($column->getSortCallback())($builder, $direction); + ($column->getSortCallback())($this->builder, $direction); } else { - $builder->orderBy($this->getSortAttribute($builder, $attribute), $direction); + $this->builder->orderBy($this->getSortAttribute($this->builder, $attribute), $direction); } } - return $builder; + return $this->builder; } /** diff --git a/src/Views/Column.php b/src/Views/Column.php index 6d9e3e0..986297d 100644 --- a/src/Views/Column.php +++ b/src/Views/Column.php @@ -2,7 +2,6 @@ namespace Luckykenlin\LivewireTables\Views; -use Closure; use Illuminate\Support\Str; class Column @@ -73,22 +72,22 @@ class Column protected bool $asHtml = false; /** - * @var Closure|null + * @var callable|null */ protected $formatCallback = null; /** - * @var Closure|null + * @var callable|null */ protected $renderCallback = null; /** - * @var Closure|null + * @var callable|null */ protected $sortCallback = null; /** - * @var Closure|null + * @var callable|null */ protected $searchCallback = null; @@ -107,7 +106,9 @@ public function __construct(?string $field = null, ?string $attribute = null) /** * @param string $field * @param string|null $attribute - * @return static + * @no-named-arguments + * + * @return Column */ public static function make(string $field, ?string $attribute = null): Column {