Skip to content

[MAINTENANCE] Use count() instead of iterator_count() in ReindexCommand#1987

Open
Erikmitk wants to merge 2 commits into
kitodo:mainfrom
Erikmitk:reindex_performance
Open

[MAINTENANCE] Use count() instead of iterator_count() in ReindexCommand#1987
Erikmitk wants to merge 2 commits into
kitodo:mainfrom
Erikmitk:reindex_performance

Conversation

@Erikmitk

@Erikmitk Erikmitk commented Jun 4, 2026

Copy link
Copy Markdown
Member

iterator_count() exhausts the iterator across the $documents just to count the number of elements from the query. The foreach-loop then iterates a second time.

In every branch $documents is of type QueryResult which implements Countable. PHP's global count() delegates to Countable::count() and works for both array and QueryResultInterface as declared by the return type. The change eliminates a full traversal of the result set solely for counting.

https://api.typo3.org/main/classes/TYPO3-CMS-Extbase-Persistence-Generic-QueryResult.html

iterator_count() was previously used inside the foreach-loop which causes the iteration to fail completely and was moved outside. But it still is an avoidable overhead.

\`iterator_count()\` exhausts the iterator across the \`$documents\` just to count the number of elements from the query. The \`foreach\`-loop then iterates a ***second*** time.

In every branch \`$documents\` is of type \`QueryResult\` which implements \`Countable\`. PHP's global \`count()\` delegates to \`Countable::count()\` and works for both \`array\` and \`QueryResultInterface\` as declared by the return type. The change eliminates a full traversal of the result set solely for counting.

https://api.typo3.org/main/classes/TYPO3-CMS-Extbase-Persistence-Generic-QueryResult.html

\`iterator_count()\` was previously used *inside* the *foreach*-loop which causes the iteration to fail completely and was moved outside. But it still is an avoidable overhead.
@Erikmitk Erikmitk force-pushed the reindex_performance branch from 5bc7e2f to e69e63d Compare June 4, 2026 12:48
PHPStan infers \`$documents\` as \`iterable<Document>|QueryResultInterface\`
across branches. \`iterable\` is not \`Countable\`, so \`count()\` is flagged.
The \`@var\` annotation narrows the type to \`QueryResultInterface\` which
implements \`Countable\` and resolves the false positive.
@Erikmitk Erikmitk force-pushed the reindex_performance branch from fa0d316 to e5f1cdd Compare June 4, 2026 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant