Fix CsvReader::current() TypeError at EOF (with regression tests)#18
Merged
Merged
Conversation
SplFileObject::current() returns false at end-of-file. The previous do-while loop always executed its body once, so calling current() when the iterator was already invalid ran count(false) and threw a TypeError on PHP 8+. That surfaces in the common OneToManyReader join path, which calls rightReader->current() after next() past the last detail row and then could not finish the last master row. Switch to while ($this->valid()) before reading the line (same approach as #3) and add regression tests, including an OneToManyReader integration case. Closes #3
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.
Summary
Replaces / supersedes #3 with the same core fix plus a reproducible PHPUnit suite.
CsvReader::current()useddo { … } while ($this->valid())when column headers were set.SplFileObject::current()returnsfalseat EOF. Ado-whilealways runs its body once, so callingcurrent()while the iterator is already invalid executedcount(false)and threw:Why #3’s author could not reproduce
Simple “last line of a normal CSV” fixtures never call
current()after the iterator is invalid. Foreach only callscurrent()whenvalid()is true, so the do-while body always started on a real line. Trailing newline / short last line / strict mode alone do not hit this path.Real-world trigger (also noted on #3)
Port\Reader\OneToManyReaderdoes:After the last matching detail row,
next()leaves the detailCsvReaderinvalid, thencurrent()is still invoked. That is exactly when the do-while blows up—so the last master row never completes. Matches the comment on #3 about OneToManyReader.Direct calls after exhaustion (
getRow(99),current()afteriterator_to_array) hit the same bug.Fix
Same idea as #3: only read a line when valid:
Tests
Three new tests (all error with TypeError on unfixed master, pass with this patch):
testCurrentAtEndOfFileWithHeadersReturnsNull— callcurrent()after exhausting the readertestGetRowPastEndWithHeadersReturnsNull—getRow(99)testOneToManyReaderConsumesLastDetailRowWithoutError— full master/detail join through the last rowsVerified locally:
count(false))Recommendation for #3
Close #3 in favor of this PR (same fix + tests + “Closes #3”). Do not merge #3 alone without tests.
Checklist
Not merging yet — waiting on CI/review per housekeeping guidance.