From 88f71aeba1a1649661d713297d3a75454636b2d1 Mon Sep 17 00:00:00 2001 From: Sam Light Date: Sat, 26 Apr 2025 12:25:03 +0100 Subject: Larastan fixes --- src/TableComponent.php | 66 ++++++++++++++++++++++++++++++++++++--- workbench/app/Livewire/Table.php | 6 ++++ workbench/app/Models/Category.php | 15 ++++++++- workbench/app/Models/Product.php | 19 +++++++++-- 4 files changed, 99 insertions(+), 7 deletions(-) diff --git a/src/TableComponent.php b/src/TableComponent.php index 14d631b..84ca518 100644 --- a/src/TableComponent.php +++ b/src/TableComponent.php @@ -3,7 +3,7 @@ namespace Lightscale\LaralightTables; use Lightscale\LaralightTables\Columns\Column; - +use Lightscale\LaralightTables\Toolbar\Filter; use Lightscale\LaralightAssets\Facades\Assets; use Livewire\Component; @@ -12,18 +12,34 @@ use Livewire\Attributes\Url; use Illuminate\Pagination\Paginator; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; use Illuminate\Support\Collection; +use Illuminate\View\View; use Exception; + +/** + * @template TModel of Model + */ abstract class TableComponent extends Component { use WithPagination; // Config + + /** + * @var string + */ protected $paginationTheme = 'bootstrap'; + + + /** + * @var ?class-string + */ protected $model = null; + protected int $defaultPageSize = 10; // Properties @@ -33,8 +49,14 @@ abstract class TableComponent extends Component #[Url] public int $pageSize; + /** + * @var array + */ public array $activeColumns = []; + /** + * @var array + */ #[Url] public array $filters = []; @@ -104,13 +126,22 @@ abstract class TableComponent extends Component } } + /** + * @return array + */ protected function toolbars(): array { return []; } + /** + * @var Collection + */ private Collection $toolbarsCache; + /** + * @return Collection + */ protected function getToolbars(): Collection { if (!isset($this->toolbarsCache)) { @@ -119,19 +150,33 @@ abstract class TableComponent extends Component return $this->toolbarsCache; } - protected function query() : Builder + /** + * @return Builder + */ + protected function query(): Builder { if($this->model === null) { throw new Exception('Requires $model to be set or query() method to be overridden'); } + \PHPStan\dumpType((new $this->model)->newQuery()); return $this->model::query(); } - abstract protected function columns() : array; + /** + * @return array + */ + abstract protected function columns(): array; + /** + * @param Builder $builder + * @param string $search + */ protected function search(Builder $builder, string $search) : void {} + /** + * @return Collection + */ protected function getFilters(): Collection { return $this->getToolbars() @@ -145,6 +190,9 @@ abstract class TableComponent extends Component return ($name = $this->order) === null ? null : $this->getColumns()[$name] ?? null; } + /** + * @param Builder $query + */ protected function applyOrder(Builder $query): void { $column = $this->getOrderColumn(); @@ -153,6 +201,9 @@ abstract class TableComponent extends Component } } + /** + * @return Builder + */ protected function buildQuery() : Builder { $query = $this->query(); @@ -170,7 +221,14 @@ abstract class TableComponent extends Component return $query; } + /** + * @var ?Collection + */ private ?Collection $columnsCache = null; + + /** + * @return Collection + */ public function getColumns(): Collection { if($this->columnsCache === null) { @@ -182,7 +240,7 @@ abstract class TableComponent extends Component return $this->columnsCache; } - public function render() + public function render(): View { $data = $this->buildQuery()->paginate($this->pageSize); $allColumns = $this->getColumns(); diff --git a/workbench/app/Livewire/Table.php b/workbench/app/Livewire/Table.php index 0cda575..6a91a24 100644 --- a/workbench/app/Livewire/Table.php +++ b/workbench/app/Livewire/Table.php @@ -5,6 +5,12 @@ namespace Workbench\App\Livewire; use Lightscale\LaralightTables\TableComponent; use Lightscale\LaralightTables\Toolbar; +use Illuminate\Database\Eloquent\Model; + +/** + * @template TModel of Model + * @extends TableComponent + */ abstract class Table extends TableComponent { diff --git a/workbench/app/Models/Category.php b/workbench/app/Models/Category.php index d1b1dcd..39e2edd 100644 --- a/workbench/app/Models/Category.php +++ b/workbench/app/Models/Category.php @@ -2,19 +2,32 @@ namespace Workbench\App\Models; +use Workbench\Database\Factories\CategoryFactory; + use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Model; class Category extends Model { + + /** + * @use HasFactory + */ use HasFactory; + + /** + * @return CategoryFactory + */ protected static function newFactory() { - return \Workbench\Database\Factories\CategoryFactory::new(); + return CategoryFactory::new(); } + /** + * @return HasMany + */ public function products(): HasMany { return $this->hasMany(Product::class); diff --git a/workbench/app/Models/Product.php b/workbench/app/Models/Product.php index 1edb185..cf9892e 100644 --- a/workbench/app/Models/Product.php +++ b/workbench/app/Models/Product.php @@ -2,6 +2,8 @@ namespace Workbench\App\Models; +use Workbench\Database\Factories\ProductFactory; + use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; @@ -9,13 +11,23 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; class Product extends Model { + + /** + * @use HasFactory + */ use HasFactory; - protected static function newFactory() + /** + * @return ProductFactory + */ + protected static function newFactory(): ProductFactory { - return \Workbench\Database\Factories\ProductFactory::new(); + return ProductFactory::new(); } + /** + * @param Builder $q + */ public function scopeSearch(Builder $q, string $s): void { $s = "%{$s}%"; @@ -25,6 +37,9 @@ class Product extends Model )); } + /** + * @return BelongsTo + */ public function category(): BelongsTo { return $this->belongsTo(Category::class); -- cgit v1.2.3