-
Notifications
You must be signed in to change notification settings - Fork 667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fputcsv $fields parameter should be array of strings #5226
Comments
I found these snippets: https://psalm.dev/r/2478131ecf<?php declare(strict_types = 1);
$handle = fopen("/tmp/output.csv", "w");
if ($handle === false) die();
class Person {}
class PersonWithToString {
public function __toString(): string {
return "to string name";
}
}
class StringablePerson implements Stringable {
public function __toString(): string {
return "stringable name";
}
}
fputcsv($handle, [
"string", // OK
new Person, // ERROR: Person can not be converted to a string.
new PersonWithToString, // OK
new StringablePerson, // OK
]);
fclose($handle);
|
See also: phpstan/phpstan-src#448 |
Indeed, the function is documented as |
Good point @orklah As a starting point for a test case I've created another snippet . I only raise this as I was caught out when I changed a string to a value object and my CSV generation failed. PHPStan didn't flag this as an issue, and nor would have have Psalm. |
I found these snippets: https://psalm.dev/r/45dc241a38<?php declare(strict_types = 1);
$handle = fopen("/tmp/output.csv", "w");
if ($handle === false) die();
class Person {}
class PersonWithToString {
public function __toString(): string {
return "to string name";
}
}
class StringablePerson implements Stringable {
public function __toString(): string {
return "stringable name";
}
}
// All there are OK. Are there any other types that can be cast to a string?
fputcsv($handle, [
"string",
1,
3.5,
true,
false,
new PersonWithToString,
new StringablePerson,
]);
// THis is a problem
fputcsv($handle, [
new Person, // ERROR, Should report this as an error
]);
fclose($handle);
|
|
I found these snippets: https://psalm.dev/r/58b7ab935a<?php declare(strict_types = 1);
$handle = fopen("/tmp/output.csv", "w");
if ($handle === false) die();
class Person {}
class PersonWithToString {
public function __toString(): string {
return "to string name";
}
}
class StringablePerson implements Stringable {
public function __toString(): string {
return "stringable name";
}
}
// All there are OK. Are there any other types that can be cast to a string?
fputcsv($handle, [
"string",
1,
3.5,
true,
false,
null,
new PersonWithToString,
new StringablePerson,
]);
// THis is a problem
fputcsv($handle, [
new Person, // ERROR, Should report this as an error
]);
fclose($handle);
|
I think the
fields
parameter forfputcsv
should be an array of strings. See PHP manual.This snippet should report an error on line
22
.Running the code in the snippet on PHP 8.0 (
PHP 8.0.0 (cli) (built: Dec 6 2020 06:56:11) ( NTS )
) results in this error:NOTE: I've seen a similar error with PHP 7.3 too.
The text was updated successfully, but these errors were encountered: