src/Pagerfanta/ExtendedPdoAdapter.php
public function rewriteCountQuery($query)
    {
        if (\preg_match('/^\s*SELECT\s+\bDISTINCT\b/is', $query) || \preg_match('/\s+GROUP\s+BY\s+/is', $query)) {
            return '';
        }
        $openParenthesis = '(?:\()';
        $closeParenthesis = '(?:\))';
        $subQueryInSelect = $openParenthesis . '.*\bFROM\b.*' . $closeParenthesis;
        $pattern = '/(?:.*' . $subQueryInSelect . '.*)\bFROM\b\s+/Uims';
        if (\preg_match($pattern, $query)) {
            return '';
        }
        $subQueryWithLimitOrder = $openParenthesis . '.*\b(LIMIT|ORDER)\b.*' . $closeParenthesis;
        $pattern = '/.*\bFROM\b.*(?:.*' . $subQueryWithLimitOrder . '.*).*/Uims';
        if (\preg_match($pattern, $query)) {
            return '';
        }
        $queryCount = \preg_replace('/(?:.*)\bFROM\b\s+/Uims', 'SELECT COUNT(*) FROM ', $query, 1);
        list($queryCount) = \preg_split('/\s+ORDER\s+BY\s+/is', $queryCount);
        list($queryCount) = \preg_split('/\bLIMIT\b/is', $queryCount);

        return \trim($queryCount);
    }
public function rewriteCountQuery($query)
    {
        if (\preg_match('/^\s*SELECT\s+\bDISTINCT\b/is', $query) || \preg_match('/\s+GROUP\s+BY\s+/is', $query)) {
            return '';
        }
        $openParenthesis = '(?:\()';
        $closeParenthesis = '(?:\))';
        $subQueryInSelect = $openParenthesis . '.*\bFROM\b.*' . $closeParenthesis;
        $pattern = '/(?:.*' . $subQueryInSelect . '.*)\bFROM\b\s+/Uims';
        if (\preg_match($pattern, $query)) {
            return '';
        }
        $subQueryWithLimitOrder = $openParenthesis . '.*\b(LIMIT|ORDER)\b.*' . $closeParenthesis;
        $pattern = '/.*\bFROM\b.*(?:.*' . $subQueryWithLimitOrder . '.*).*/Uims';
        if (\preg_match($pattern, $query)) {
            return '';
        }
        $queryCount = \preg_replace('/(?:.*)\bFROM\b\s+/Uims', 'SELECT COUNT(*) FROM ', $query, 1);
        list($queryCount) = \preg_split('/\s+ORDER\s+BY\s+/is', $queryCount);
        list($queryCount) = \preg_split('/\bLIMIT\b/is', $queryCount);

        return \trim($queryCount);
    }
Return count query

@param string $query

@return string

@see https://github.com/pear/Pager/blob/master/examples/Pager_Wrapper.php
Taken from pear/pager and modified.
tested at https://github.com/pear/Pager/blob/80c0e31c8b94f913cfbdeccbe83b63822f42a2f8/tests/pager_wrapper_test.php#L19
@codeCoverageIgnore