Skip to content

Commit 3b83ab0

Browse files
committed
(fix): normalize Document attributes in Collection
PHPStan rejected Collection constructors that still received attribute/index Documents from older tests. Collection now converts those to Attribute and Index on construct.
1 parent c1d1389 commit 3b83ab0

3 files changed

Lines changed: 52 additions & 7 deletions

File tree

‎src/Database/Collection.php‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,38 @@
1010
class Collection
1111
{
1212
/**
13-
* @param array<Attribute> $attributes
14-
* @param array<Index> $indexes
13+
* @var array<Attribute>
14+
*/
15+
public array $attributes = [];
16+
17+
/**
18+
* @var array<Index>
19+
*/
20+
public array $indexes = [];
21+
22+
/**
23+
* @param array<Attribute|Document> $attributes
24+
* @param array<Index|Document> $indexes
1525
* @param array<string> $permissions
1626
* @param array<string, mixed> $metadata
1727
*/
1828
public function __construct(
1929
public string $id = '',
2030
public string $name = '',
21-
public array $attributes = [],
22-
public array $indexes = [],
31+
array $attributes = [],
32+
array $indexes = [],
2333
public array $permissions = [],
2434
public bool $documentSecurity = true,
2535
public array $metadata = [],
2636
) {
37+
$this->attributes = array_values(array_map(
38+
fn (Attribute|Document $attr): Attribute => $attr instanceof Attribute ? $attr : Attribute::fromDocument($attr),
39+
$attributes,
40+
));
41+
$this->indexes = array_values(array_map(
42+
fn (Index|Document $idx): Index => $idx instanceof Index ? $idx : Index::fromDocument($idx),
43+
$indexes,
44+
));
2745
}
2846

2947
/**

‎src/Database/Traits/Collections.php‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ public function createCollection(Collection $collection): Document
5555
$documentSecurity = $collection->documentSecurity;
5656
$metadata = $collection->metadata;
5757

58-
$attributes = array_map(fn ($attr): Attribute => $attr instanceof Attribute ? $attr : Attribute::fromDocument($attr), $attributes);
59-
$indexes = array_map(fn ($idx): Index => $idx instanceof Index ? $idx : Index::fromDocument($idx), $indexes);
60-
6158
foreach ($attributes as $attribute) {
6259
if (in_array($attribute->type, [ColumnType::Point, ColumnType::Linestring, ColumnType::Polygon, ColumnType::Vector, ColumnType::Object], true)) {
6360
$existingFilters = $attribute->filters;

‎tests/unit/CollectionModelTest.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,36 @@ public function testConstructorDefaults(): void
2828
$this->assertSame([], $collection->metadata);
2929
}
3030

31+
public function testConstructorNormalizesDocumentAttributesAndIndexes(): void
32+
{
33+
$collection = new Collection(
34+
id: 'nested',
35+
attributes: [
36+
new Document([
37+
'$id' => 'name',
38+
'type' => 'string',
39+
'size' => 64,
40+
'required' => true,
41+
]),
42+
],
43+
indexes: [
44+
new Document([
45+
'$id' => 'idx_name',
46+
'key' => 'idx_name',
47+
'type' => 'key',
48+
'attributes' => ['name'],
49+
'lengths' => [64],
50+
'orders' => ['ASC'],
51+
]),
52+
],
53+
);
54+
55+
$this->assertInstanceOf(Attribute::class, $collection->attributes[0]);
56+
$this->assertSame('name', $collection->attributes[0]->key);
57+
$this->assertInstanceOf(Index::class, $collection->indexes[0]);
58+
$this->assertSame('idx_name', $collection->indexes[0]->key);
59+
}
60+
3161
public function testConstructorWithValues(): void
3262
{
3363
$attr = Attribute::string(key: 'title', size: 128);

0 commit comments

Comments
 (0)