Skip to content

Commit

Permalink
feat: also handle stream error message in ffgets
Browse files Browse the repository at this point in the history
  • Loading branch information
n-peugnet committed Jan 22, 2024
1 parent 9e256fa commit 3000632
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

### Changed

- Stream reading errors are silenced in parseObjects, an exception that contains
the error message is still thrown.
- Stream reading errors in `parseObjects()` and `parseHeader()` are silenced,
an exception is still thrown but it now contains the error message.

### Removed

Expand Down
5 changes: 4 additions & 1 deletion src/SphinxInventoryParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,11 @@ protected function parseObjectsV2(string $baseURI): Generator {
*/
function ffgets($stream, ?int $length = null): string
{
$line = is_null($length) ? fgets($stream) : fgets($stream, $length);
$line = is_null($length) ? @fgets($stream) : @fgets($stream, $length);
if ($line === false) {
if (!feof($stream)) {
throw new UnexpectedValueException('could not read line: ' . error_msg());
}
throw new UnexpectedValueException('unexpected end of file');
}
return $line;
Expand Down
10 changes: 10 additions & 0 deletions tests/SphinxInventoryParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ public function testParseHeaderEmptyProject(): void
fclose($stream);
}

public function testParseHeaderStreamError(): void
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage("could not read line: fgets():");
$stream = fopen(__DIR__ . "/data/valid.header", 'a');
$parser = new SphinxInventoryParser($stream);
$parser->parseHeader();
fclose($stream);
}

public function testParseFromDocException(): void
{
$this->expectException(RuntimeException::class);
Expand Down

0 comments on commit 3000632

Please sign in to comment.