]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/ReversedFileReader.php
Update function / rearrange tab order
[friendica.git] / src / Util / ReversedFileReader.php
index 248792f17aa81786f43bda72f9fa49adfce4f797..58fb2cb3faaceab460c86ed8192cb103625725fe 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2021, Friendica
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -55,12 +55,12 @@ class ReversedFileReader implements \Iterator
         * @param string $filename      File to open
         * @return $this
         */
-       public function open(string $filename)
+       public function open(string $filename): ReversedFileReader
        {
                $this->fh = fopen($filename, 'r');
                if (!$this->fh) {
                        // this should use a custom exception.
-                       throw \Exception("Unable to open $filename");
+                       throw new \Exception("Unable to open $filename");
                }
                $this->filesize = filesize($filename);
                $this->pos      = -1;
@@ -72,10 +72,11 @@ class ReversedFileReader implements \Iterator
 
        /**
         * Read $size bytes behind last position
-        * 
+        *
+        * @param int $size
         * @return string
         */
-       private function _read($size)
+       private function _read(int $size): string
        {
                $this->pos -= $size;
                fseek($this->fh, $this->pos);
@@ -85,16 +86,19 @@ class ReversedFileReader implements \Iterator
        /**
         * Read next line from end of file
         * Return null if no lines are left to read
-        * 
-        * @return ?string
+        *
+        * @return string|null Depending on data being buffered
         */
-       private function _readline()
+       private function _readline(): ?string
        {
                $buffer = & $this->buffer;
                while (true) {
                        if ($this->pos == 0) {
                                return array_pop($buffer);
                        }
+                       if (is_null($buffer)) {
+                               return null;
+                       }
                        if (count($buffer) > 1) {
                                return array_pop($buffer);
                        }
@@ -104,10 +108,11 @@ class ReversedFileReader implements \Iterator
 
        /**
         * Fetch next line from end and set it as current iterator value.
-        * 
+        *
         * @see Iterator::next()
         * @return void
         */
+       #[\ReturnTypeWillChange]
        public function next()
        {
                ++$this->key;
@@ -116,10 +121,11 @@ class ReversedFileReader implements \Iterator
 
        /**
         * Rewind iterator to the first line at the end of file
-        * 
+        *
         * @see Iterator::rewind()
-        * @return void 
+        * @return void
         */
+       #[\ReturnTypeWillChange]
        public function rewind()
        {
                if ($this->filesize > 0) {
@@ -133,33 +139,33 @@ class ReversedFileReader implements \Iterator
 
        /**
         * Return current line number, starting from zero at the end of file
-        * 
+        *
         * @see Iterator::key()
         * @return int
         */
-       public function key()
+       public function key(): int
        {
                return $this->key;
        }
 
        /**
         * Return current line
-        * 
+        *
         * @see Iterator::current()
         * @return string
         */
-       public function current()
+       public function current(): string
        {
                return $this->value;
        }
 
        /**
-        * Checks if current iterator value is valid, that is, we readed all lines in files
-        * 
+        * Checks if current iterator value is valid, that is, we read all lines in files
+        *
         * @see Iterator::valid()
         * @return bool
         */
-       public function valid()
+       public function valid(): bool
        {
                return ! is_null($this->value);
        }