Skip to content
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

Closed
DaveLiddament opened this issue Feb 15, 2021 · 7 comments · Fixed by #6149
Closed

fputcsv $fields parameter should be array of strings #5226

DaveLiddament opened this issue Feb 15, 2021 · 7 comments · Fixed by #6149

Comments

@DaveLiddament
Copy link
Contributor

DaveLiddament commented Feb 15, 2021

I think the fields parameter for fputcsv 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:

PHP Fatal error:  Uncaught Error: Object of class Person could not be converted to string in /home/vagrant/example/csv.php:24
Stack trace:
#0 /home/vagrant/example/csv.php(24): fputcsv()
#1 {main}
  thrown in /home/vagrant/example/csv.php on line 24

NOTE: I've seen a similar error with PHP 7.3 too.

@psalm-github-bot
Copy link

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);
Psalm output (using commit b7792ab):

No issues!

@DaveLiddament
Copy link
Contributor Author

See also: phpstan/phpstan-src#448

@orklah
Copy link
Collaborator

orklah commented Feb 15, 2021

Indeed, the function is documented as array. However, please consider allowing int or floats too at least. They can be casted to a string without any issue

@DaveLiddament
Copy link
Contributor Author

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.

@psalm-github-bot
Copy link

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);
Psalm output (using commit b7792ab):

No issues!

@DaveLiddament
Copy link
Contributor Author

null also works.... So here in updated snippet

@psalm-github-bot
Copy link

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);
Psalm output (using commit b7792ab):

No issues!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants