Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Knp\Component\Pager\Event\Subscriber\Paginate;

use Knp\Component\Pager\Event\ItemsEvent;
use Solarium\QueryType\Select\Query\Query;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
Expand All @@ -19,11 +20,23 @@ public function items(ItemsEvent $event)
list($client, $query) = $values;

if ($client instanceof \Solarium\Client && $query instanceof \Solarium\QueryType\Select\Query\Query) {

if ($grouping = $query->getComponent(Query::COMPONENT_GROUPING, false)) {

Copilot AI Mar 25, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$grouping is only conditionally assigned inside the if ($grouping = ...) block, but it’s later referenced in if ($event->count === null && $grouping). If getNumFound() returns null and the query has no grouping component, this will read an undefined variable (PHP notice/warning). Initialize $grouping (e.g., to null/false) before the first if so it’s always defined.

Suggested change
if ($grouping = $query->getComponent(Query::COMPONENT_GROUPING, false)) {
$grouping = $query->getComponent(Query::COMPONENT_GROUPING, false);
if ($grouping) {

Copilot uses AI. Check for mistakes.
$grouping->setNumberOfGroups(true);
}
Comment on lines +24 to +26

Copilot AI Mar 25, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New grouping-aware pagination logic is introduced here, but there’s no PHPUnit coverage asserting that (a) the query enables numberOfGroups, and (b) totalItemCount uses ngroups when grouping is enabled. Please add a unit test for SolariumQuerySubscriber similar to tests/Test/Pager/Subscriber/Paginate/ElasticaTest.php, using mocks for the Solarium client/result/grouping components.

Copilot uses AI. Check for mistakes.

$query->setStart($event->getOffset())->setRows($event->getLimit());
$solrResult = $client->select($query);

$event->items = $solrResult->getIterator();
$event->count = $solrResult->getNumFound();

if ($event->count === null && $grouping) {
$groups = $solrResult->getComponent('grouping')->getGroups();
Comment on lines 31 to +35

Copilot AI Mar 25, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sets $event->count from getNumFound() and only falls back to ngroups when getNumFound() is null. With Solr result grouping, numFound is typically the number of matched documents (not groups), so pagination will still use the wrong total. When grouping is enabled, prefer the grouping numberOfGroups value (and only fall back to numFound if ngroups isn’t available).

Copilot uses AI. Check for mistakes.
$firstGroup = reset($groups);
$event->count = $firstGroup->getNumberOfGroups();
Comment on lines +35 to +37

Copilot AI Mar 25, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reset($groups) can return false when there are no grouping results (e.g., zero matches, or grouping component present but no groups returned). Calling getNumberOfGroups() on false will cause a fatal error. Guard for an empty $groups / missing first group and set $event->count to 0 (or fall back safely) in that case.

Suggested change
$groups = $solrResult->getComponent('grouping')->getGroups();
$firstGroup = reset($groups);
$event->count = $firstGroup->getNumberOfGroups();
$groupingComponent = $solrResult->getComponent('grouping');
if ($groupingComponent !== null) {
$groups = $groupingComponent->getGroups();
if (!empty($groups)) {
$firstGroup = reset($groups);
if (false !== $firstGroup) {
$event->count = $firstGroup->getNumberOfGroups();
} else {
$event->count = 0;
}
} else {
$event->count = 0;
}
} else {
$event->count = 0;
}

Copilot uses AI. Check for mistakes.
}

$event->setCustomPaginationParameter('result', $solrResult);
$event->stopPropagation();
}
Expand Down
Loading