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

Incorrect return type for SplFileObject::fgetcsv #464

Merged
merged 1 commit into from
Mar 5, 2021

Conversation

TomAdam
Copy link
Contributor

@TomAdam TomAdam commented Mar 5, 2021

Return type should be array|false instead of array|false|null. See https://www.php.net/manual/en/splfileobject.fgetcsv#refsect1-splfileobject.fgetcsv-returnvalues

@ondrejmirtes ondrejmirtes merged commit b3c7c40 into phpstan:master Mar 5, 2021
@ondrejmirtes
Copy link
Member

Thank you!

This was referenced Mar 9, 2021
This was referenced Mar 13, 2021
@VincentLanglet
Copy link
Contributor

@TomAdam @ondrejmirtes Even if the doc is saying that null is not a possible value, I can get some with

$commissionRateFile = new \SplFileObject($commissionRateFilePath);
$commissionRateFile->setFlags(\SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE);

You can see another example here: facebook/hhvm#5823

array(3) {
  [0] =>
  string(5) "row11"
  [1] =>
  string(0) ""
  [2] =>
  string(5) "row13"
}
array(3) {
  [0] =>
  string(5) "row21"
  [1] =>
  string(5) "row22"
  [2] =>
  string(5) "row23"
}
NULL

@ondrejmirtes
Copy link
Member

@VincentLanglet Your code example doesn't include fgetcsv call, I don't get how it's related to this method.

@VincentLanglet
Copy link
Contributor

I meant

$commissionRateFile = new \SplFileObject($commissionRateFilePath);
$commissionRateFile->setFlags(\SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE);
while (!$commissionRateFile->eof()) {
    $line = $commissionRateFile->fgetcsv(';');
}

The last line of my csv was an empty line, and the last $line in my code was null.

@ondrejmirtes
Copy link
Member

Can it be null in all cases or only for some setFlags combination?

@VincentLanglet
Copy link
Contributor

It's only related to some setFlags combination.

Without setFlags, I get [null].
With setFlags(\SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE), the empty lines was supposed to be skipped according to the doc, but it return null (instead of [null]).

@VincentLanglet
Copy link
Contributor

You can look at the psalm code https://raw.githubusercontent.com/vimeo/psalm/master/dictionaries/CallMap.php

They are using list<string>|array{0: null}|false|null

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.

3 participants