ext/spl: Fix default argument handling for named parameters in SplFileObject::fgetcsv()#22160
Open
arshidkv12 wants to merge 3 commits into
Open
ext/spl: Fix default argument handling for named parameters in SplFileObject::fgetcsv()#22160arshidkv12 wants to merge 3 commits into
arshidkv12 wants to merge 3 commits into
Conversation
…eObject::fgetcsv()
Member
|
Hi @arshidkv12 thanks for looking into but I m afraid it's more a band aid than a fix (and even it is incomplete). e.g. |
Contributor
Author
|
You are correct. I think this should be changed in the stub.php file. Then manually assign the default value if it is null.. public function fgetcsv(string $separator, string $enclosure, string $escape): array|false {} |
Member
|
A few suggested tests to add so the brittleness of the current approach is visible in CI.
$f->setCsvControl(';', "\n", "\\");
$f->fwrite("a,b;c\n");
$f->seek(0);
var_dump($f->fgetcsv(',', '"', '\\')); // user explicitly wants ,
// expected: ["a","b;c"] patched: ["a,b","c"] ← regression introduced by this PR
2. Enclosure-only via named arg (the bug is unfixed on enclosure).
$f->setCsvControl(',', "'", "\\");
$f->fwrite("a,'b,c'\n");
$f->seek(0);
var_dump($f->fgetcsv(escape: '\\')); // named-skip fills enclosure with "
// expected: ["a","b,c"] patched: ["a","'b","c'"]
3. Escape via named arg with a non-default enclosure (also exercises the is_escape_default deprecation path).
$f->setCsvControl(',', "'");
$f->fwrite("a,'b\\'c'\n");
$f->seek(0);
var_dump($f->fgetcsv(escape: '\\')); // enclosure silently becomes "
4. Same bug shape on fputcsv() (the PR does not touch it).
$f->setCsvControl(';', "\n", "\\");
$f->fputcsv(['a','b'], escape: '\\');
$f->seek(0);
echo $f->fgets(); // expected "a;b\n" patched: "a,b\n"
5. Reflection round-trip to lock the API contract.
$rm = new ReflectionMethod(SplFileObject::class, 'fgetcsv');
foreach ($rm->getParameters() as $p) {
var_dump($p->getName(), $p->allowsNull(), $p->getDefaultValue());
}
6. is_escape_default invariant. After setCsvControl(',', '"') (two args), fgetcsv() (no args) should still emit the escape-default deprecation. Confirms the rewrite
does not accidentally suppress the existing notice through the nullability change.
Two meta-points:
- The PR's own test calls setCsvControl(';', "\n", "\\") (three args), which flips is_escape_default to false and hides cases 3 and 6. Dropping the third arg from
setCsvControl already changes what is exercised, so both shapes are worth covering.
- Worth a quick run against the standalone fgetcsv() / fputcsv() in ext/standard as well, just to confirm those are unaffected (no instance state to clobber there),
so the fix scope is correctly limited to the SPL methods.
Note: suggested test cases with help of LLM. |
Member
|
For final review, might be best to wait Gina's opinion not enough familiar with this part of the code :) |
…eObject::fgetcsv()
…eObject::fgetcsv()
Contributor
Author
|
I can fix |
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.
#22156