summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Light <samlight1994@gmail.com>2025-04-26 12:25:03 +0100
committerSam Light <samlight1994@gmail.com>2025-04-26 12:25:03 +0100
commit88f71aeba1a1649661d713297d3a75454636b2d1 (patch)
tree9f37fb5bbcd22930dd5e7686958614abf2a970d0
parenta1ba375df4978a606d519430ce8f7c7f0d170eed (diff)
Larastan fixes
-rw-r--r--src/TableComponent.php66
-rw-r--r--workbench/app/Livewire/Table.php6
-rw-r--r--workbench/app/Models/Category.php15
-rw-r--r--workbench/app/Models/Product.php19
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<TModel>
+ */
protected $model = null;
+
protected int $defaultPageSize = 10;
// Properties
@@ -33,8 +49,14 @@ abstract class TableComponent extends Component
#[Url]
public int $pageSize;
+ /**
+ * @var array<string>
+ */
public array $activeColumns = [];
+ /**
+ * @var array<string, string>
+ */
#[Url]
public array $filters = [];
@@ -104,13 +126,22 @@ abstract class TableComponent extends Component
}
}
+ /**
+ * @return array<Toolbar>
+ */
protected function toolbars(): array
{
return [];
}
+ /**
+ * @var Collection<int, Toolbar>
+ */
private Collection $toolbarsCache;
+ /**
+ * @return Collection<int, Toolbar>
+ */
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<TModel>
+ */
+ 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<Column>
+ */
+ abstract protected function columns(): array;
+ /**
+ * @param Builder<TModel> $builder
+ * @param string $search
+ */
protected function search(Builder $builder, string $search) : void {}
+ /**
+ * @return Collection<int, Filter>
+ */
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<TModel> $query
+ */
protected function applyOrder(Builder $query): void
{
$column = $this->getOrderColumn();
@@ -153,6 +201,9 @@ abstract class TableComponent extends Component
}
}
+ /**
+ * @return Builder<TModel>
+ */
protected function buildQuery() : Builder
{
$query = $this->query();
@@ -170,7 +221,14 @@ abstract class TableComponent extends Component
return $query;
}
+ /**
+ * @var ?Collection<int, Column>
+ */
private ?Collection $columnsCache = null;
+
+ /**
+ * @return Collection<int, Column>
+ */
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<TModel>
+ */
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<CategoryFactory>
+ */
use HasFactory;
+
+ /**
+ * @return CategoryFactory<static>
+ */
protected static function newFactory()
{
- return \Workbench\Database\Factories\CategoryFactory::new();
+ return CategoryFactory::new();
}
+ /**
+ * @return HasMany<Product, $this>
+ */
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<ProductFactory>
+ */
use HasFactory;
- protected static function newFactory()
+ /**
+ * @return ProductFactory<static>
+ */
+ protected static function newFactory(): ProductFactory
{
- return \Workbench\Database\Factories\ProductFactory::new();
+ return ProductFactory::new();
}
+ /**
+ * @param Builder<static> $q
+ */
public function scopeSearch(Builder $q, string $s): void
{
$s = "%{$s}%";
@@ -25,6 +37,9 @@ class Product extends Model
));
}
+ /**
+ * @return BelongsTo<Category, $this>
+ */
public function category(): BelongsTo
{
return $this->belongsTo(Category::class);