Skip to content

Commit

Permalink
fixed psalm
Browse files Browse the repository at this point in the history
  • Loading branch information
ken lin committed Oct 31, 2022
1 parent 84bea2b commit 0b5e6cc
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 67 deletions.
5 changes: 3 additions & 2 deletions src/Components/BooleanFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/Components/DeleteModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Luckykenlin\LivewireTables\Components;

use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Livewire\Component;

Expand All @@ -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');
}
Expand Down
15 changes: 8 additions & 7 deletions src/Components/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,7 +18,7 @@ abstract class Filter
* Unique key for filters component.
*
* @var string
*/
*/
public string $uriKey = '';

/**
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
11 changes: 6 additions & 5 deletions src/Components/MultiSelectFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,17 +81,17 @@ 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(', ');
}

/**
* @param array $options
* @return SelectFilter
* @return MultiSelectFilter
*/
public function options(array $options): static
public function options(array $options): MultiSelectFilter
{
$this->options = $options;

Expand All @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions src/Components/SelectFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Helper
*/
public static function getTableNameFromBuilder(Builder $builder): string
{
return $builder?->getModel()?->getTable();
return $builder->getModel()->getTable();
}

/**
Expand Down
17 changes: 9 additions & 8 deletions src/LivewireTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand All @@ -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());
Expand Down
23 changes: 13 additions & 10 deletions src/Traits/Filterable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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()];
}

/**
Expand All @@ -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;
}
Expand All @@ -95,7 +98,7 @@ public function resetFilters(): void
*/
public function hasFilters(): bool
{
return count($this->filters());
return count($this->filters()) > 0;
}

/**
Expand All @@ -105,7 +108,7 @@ public function hasFilters(): bool
*/
public function removeFilter($uriKey): void
{
if (! isset($this->filters[$uriKey])) {
if (!isset($this->filters[$uriKey])) {
return;
}

Expand All @@ -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();
}

Expand Down
12 changes: 3 additions & 9 deletions src/Traits/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <int>
*/
public array $perPageOptions;
public $perPageOptions;

/**
* Show the per page select.
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions src/Traits/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -73,7 +72,7 @@ protected function applySearch(Builder $builder): Builder
});
});

return $builder;
return $this->builder;
}

/**
Expand Down
Loading

0 comments on commit 0b5e6cc

Please sign in to comment.