[MAINTENANCE] Use count() instead of iterator_count() in ReindexCommand#1987
Open
Erikmitk wants to merge 2 commits into
Open
[MAINTENANCE] Use count() instead of iterator_count() in ReindexCommand#1987Erikmitk wants to merge 2 commits into
Erikmitk wants to merge 2 commits into
Conversation
\`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.
5bc7e2f to
e69e63d
Compare
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.
fa0d316 to
e5f1cdd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
iterator_count()exhausts the iterator across the$documentsjust to count the number of elements from the query. Theforeach-loop then iterates a second time.In every branch
$documentsis of typeQueryResultwhich implementsCountable. PHP's globalcount()delegates toCountable::count()and works for botharrayandQueryResultInterfaceas 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.