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

Fatal error with ltrim() when using the EmptyEscapeParser #358

Merged
merged 4 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Polyfill/EmptyEscapeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Copy link
Member

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 ?

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Member

@nyamsprod nyamsprod Oct 8, 2019

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);
            }

Copy link
Contributor Author

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!

return [null];
}
do {
$method = 'extractFieldContent';
$buffer = ltrim(self::$line, self::$trim_mask);
Expand Down
29 changes: 29 additions & 0 deletions tests/Polyfill/EmptyEscapeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,35 @@ public function testPreserveEmptyLines()
}
}

/**
* @covers ::parse
* @covers ::extractRecord
* @covers ::extractFieldContent
* @covers ::extractEnclosedFieldContent
*/
public function testReadingOnlyStream()
{
$source = <<<EOF
"1","2"

EOF;

$expected = [
['1', '2'],
[null],
];

$path = sys_get_temp_dir().'/test.csv';
file_put_contents($path, $source);
Copy link
Member

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

Copy link
Contributor Author

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.


$stream = Stream::createFromPath($path);
foreach (EmptyEscapeParser::parse($stream) as $offset => $record) {
self::assertSame($expected[$offset], $record);
}

unlink($path);
}

/**
* @covers ::parse
* @covers ::extractRecord
Expand Down