Skip to content

Commit adcffb0

Browse files
committed
* rename prop $orderByDesc and its get/setter to $isOrderByDesc @ BaseQuery
* rename param `$orderByDesc` to `$isOrderByDesc` @ `App\Tests\PostsQuery\PostsTreeTest::testReOrderNestedPosts()`, `PostsTree::reOrderNestedPosts()` & `setSortingKeyForSortablePost()` * rename param `$orderByDesc` to `$isOrderByDesc` @ `setResult()` * rename var `$results` to `$result` @ `getQueryResult()` @ `QueryResult` @ `App\PostsQuery` @ be
1 parent 2061479 commit adcffb0

5 files changed

Lines changed: 24 additions & 24 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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ public function getQueryResult(QueryBuilder $queryBuilder, int $limit): array
6565
Helper::abortAPIIf(40006, $plansCost > $planCostLimit);
6666
}
6767

68-
$results = collect($query->getResult());
69-
if ($results->count() === $maxResults) {
70-
$results->pop();
68+
$result = collect($query->getResult());
69+
if ($result->count() === $maxResults) {
70+
$result->pop();
7171
$hasMorePages = true;
7272
}
7373
return [
74-
'result' => $results,
74+
'result' => $result,
7575
'hasMorePages' => $hasMorePages ?? false,
7676
'query' => $rawSQL,
7777
'queryPlan' => $explainJSON
@@ -83,7 +83,7 @@ public function setResult(
8383
Collection $queries,
8484
?string $cursorParamValue,
8585
string $orderByField,
86-
bool $orderByDesc,
86+
bool $isOrderByDesc,
8787
Collection $queryByPostIDParamsName,
8888
): void {
8989
$this->stopwatch->start('setResult');
@@ -95,8 +95,8 @@ public function setResult(
9595
$queries = $queries->intersectByKeys($cursorsKeyByPostType);
9696
}
9797

98-
$queries->each(function (QueryBuilder $qb, string $postType) use ($orderByDesc, $orderByField, $cursorsKeyByPostType) {
99-
$qb->addOrderBy("t.$orderByField", $orderByDesc === true ? 'DESC' : 'ASC')
98+
$queries->each(function (QueryBuilder $qb, string $postType) use ($isOrderByDesc, $orderByField, $cursorsKeyByPostType) {
99+
$qb->addOrderBy("t.$orderByField", $isOrderByDesc === true ? 'DESC' : 'ASC')
100100
// cursor paginator requires values of orderBy column are unique
101101
// if not it should fall back to other unique field (here is the post ID primary key)
102102
// https://use-the-index-luke.com/no-offset
@@ -110,7 +110,7 @@ public function setResult(
110110
return;
111111
}
112112
$comparisons = $cursors->keys()->map(
113-
fn(string $fieldName): Comparison => $orderByDesc
113+
fn(string $fieldName): Comparison => $isOrderByDesc
114114
? $qb->expr()->lt("t.$fieldName", ":cursor_$fieldName")
115115
: $qb->expr()->gt("t.$fieldName", ":cursor_$fieldName"),
116116
);

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)