Skip to content
Snippets Groups Projects

Issue #3301239 by @immaculate.x : Check if the file is correctly opened before...

4 unresolved threads
Files
2
@@ -150,13 +150,14 @@ public function setURI($uri) {
* If the URI is not yet set.
*/
public function open() {
if (!empty($this->uri)) {
$this->fd = fopen($this->uri, 'rb');
$this->readHeader();
}
else {
if (empty($this->uri)) {
throw new \Exception('Cannot open stream without URI set.');
}
$this->fd = @fopen($this->uri, 'rb');
if (!$this->fd) {
throw new \Exception('Cannot open stream for uri ' . $this->uri);
}
$this->readHeader();
}
/**
@@ -245,7 +246,7 @@ private function readHeader() {
private function readLine() {
// Read a line and set the stream finished indicator if it was not
// possible anymore.
$line = fgets($this->fd);
$line = $this->fd ? fgets($this->fd) : FALSE;
    • When the file descriptor is not set we should make sure an error is logged in $this->errors[] and that we return FALSE.

      It would probably more readable if we check that $this->fd is set in a separate code block, something like this:

      if (empty($this->fd)) {
        $this->errors[] = new FormattableMarkup( ... );
        return FALSE;
      }
      
      $line = fgets($this->fd);
Please register or sign in to reply
$this->finished = ($line === FALSE);
if (!$this->finished) {
Loading