fix: return an array from SpatieTagsEntry/SpatieTagsColumn getState() when state is not a Collection or array

The early-return branch returned the raw state from a method typed `array`,
which threw a `TypeError` for any truthy scalar state. PHPStan 2.2.8 narrows
`mixed` more precisely and surfaced this, failing CI on every branch.
This commit is contained in:
Dan Harrin
2026-08-05 15:38:06 +01:00
parent 1d05d6b10d
commit 40daa8dadc
5 changed files with 29 additions and 2 deletions
@@ -29,7 +29,7 @@ class SpatieTagsEntry extends TextEntry
$state = parent::getState();
if ($state && (! $state instanceof Collection) && (! is_array($state))) {
return $state;
return Arr::wrap($state);
}
$record = $this->getRecord();
@@ -32,7 +32,7 @@ class SpatieTagsColumn extends TextColumn
$state = parent::getState();
if ($state && (! $state instanceof Collection) && (! is_array($state))) {
return $state;
return Arr::wrap($state);
}
$record = $this->getRecord();
@@ -23,6 +23,8 @@ class SpatieTagsColumnTable extends Component implements HasActions, HasSchemas,
public ?string $tagType = null;
public ?string $customState = null;
protected function getTableQuery(): Builder
{
return Article::query();
@@ -36,6 +38,10 @@ class SpatieTagsColumnTable extends Component implements HasActions, HasSchemas,
$column->type($this->tagType);
}
if ($this->customState !== null) {
$column->state($this->customState);
}
return $table
->columns([$column]);
}
@@ -102,6 +102,13 @@ describe('rendering with tags', function (): void {
->assertCanRenderTableColumn('tags');
});
it('wraps a non-array custom state in an array', function (): void {
$record = Article::factory()->create();
livewire(SpatieTagsColumnTable::class, ['customState' => 'Laravel'])
->assertTableColumnStateSet('tags', ['Laravel'], record: $record);
});
it('can render column with typed tags', function (): void {
$record = Article::factory()->create();
$record->attachTag('Laravel', 'framework');
@@ -138,6 +138,20 @@ describe('state from tags', function (): void {
expect($state)->toBe([]);
});
it('wraps a non-array custom state in an array', function (): void {
$record = Article::factory()->create();
$record->load('tags');
$entry = SpatieTagsEntry::make('tags')
->state('Laravel')
->container(
Schema::make(Livewire::make())
->record($record)
);
expect($entry->getState())->toBe(['Laravel']);
});
it('deduplicates tag names in state', function (): void {
$record = Article::factory()->create();
$record->attachTags(['Laravel', 'PHP']);