Skip to content

Commit 0d4c933

Browse files
Merge #684
684: Improve coding standards r=curquiza a=norkunas # Pull Request ## Related issue Fixes #<issue_number> ## What does this PR do? - Add missing phpdocs; - Replace `DateTime` with `DateTimeinterface` to encourage usage of `DateTimeImmutable`; - Add more tests for contracts; - Make tests a little bit more consistent; - Add missing php typehints; ## PR checklist Please check if your PR fulfills the following requirements: - [ ] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Co-authored-by: Tomas <[email protected]>
2 parents d17688c + 55dc98f commit 0d4c933

36 files changed

+1362
-302
lines changed

src/Contracts/DeleteTasksQuery.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@ class DeleteTasksQuery
1010
{
1111
use TasksQueryTrait;
1212

13-
private array $canceledBy;
14-
15-
public function setCanceledBy(array $canceledBy)
13+
/**
14+
* @var non-empty-list<int>|null
15+
*/
16+
private ?array $canceledBy = null;
17+
18+
/**
19+
* @param non-empty-list<int> $canceledBy
20+
*
21+
* @return $this
22+
*/
23+
public function setCanceledBy(array $canceledBy): self
1624
{
1725
$this->canceledBy = $canceledBy;
1826

@@ -24,8 +32,8 @@ public function toArray(): array
2432
return array_filter(
2533
array_merge(
2634
$this->baseArray(),
27-
['canceledBy' => $this->formatArray($this->canceledBy ?? null)]
28-
), function ($item) { return null != $item || is_numeric($item); }
35+
['canceledBy' => $this->formatArray($this->canceledBy)]
36+
), static function ($item) { return null !== $item; }
2937
);
3038
}
3139
}

src/Contracts/DocumentsQuery.php

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,58 @@
66

77
class DocumentsQuery
88
{
9-
private int $offset;
10-
private int $limit;
11-
private array $fields;
12-
private array $filter;
9+
/**
10+
* @var non-negative-int|null
11+
*/
12+
private ?int $offset = null;
13+
14+
/**
15+
* @var non-negative-int|null
16+
*/
17+
private ?int $limit = null;
18+
19+
/**
20+
* @var non-empty-list<string>|null
21+
*/
22+
private ?array $fields = null;
23+
24+
/**
25+
* @var list<non-empty-string|list<non-empty-string>>|null
26+
*/
27+
private ?array $filter = null;
28+
1329
private ?bool $retrieveVectors = null;
1430

15-
public function setOffset(int $offset): DocumentsQuery
31+
/**
32+
* @param non-negative-int $offset
33+
*
34+
* @return $this
35+
*/
36+
public function setOffset(int $offset): self
1637
{
1738
$this->offset = $offset;
1839

1940
return $this;
2041
}
2142

22-
public function setLimit(int $limit): DocumentsQuery
43+
/**
44+
* @param non-negative-int $limit
45+
*
46+
* @return $this
47+
*/
48+
public function setLimit(int $limit): self
2349
{
2450
$this->limit = $limit;
2551

2652
return $this;
2753
}
2854

29-
public function setFields(array $fields): DocumentsQuery
55+
/**
56+
* @param non-empty-list<string> $fields
57+
*
58+
* @return $this
59+
*/
60+
public function setFields(array $fields): self
3061
{
3162
$this->fields = $fields;
3263

@@ -38,9 +69,9 @@ public function setFields(array $fields): DocumentsQuery
3869
*
3970
* @param list<non-empty-string|list<non-empty-string>> $filter a filter expression written as an array of strings
4071
*
41-
* @return DocumentsQuery the updated DocumentsQuery instance
72+
* @return $this
4273
*/
43-
public function setFilter(array $filter): DocumentsQuery
74+
public function setFilter(array $filter): self
4475
{
4576
$this->filter = $filter;
4677

@@ -49,8 +80,10 @@ public function setFilter(array $filter): DocumentsQuery
4980

5081
/**
5182
* @param bool|null $retrieveVectors boolean value to show _vector details
83+
*
84+
* @return $this
5285
*/
53-
public function setRetrieveVectors(?bool $retrieveVectors): DocumentsQuery
86+
public function setRetrieveVectors(?bool $retrieveVectors): self
5487
{
5588
$this->retrieveVectors = $retrieveVectors;
5689

@@ -64,7 +97,27 @@ public function setRetrieveVectors(?bool $retrieveVectors): DocumentsQuery
6497
*/
6598
public function hasFilter(): bool
6699
{
67-
return isset($this->filter);
100+
return null !== $this->filter;
101+
}
102+
103+
/**
104+
* @return array{
105+
* offset?: non-negative-int,
106+
* limit?: non-negative-int,
107+
* fields?: non-empty-list<string>|non-empty-string,
108+
* filter?: list<non-empty-string|list<non-empty-string>>,
109+
* retrieveVectors?: 'true'|'false'
110+
* }
111+
*/
112+
public function toArray(): array
113+
{
114+
return array_filter([
115+
'offset' => $this->offset,
116+
'limit' => $this->limit,
117+
'fields' => $this->getFields(),
118+
'filter' => $this->filter,
119+
'retrieveVectors' => (null !== $this->retrieveVectors ? ($this->retrieveVectors ? 'true' : 'false') : null),
120+
], static function ($item) { return null !== $item; });
68121
}
69122

70123
/**
@@ -75,27 +128,16 @@ public function hasFilter(): bool
75128
*
76129
* @return array|string|null
77130
*/
78-
private function fields()
131+
private function getFields()
79132
{
80-
if (!isset($this->fields)) {
133+
if (null === $this->fields) {
81134
return null;
82135
}
83136

84-
if ($this->hasFilter()) {
137+
if (null !== $this->filter) {
85138
return $this->fields;
86-
} else {
87-
return implode(',', $this->fields);
88139
}
89-
}
90140

91-
public function toArray(): array
92-
{
93-
return array_filter([
94-
'offset' => $this->offset ?? null,
95-
'limit' => $this->limit ?? null,
96-
'filter' => $this->filter ?? null,
97-
'fields' => $this->fields(),
98-
'retrieveVectors' => (null !== $this->retrieveVectors ? ($this->retrieveVectors ? 'true' : 'false') : null),
99-
], function ($item) { return null !== $item; });
141+
return implode(',', $this->fields);
100142
}
101143
}

src/Contracts/FacetSearchQuery.php

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,118 @@
66

77
class FacetSearchQuery
88
{
9+
/**
10+
* @var non-empty-string|null
11+
*/
12+
private ?string $facetName = null;
13+
14+
/**
15+
* @var non-empty-string|null
16+
*/
17+
private ?string $facetQuery = null;
18+
919
private ?string $q = null;
10-
private ?string $matchingStrategy = null;
20+
21+
/**
22+
* @var list<non-empty-string|list<non-empty-string>>|null
23+
*/
1124
private ?array $filter = null;
12-
private ?string $facetQuery = null;
13-
private ?string $facetName = null;
1425

15-
public function setQuery(string $q): FacetSearchQuery
26+
/**
27+
* @var 'last'|'all'|'frequency'|null
28+
*/
29+
private ?string $matchingStrategy = null;
30+
31+
/**
32+
* @var non-empty-list<non-empty-string>|null
33+
*/
34+
private ?array $attributesToSearchOn = null;
35+
36+
/**
37+
* @return $this
38+
*/
39+
public function setFacetName(string $facetName): self
1640
{
17-
$this->q = $q;
41+
$this->facetName = $facetName;
1842

1943
return $this;
2044
}
2145

22-
public function setMatchingStrategy(string $matchingStrategy): FacetSearchQuery
46+
/**
47+
* @return $this
48+
*/
49+
public function setFacetQuery(string $facetQuery): self
2350
{
24-
$this->matchingStrategy = $matchingStrategy;
51+
$this->facetQuery = $facetQuery;
2552

2653
return $this;
2754
}
2855

29-
public function setFilter(array $filter): FacetSearchQuery
56+
/**
57+
* @return $this
58+
*/
59+
public function setQuery(string $q): self
60+
{
61+
$this->q = $q;
62+
63+
return $this;
64+
}
65+
66+
/**
67+
* @param list<non-empty-string|list<non-empty-string>> $filter
68+
*
69+
* @return $this
70+
*/
71+
public function setFilter(array $filter): self
3072
{
3173
$this->filter = $filter;
3274

3375
return $this;
3476
}
3577

36-
public function setFacetQuery(string $facetQuery): FacetSearchQuery
78+
/**
79+
* @param 'last'|'all'|'frequency' $matchingStrategy
80+
*
81+
* @return $this
82+
*/
83+
public function setMatchingStrategy(string $matchingStrategy): self
3784
{
38-
$this->facetQuery = $facetQuery;
85+
$this->matchingStrategy = $matchingStrategy;
3986

4087
return $this;
4188
}
4289

43-
public function setFacetName(string $facetName): FacetSearchQuery
90+
/**
91+
* @param non-empty-list<non-empty-string> $attributesToSearchOn
92+
*
93+
* @return $this
94+
*/
95+
public function setAttributesToSearchOn(array $attributesToSearchOn): self
4496
{
45-
$this->facetName = $facetName;
97+
$this->attributesToSearchOn = $attributesToSearchOn;
4698

4799
return $this;
48100
}
49101

102+
/**
103+
* @return array{
104+
* facetName?: non-empty-string,
105+
* facetQuery?: non-empty-string,
106+
* q?: string,
107+
* filter?: list<non-empty-string|list<non-empty-string>>,
108+
* matchingStrategy?: 'last'|'all'|'frequency'|null,
109+
* attributesToSearchOn?: non-empty-list<non-empty-string>
110+
* }
111+
*/
50112
public function toArray(): array
51113
{
52114
return array_filter([
115+
'facetName' => $this->facetName,
116+
'facetQuery' => $this->facetQuery,
53117
'q' => $this->q,
54-
'matchingStrategy' => $this->matchingStrategy,
55118
'filter' => $this->filter,
56-
'facetQuery' => $this->facetQuery,
57-
'facetName' => $this->facetName,
58-
], function ($item) { return null !== $item; });
119+
'matchingStrategy' => $this->matchingStrategy,
120+
'attributesToSearchOn' => $this->attributesToSearchOn,
121+
], static function ($item) { return null !== $item; });
59122
}
60123
}

src/Contracts/FederationOptions.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ class FederationOptions
88
{
99
private ?float $weight = null;
1010

11-
public function setWeight(float $weight): FederationOptions
11+
/**
12+
* @return $this
13+
*/
14+
public function setWeight(float $weight): self
1215
{
1316
$this->weight = $weight;
1417

1518
return $this;
1619
}
1720

21+
/**
22+
* @return array{
23+
* weight?: float,
24+
* }
25+
*/
1826
public function toArray(): array
1927
{
2028
return array_filter([

src/Contracts/HybridSearchOptions.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ class HybridSearchOptions
1313
*/
1414
private ?string $embedder = null;
1515

16-
public function setSemanticRatio(float $ratio): HybridSearchOptions
16+
/**
17+
* @return $this
18+
*/
19+
public function setSemanticRatio(float $ratio): self
1720
{
1821
$this->semanticRatio = $ratio;
1922

@@ -22,8 +25,10 @@ public function setSemanticRatio(float $ratio): HybridSearchOptions
2225

2326
/**
2427
* @param non-empty-string $embedder
28+
*
29+
* @return $this
2530
*/
26-
public function setEmbedder(string $embedder): HybridSearchOptions
31+
public function setEmbedder(string $embedder): self
2732
{
2833
$this->embedder = $embedder;
2934

0 commit comments

Comments
 (0)