Skip to content

Conversation

@DaveLiddament
Copy link
Contributor

@DaveLiddament DaveLiddament commented Feb 15, 2021

Fix for issues added in #448

The `fields` parameter for `fputcsv` should be an array of strings. See [PHP manual](https://www.php.net/manual/en/function.fputcsv.php).

This should fix [this snippet](https://phpstan.org/r/65420d60-d1fd-4eef-9ba3-e057672082ee) by correctly reporting 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()
phpstan#1 {main}
  thrown in /home/vagrant/example/csv.php on line 24
```

NOTE: I've seen  a similar error with PHP 7.3 too.
@ondrejmirtes
Copy link
Member

As I said here #448 (comment) Stringable doesn't work. You need to do a similar thing that's done here for sprintf:

if (
$parameterSignature->getName() === 'values'
&& (
$lowerCasedFunctionName === 'printf'
|| $lowerCasedFunctionName === 'sprintf'
)
) {
$type = new UnionType([
new StringAlwaysAcceptingObjectWithToStringType(),
new IntegerType(),
new FloatType(),
new NullType(),
new BooleanType(),
]);
}
(and write a test for CallToFunctionParametersRuleTest).

@DaveLiddament
Copy link
Contributor Author

I'll create another PR with correct code. Closing this one.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants