Skip to content

Commit

Permalink
Update internal codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Sep 25, 2023
1 parent 9261f4f commit ea07f8a
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
use function array_combine;
use function array_fill;
use function array_filter;
use function array_key_exists;
use function array_merge;
use function array_pad;
use function array_shift;
use function array_slice;
use function array_unique;
use function count;
Expand Down Expand Up @@ -309,7 +312,7 @@ public function withoutFormatter(): self
* @throws ParserError
* @throws SyntaxError
*/
public function parseFile($filenameOrStream, $filenameContext = null): TabularDataReader
public function parseFile(mixed $filenameOrStream, $filenameContext = null): TabularDataReader
{
if (is_resource($filenameOrStream)) {
return $this->parseHTML($this->streamToString($filenameOrStream));
Expand All @@ -332,23 +335,6 @@ public function parseFile($filenameOrStream, $filenameContext = null): TabularDa
return $this->parseHTML($html);
}

/**
* @param resource $stream
*
* @throws ParserError
*/
private function streamToString($stream): string
{
set_error_handler(fn (int $errno, string $errstr, string $errfile, int $errline) => true);
$html = stream_get_contents($stream);
restore_error_handler();

return match (false) {
$html => throw new ParserError('The resource could not be read.'),
default => $html,
};
}

/**
* @throws ParserError
* @throws SyntaxError
Expand All @@ -372,6 +358,23 @@ public function parseHTML(DOMDocument|DOMElement|SimpleXMLElement|Stringable|str
return new ResultSet($this->extractTableContents($xpath, $header), $header);
}

/**
* @param resource $stream
*
* @throws ParserError
*/
private function streamToString($stream): string
{
set_error_handler(fn (int $errno, string $errstr, string $errfile, int $errline) => true);
$html = stream_get_contents($stream);
restore_error_handler();

return match (false) {
$html => throw new ParserError('The resource could not be read.'),
default => $html,
};
}

/**
* @throws ParserError
*/
Expand Down

0 comments on commit ea07f8a

Please sign in to comment.