Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/spl/spl_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -2257,7 +2257,7 @@ PHP_METHOD(SplFileObject, fgetcsv)
size_t d_len = 0, e_len = 0;
zend_string *escape_str = NULL;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ssS", &delim, &d_len, &enclo, &e_len, &escape_str) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!S!", &delim, &d_len, &enclo, &e_len, &escape_str) == FAILURE) {
RETURN_THROWS();
}

Expand Down
2 changes: 1 addition & 1 deletion ext/spl/spl_directory.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function fgets(): string {}
public function fread(int $length): string|false {}

/** @tentative-return-type */
public function fgetcsv(string $separator = ",", string $enclosure = "\"", string $escape = "\\"): array|false {}
public function fgetcsv(?string $separator = null, ?string $enclosure = null, ?string $escape = null): array|false {}

/** @tentative-return-type */
public function fputcsv(array $fields, string $separator = ",", string $enclosure = "\"", string $escape = "\\", string $eol = "\n"): int|false {}
Expand Down
8 changes: 4 additions & 4 deletions ext/spl/spl_directory_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 112 additions & 0 deletions ext/spl/tests/gh22156.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
--TEST--
Bug GH-22156 SplFileObject::fgetcsv() with named escape parameter
--FILE--
<?php

echo "Test 1:\n";
$file = new SplFileObject('php://memory', 'rw+');

$file->fwrite("foo;bar;baz");
$file->seek(0);

$file->setCsvControl(';', "\n", "\\");

while (!$file->eof()) {
$line = $file->fgetcsv(escape: '\\');
var_dump($line);
}

echo "Test 2:\n";
$f = new SplFileObject('php://memory', 'rw+');
$f->setCsvControl(';', "\n", "\\");
$f->fwrite("a,b;c\n");
$f->seek(0);
var_dump($f->fgetcsv(',', '"', '\\'));

echo "Test 3:\n";
$f = new SplFileObject('php://memory', 'rw+');
$f->setCsvControl(',', "'", "\\");
$f->fwrite("a,'b,c'\n");
$f->seek(0);
var_dump($f->fgetcsv(escape: '\\'));

echo "Test 4:\n";
$f = new SplFileObject('php://memory', 'rw+');
$f->setCsvControl(',', "'");
$f->fwrite("a,'b\\'c'\n");
$f->seek(0);
var_dump($f->fgetcsv(escape: '\\'));

echo "Test 5:\n";

$rm = new ReflectionMethod(SplFileObject::class, 'fgetcsv');
foreach ($rm->getParameters() as $p) {
var_dump(
$p->getName(),
$p->allowsNull(),
$p->getDefaultValue()
);
}

echo "Test 6:\n";
$f = new SplFileObject('php://memory', 'rw+');
$f->setCsvControl(',', '"');
$f->fwrite("a,b\n");
$f->seek(0);
var_dump($f->fgetcsv());

?>
--EXPECTF--
Test 1:
array(3) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
[2]=>
string(3) "baz"
}
Test 2:
array(2) {
[0]=>
string(1) "a"
[1]=>
string(3) "b;c"
}
Test 3:
array(2) {
[0]=>
string(1) "a"
[1]=>
string(3) "b,c"
}
Test 4:

Deprecated: SplFileObject::setCsvControl(): the $escape parameter must be provided as its default value will change in %s on line %d
array(2) {
[0]=>
string(1) "a"
[1]=>
string(4) "b\'c"
}
Test 5:
string(9) "separator"
bool(true)
NULL
string(9) "enclosure"
bool(true)
NULL
string(6) "escape"
bool(true)
NULL
Test 6:

Deprecated: SplFileObject::setCsvControl(): the $escape parameter must be provided as its default value will change in %s on line %d

Deprecated: SplFileObject::fgetcsv(): the $escape parameter must be provided, as its default value will change, either explicitly or via SplFileObject::setCsvControl() in %s on line %d
array(2) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
}
Loading