-
-
Notifications
You must be signed in to change notification settings - Fork 341
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
Fatal error with ltrim() when using the EmptyEscapeParser #358
Conversation
…escape parser and an empty last line
EmptyEscapeParser
EmptyEscapeParser
src/Polyfill/EmptyEscapeParser.php
Outdated
@@ -150,7 +150,9 @@ private static function filterDocument($document) | |||
private static function extractRecord(): array | |||
{ | |||
$record = []; | |||
self::$line = self::$document->fgets(); | |||
if (false === (self::$line = self::$document->fgets())) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would make the conditions more explicit ...
if (false === self::$line) {
return [null];
}
and that should be enough no ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would agree but that seems to confuse PHPStan:
------ ------------------------------------------------------------------
Line src/Polyfill/EmptyEscapeParser.php
------ ------------------------------------------------------------------
166 Strict comparison using !== between false and string will always
evaluate to true.
------ ------------------------------------------------------------------
See also: https://travis-ci.org/thephpleague/csv/jobs/594868542
It seems a bad idea to ignore the error completely but to ignore the error in just that file would require updating PHPStan which seems to open a can of worms (at least in my development environment).
This won't work with the current version of PHPStan. We need v0.11+:
ignoreErrors:
-
message: '#Strict comparison using \!\=\= between false and string will always evaluate to true.#'
path: src/Polyfill/EmptyEscapeParser.php
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can not update because of the PHP7.0+ requirement but I have no issue ignoring this PHPstan notice, this is kind of a trade off. The day we drop PHP7.2- we will be able to upgrade/improve the ignore message IMHO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think your patch is applied at the wrong lines 🤔 if you put the patch inside the while loop then PHPStan will not complained 👍 . You just need to patch the $buffer
not so much the raw self::$line
.
$buffer = '';
if (false !== self::$line) {
$buffer = ltrim(self::$line, self::$trim_mask);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's great - trade-off avoided!
]; | ||
|
||
$path = sys_get_temp_dir().'/test.csv'; | ||
file_put_contents($path, $source); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to use php://temp
or add a file to the data
directory under so that tests are not dependent of the underlying OS
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I completely overlooked the data
directory. I've rewritten the test to use the foo_readonly.csv
file.
I'll merge your patch. I may update the code but as it is it's fine by me ... thanks. Hopefully I'll make a release for that later this week. And I won't forget to mention your name in the CHANGELOG 🎉 |
version 9.4.1 is released with your bugfix 🎉 |
When calling
$csv->setEscape('');
I've noticed that when the last line of my read-only CSV created from path was empty the following error would occur:I believe this to be down to differences between the
SplFileObject
andLeague\Csv\Stream
implementations. For the empty linefgets()
on aSplFileObject
returns''
but using the procedural style orLeague\Csv\Stream
,false
is returned:I'm not sure this pull request contains the best way to rectify the issue but tests are passing and if nothing else this can start a discussion.