Skip to content

Commit

Permalink
* method fillWithParentPost() now only return an array with keys `m…
Browse files Browse the repository at this point in the history
…atchQueryPostCount` & `notMatchQueryParentPostCount` as other values can be retrieved from param `$result` or props on `$this` by its only caller in `App\Controller\PostsController->query()` @ `App\PostsQuery\PostsTree`

@ be
  • Loading branch information
n0099 committed Oct 24, 2024
1 parent da7048c commit e547916
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
11 changes: 5 additions & 6 deletions be/src/Controller/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,22 @@ public function query(Request $request): array
$query->query($params, $request->query->get('cursor'));
$this->stopwatch->stop('$queryClass->query()');
$this->stopwatch->start('fillWithParentPost');
$result = $query->postsTree->fillWithParentPost($query->queryResult);
$matchQueryPostCounts = $query->postsTree->fillWithParentPost($query->queryResult);
$this->stopwatch->stop('fillWithParentPost');

$this->stopwatch->start('queryUsers');
$latestRepliers = $this->latestReplierRepository->getLatestRepliersWithoutNameWhenHasUid(
$result['threads']->map(fn(Thread $thread) => $thread->getLatestReplierId()),
$query->postsTree->threads->map(fn(Thread $thread) => $thread->getLatestReplierId()),
);
$uids = collect($result)
->only(Helper::POST_TYPES_PLURAL)
$uids = collect([$query->postsTree->threads, $query->postsTree->replies, $query->postsTree->subReplies])
->flatMap(static fn(Collection $posts) => $posts->map(fn(Post $post) => $post->getAuthorUid()))
->concat($latestRepliers->pluck('uid')->filter()) // filter() will remove NULLs
->unique();
$users = collect($this->userRepository->getUsers($uids));
$this->stopwatch->stop('queryUsers');

$this->stopwatch->start('queryUserRelated');
$fid = $result['fid'];
$fid = $query->queryResult->fid;
$authorExpGrades = collect($this->authorExpGradeRepository->getLatestOfUsers($fid, $uids))
->keyBy(fn(AuthorExpGrade $authorExpGrade) => $authorExpGrade->uid);
$users->each(fn(User $user) => $user->setCurrentAuthorExpGrade($authorExpGrades[$user->getUid()]));
Expand All @@ -115,7 +114,7 @@ public function query(Request $request): array
'pages' => [
'currentCursor' => $query->queryResult->currentCursor,
'nextCursor' => $query->queryResult->nextCursor,
...Arr::except($result, ['fid', ...Helper::POST_TYPES_PLURAL]),
...$matchQueryPostCounts,
],
'forum' => $this->forumRepository->getForum($fid),
'threads' => $query->postsTree->reOrderNestedPosts(
Expand Down
21 changes: 8 additions & 13 deletions be/src/PostsQuery/PostsTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
/** @psalm-import-type PostsKeyByTypePluralName from CursorCodec */
readonly class PostsTree

Check warning on line 20 in be/src/PostsQuery/PostsTree.php

View workflow job for this annotation

GitHub Actions / runs-on (windows-latest) / phpmd

PostsTree.php: The class PostsTree has a coupling between objects value of 14. Consider to reduce the number of dependencies under 13. (CouplingBetweenObjects, Design Rules)
{
/** @var Collection<int, Thread> $threads */
/** @var Collection<int, Thread> */
public Collection $threads;

/** @var Collection<int, Reply> $replies */
/** @var Collection<int, Reply> */
public Collection $replies;

/** @var Collection<int, SubReply> $subReplies */
/** @var Collection<int, SubReply> */
public Collection $subReplies;

public function __construct(
Expand All @@ -36,11 +36,7 @@ public function __construct(

/**
* @return array{
* fid: int,
* threads: Collection<int, Thread>,
* replies: Collection<int, Reply>,
* subReplies: Collection<int, SubReply>,
* matchQueryPostCount: array{thread: int, reply: int, subReply: int},
* matchQueryPostCount: array{thread?: int, reply?: int, subReply?: int},
* notMatchQueryParentPostCount: array{thread: int, reply: int},
* }
*/
Expand Down Expand Up @@ -94,15 +90,14 @@ public function fillWithParentPost(QueryResult $result): array
$this->stopwatch->stop('parsePostContentProtoBufBytes');

return [
'fid' => $result->fid,
'matchQueryPostCount' => collect(Helper::POST_TYPES)
->combine([$tids, $pids, $spids])
->map(static fn(Collection $ids, string $type) => $ids->count()),
->map(static fn(Collection $ids, string $type) => $ids->count())
->toArray(),
'notMatchQueryParentPostCount' => [
'thread' => $parentThreadsID->diff($tids)->count(),
'reply' => $parentRepliesID->diff($pids)->count(),
],
...array_combine(Helper::POST_TYPES_PLURAL, [$this->threads, $this->replies, $this->subReplies]),
];
}

Expand Down Expand Up @@ -151,7 +146,7 @@ public function reOrderNestedPosts(
* @param string $childPostTypePluralName
* @return Collection<int, Collection<string, mixed|Collection<int, Collection<string, mixed>>>>
*/
$setSortingKeyFromCurrentAndChildPosts = function (
$setSortingKeyFromCurrentAndChildPosts = static function (
Collection $curPost,
string $childPostTypePluralName,
) use ($orderByField, $orderByDesc): Collection {

Check failure on line 152 in be/src/PostsQuery/PostsTree.php

View workflow job for this annotation

GitHub Actions / runs-on (windows-latest) / phpcs

PostsTree.php: Squiz.Functions.MultiLineFunctionDeclaration.UseOneParamPerLine: Multi-line use declarations must define one parameter per line
Expand Down Expand Up @@ -185,7 +180,7 @@ public function reOrderNestedPosts(

return $curPost;
};
$sortBySortingKey = fn(Collection $posts): Collection => $posts
$sortBySortingKey = static fn(Collection $posts): Collection => $posts
->sortBy(fn(Collection $i) => $i['sortingKey'], descending: $orderByDesc);
$removeSortingKey = $shouldRemoveSortingKey
? /** @psalm-return Collection<array-key, Collection> */
Expand Down

0 comments on commit e547916

Please sign in to comment.