Skip to content

Commit 8927f10

Browse files
committed
Merge branch 'dev' into union-post-queries
2 parents 3a76ea6 + bb39f2b commit 8927f10

5 files changed

Lines changed: 28 additions & 28 deletions

File tree

be/src/PostsQuery/BaseQuery.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
{
77
private string $orderByField;
88

9-
private bool $orderByDesc;
9+
private bool $isOrderByDesc;
1010

1111
public function __construct(
1212
public QueryResult $queryResult,
@@ -28,12 +28,12 @@ protected function setOrderByField(string $value): self
2828

2929
public function isOrderByDesc(): bool
3030
{
31-
return $this->orderByDesc;
31+
return $this->isOrderByDesc;
3232
}
3333

34-
protected function setOrderByDesc(bool $value): self
34+
protected function setIsOrderByDesc(bool $value): self
3535
{
36-
$this->orderByDesc = $value;
36+
$this->isOrderByDesc = $value;
3737
return $this;
3838
}
3939
}

be/src/PostsQuery/PostsTree.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,24 +127,24 @@ public function nestPostsWithParent(): Collection
127127
public function reOrderNestedPosts(
128128
Collection $nestedPosts,
129129
string $orderByField,
130-
bool $orderByDesc,
130+
bool $isOrderByDesc,
131131
): Collection {
132132
$sortBySortingKey = static fn(Collection $posts): Collection => $posts
133-
->sortBy(fn(SortablePost $post) => $post->getSortingKey(), descending: $orderByDesc)
133+
->sortBy(fn(SortablePost $post) => $post->getSortingKey(), descending: $isOrderByDesc)
134134
->values(); // reset keys
135135
$getOrderByProp = 'get' . ucfirst($orderByField);
136136
return $sortBySortingKey($nestedPosts->map(
137-
function (Thread $thread) use ($getOrderByProp, $orderByDesc, $sortBySortingKey): Thread {
137+
function (Thread $thread) use ($getOrderByProp, $isOrderByDesc, $sortBySortingKey): Thread {
138138
$thread->setReplies($sortBySortingKey($thread->getReplies()->map(
139-
function (Reply $reply) use ($getOrderByProp, $orderByDesc): Reply {
139+
function (Reply $reply) use ($getOrderByProp, $isOrderByDesc): Reply {
140140
$reply->setSubReplies($reply->getSubReplies()->sortBy(
141141
fn(SubReply $subReplies) => $subReplies->{$getOrderByProp}(),
142-
descending: $orderByDesc,
142+
descending: $isOrderByDesc,
143143
)->values()); // reset keys
144-
return $this->setSortingKeyForSortablePost($reply, $reply->getSubReplies(), $getOrderByProp, $orderByDesc);
144+
return $this->setSortingKeyForSortablePost($reply, $reply->getSubReplies(), $getOrderByProp, $isOrderByDesc);
145145
},
146146
)));
147-
$this->setSortingKeyForSortablePost($thread, $thread->getReplies(), $getOrderByProp, $orderByDesc);
147+
$this->setSortingKeyForSortablePost($thread, $thread->getReplies(), $getOrderByProp, $isOrderByDesc);
148148
return $thread;
149149
},
150150
));
@@ -160,7 +160,7 @@ private function setSortingKeyForSortablePost(
160160
SortablePost $currentPost,
161161
Collection $subPosts,
162162
string $getOrderByProp,
163-
bool $orderByDesc,
163+
bool $isOrderByDesc,
164164
): SortablePost {
165165
// use the topmost value between sorting key or value of orderBy field within its sub-posts
166166
/* @var ?(T is Thread ? Reply : (T is Reply ? SubReply : never)) $firstSubPost */
@@ -185,7 +185,7 @@ private function setSortingKeyForSortablePost(
185185

186186
// Collection->filter() will remove falsy values like null
187187
$currentAndSubPostSortingKeys = $currentAndSubPostSortingKeys->filter()->sort();
188-
$currentPost->setSortingKey($orderByDesc
188+
$currentPost->setSortingKey($isOrderByDesc
189189
? $currentAndSubPostSortingKeys->last()
190190
: $currentAndSubPostSortingKeys->first());
191191

be/src/PostsQuery/Query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function query(QueryParams $params, ?string $cursor): void
4141

4242
$orderByParam = $params->pick('orderBy')[0];
4343
$this->setOrderByField($orderByParam->value === 'default' ? 'postedAt' : $orderByParam->value)
44-
->setOrderByDesc($orderByParam->value === 'default'
44+
->setIsOrderByDesc($orderByParam->value === 'default'
4545
? $queryByPostIDParamsName->isEmpty()
4646
: $orderByParam->getSub('direction') === 'DESC');
4747

be/src/PostsQuery/QueryResult.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ public function getQueryResult(AbstractQuery $query, int $limit): array
5858
Helper::abortAPIIf(40006, $plansCost > $planCostLimit);
5959
}
6060

61-
$results = collect($query->getResult());
62-
if ($results->count() === $maxResults) {
63-
$results->pop();
61+
$result = collect($query->getResult());
62+
if ($result->count() === $maxResults) {
63+
$result->pop();
6464
$hasMorePages = true;
6565
}
6666
return [
67-
'result' => $results,
67+
'result' => $result,
6868
'hasMorePages' => $hasMorePages ?? false,
6969
'queryPlan' => $explainJSON
7070
];
@@ -75,7 +75,7 @@ public function setResult(
7575
Collection $queries,
7676
?string $cursorParamValue,
7777
string $orderByField,
78-
bool $orderByDesc,
78+
bool $isOrderByDesc,
7979
Collection $queryByPostIDParamsName,
8080
): void {
8181
$this->stopwatch->start('setResult');
@@ -88,9 +88,9 @@ public function setResult(
8888
}
8989
$maxResults = $this->perPageItems + 1;
9090

91-
$queries->each(function (QueryBuilder $qb, string $postType) use ($maxResults, $orderByDesc, $orderByField, $cursorsKeyByPostType) {
91+
$queries->each(function (QueryBuilder $qb, string $postType) use ($maxResults, $isOrderByDesc, $orderByField, $cursorsKeyByPostType) {
9292
$qb->addSelect("t.$orderByField AS orderByField")
93-
->addOrderBy("t.$orderByField", $orderByDesc === true ? 'DESC' : 'ASC')
93+
->addOrderBy("t.$orderByField", $isOrderByDesc === true ? 'DESC' : 'ASC')
9494
// cursor paginator requires values of orderBy column are unique
9595
// if not it should fall back to other unique field (here is the post ID primary key)
9696
// https://use-the-index-luke.com/no-offset
@@ -105,7 +105,7 @@ public function setResult(
105105
return;
106106
}
107107
$comparisons = $cursors->keys()->map(
108-
fn(string $fieldName): Comparison => $orderByDesc
108+
fn(string $fieldName): Comparison => $isOrderByDesc
109109
? $qb->expr()->lt("t.$fieldName", ":cursor_$fieldName")
110110
: $qb->expr()->gt("t.$fieldName", ":cursor_$fieldName"),
111111
);
@@ -118,7 +118,7 @@ public function setResult(
118118
'postsKeyByTypePluralName' => $postsKeyByTypePluralName,
119119
'hasMorePages' => $hasMorePages,
120120
'queryPlan' => $queryPlan
121-
] = $this->getUnionQueryResult($queries, $orderByDesc, $maxResults);
121+
] = $this->getUnionQueryResult($queries, $isOrderByDesc, $maxResults);
122122

123123
$this->threads = $postsKeyByTypePluralName->get('threads', collect());
124124
$this->replies = $postsKeyByTypePluralName->get('replies', collect());
@@ -147,11 +147,11 @@ public function setResult(
147147
* orderByField: mixed
148148
* }
149149
* @param Collection<Helpecr::POST_TYPE, QueryBuilder> $queries
150-
* @param bool $orderByDesc
150+
* @param bool $isOrderByDesc
151151
* @param int $maxResults
152152
* @return array{unionOfQueriesSQL: string, postsKeyByTypePluralName: PostsKeyByTypePluralName, hasMorePages: bool, queryPlan: array}
153153
*/
154-
private function getUnionQueryResult(Collection $queries, bool $orderByDesc, int $maxResults): array
154+
private function getUnionQueryResult(Collection $queries, bool $isOrderByDesc, int $maxResults): array
155155
{
156156
/** @var DBALQueryBuilder $unionOfQueries */
157157
// https://stackoverflow.com/questions/36959801/doctrine-orm-querybuilder-or-dbal-querybuilder
@@ -172,7 +172,7 @@ private function getUnionQueryResult(Collection $queries, bool $orderByDesc, int
172172
$firstQueryFieldAliases = array_flip((new Parser($firstQuery->getQuery()))
173173
->parse()->getResultSetMapping()->scalarMappings);
174174
$unionOfQueries = $unionOfQueries
175-
->addOrderBy($firstQueryFieldAliases['orderByField'], $orderByDesc === true ? 'DESC' : 'ASC')
175+
->addOrderBy($firstQueryFieldAliases['orderByField'], $isOrderByDesc === true ? 'DESC' : 'ASC')
176176
->addOrderBy($firstQueryFieldAliases['postId'])
177177
->setMaxResults($maxResults);
178178
$unionOfQueriesSQL = $unionOfQueries->getSQL();

be/tests/PostsQuery/PostsTreeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ protected function setUp(): void
2424
}
2525

2626
#[DataProvider('provideReOrderNestedPostsData')]
27-
public function testReOrderNestedPosts(Collection $input, bool $orderByDesc, Collection $expected): void
27+
public function testReOrderNestedPosts(Collection $input, bool $isOrderByDesc, Collection $expected): void
2828
{
29-
self::assertEquals($expected, $this->sut->reOrderNestedPosts($input, 'postedAt', $orderByDesc));
29+
self::assertEquals($expected, $this->sut->reOrderNestedPosts($input, 'postedAt', $isOrderByDesc));
3030
}
3131

3232
public static function provideReOrderNestedPostsData(): array

0 commit comments

Comments
 (0)