Repository navigation
fix: parse multiline fields in CSVArray::toArray - #100
Merged
Merged
Conversation
Read records with fgetcsv instead of splitting lines first, so quoted fields may contain line breaks. 🤖 Generated with Claude Code
Code coverage overviewLanguages: PHP PHP / code-coverage/phpunit
View coverage of the 6 most impacted files.
ℹ️ Calculations represent line coverage. Results may change as calculations are updated. Updated |
🤖 Generated with Claude Code
fgetcsv ends records only at \n, so input whose lines end in a lone \r, such as classic Mac exports, parsed to no rows. Input without any \n now has its \r normalized to \n before parsing. 🤖 Generated with Claude Code
fgetcsv ends records only at \n and returns [null] for blank lines, which is indistinguishable from an enclosed empty value. That dropped single-column rows with "", merged rows with mixed line breaks and broke CR-only input with an enclosed \n. Keep splitting at \r\n, \n and \r as before, but join lines while an enclosed field is open, then parse each record with str_getcsv. 🤖 Generated with Claude Code
A stray enclosure inside an unenclosed field, such as an inch mark, opened an enclosure that never closed and merged the following rows. Track field state like str_getcsv does: an enclosure opens only at the start of a field, after optional spaces and tabs. 🤖 Generated with Claude Code
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved scanner edge cases split valid multiline CSV fields into separate rows, while retaining all line separators substantially increases peak memory.
4 open issues
What changed in this PR
Updates CSVArray::toArray() to parse quoted multiline fields as single records, improving round-trip conversion with toCSV().
Changes:
- Adds quote-aware record splitting before parsing headers and values with
str_getcsv(). - Expands coverage for line endings, enclosure and escape behavior, and empty input; enables the existing multiline round-trip test.
| File | Description |
|---|---|
tests/CSVArrayTest.php |
Adds parsing regression cases and enables multiline round-trip coverage. |
src/CSVArray.php |
Introduces field-state tracking to join lines within enclosed CSV fields. |
Review effort: Max
Splitting into lines first kept every captured line break in memory and lost the escape state at line breaks. Scan the input once and cut records at line breaks outside enclosed fields. Also open an enclosure before checking for a delimiter, and skip vertical tabs and form feeds before an enclosure, as str_getcsv does. 🤖 Generated with Claude Code
🤖 Generated with Claude Code
github-actions Bot
pushed a commit
that referenced
this pull request
Nov 4, 2026
## [7.0.1](v7.0.0...v7.0.1) (2026-11-04) ### Bug Fixes * parse multiline fields in CSVArray::toArray ([#100](#100)) ([a211c1e](a211c1e))
|
🎉 This PR is included in version 7.0.1 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
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.

CSVArray::toArray()split the CSV into lines before parsing.A quoted field with a line break became several broken rows.
It now cuts records only at line breaks outside quoted fields, then parses each record with
str_getcsv.Line breaks, blank lines and empty input behave as before
Lines still split at
\r\n,\nand\r, mixed in one file too.Blank lines are skipped and keep their row number, missing columns default to
'', and''returns[].Row keys count records, which differs from line numbers only for multiline fields.
fgetcsvoverphp://memorywas tried and rejectedfgetcsvends records only at\n, so\r-only and mixed line breaks merged rows.It returns
[null]for a blank line, the same as for a quoted""in a one-column file, so such rows were dropped.It also needed PHPStan ignores, since
Safe\fgetcsvis missing in Safe 1.x.For agents 🤖: Copilot threads addressed, CI running, then review
splitRecords()scans the input once and followsstr_getcsvquoting: an enclosure opens only at the start of a field, after spaces, tabs,\vor\f. A doubled enclosure stays inside the field, and an escape character inside it skips the next character.fgetcsv, for escape\,"and'', and for delimiter equal to enclosure. With escape\n,fgetcsvitself is no valid reference; a test covers that case.Missing column headers.throw stays as on master.make rectorfails locally aftercomposer update:Undefined constant PHPUnitSetList::PHPUNIT_40. This comes from the newer Rector PHPUnit sets and is unrelated to this change.🤖 Generated with Claude Code