]> git.mxchange.org Git - core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Sat, 28 Nov 2020 04:16:53 +0000 (05:16 +0100)
committerRoland Häder <roland@mxchange.org>
Sat, 28 Nov 2020 04:18:03 +0000 (05:18 +0100)
- added checks on $length parameter
- added type-hints for primitive variables

Signed-off-by: Roland Häder <roland@mxchange.org>
12 files changed:
framework/main/classes/file_directories/binary/class_BaseBinaryFile.php
framework/main/classes/file_directories/class_BaseAbstractFile.php
framework/main/classes/file_directories/input/raw/class_FrameworkRawFileInputPointer.php
framework/main/classes/file_directories/input/text/class_FrameworkTextFileInputPointer.php
framework/main/classes/file_directories/io/class_FrameworkFileInputOutputPointer.php
framework/main/classes/file_directories/output/raw/class_FrameworkRawFileOutputPointer.php
framework/main/classes/file_directories/output/text/class_FrameworkTextFileOutputPointer.php
framework/main/classes/index/file_stack/class_FileStackIndex.php
framework/main/classes/iterator/file/class_FileIterator.php
framework/main/classes/stacker/file/class_BaseFileStack.php
framework/main/interfaces/io/pointer/class_InputPointer.php
framework/main/interfaces/io/pointer/class_OutputPointer.php

index 819e78a6e63bbe2734b541bc31010e085959c042..6ba224e416968a8ed0828c160563a0f5077a0687 100644 (file)
@@ -11,6 +11,7 @@ use Org\Mxchange\CoreFramework\Filesystem\File\BaseAbstractFile;
 
 // Import SPL stuff
 use \BadMethodCallException;
+use \InvalidArgumentException;
 use \SplFileInfo;
 
 /**
@@ -869,10 +870,15 @@ abstract class BaseBinaryFile extends BaseAbstractFile {
         *
         * @param       $length                 Length of raw data
         * @return      $seekPosition   Found next gap's seek position
+        * @throws      InvalidArgumentException        If the parameter is not valid
         */
        public function searchNextGap (int $length) {
                // If the file is only gaps, no need to seek
-               if ($this->isFileOnlyGaps()) {
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%d - CALLED!', $length));
+               if ($length <= 0) {
+                       // Throw IAE
+                       throw new InvalidArgumentException(sprintf('length=%d is not valid', $length));
+               } elseif ($this->isFileOnlyGaps()) {
                        // The first empty block is the first one right after the header
                        return ($this->getHeaderSize() + 1);
                }
index 69b15b06e72c62750a7cef99579a1ed09b66a95b..ced4f119a6d7c786db8736f62558addffbd8e7db 100644 (file)
@@ -126,7 +126,7 @@ abstract class BaseAbstractFile extends BaseFrameworkSystem implements FilePoint
         * @param       $totalEntries   Total entries in this file
         * @return      void
         */
-       protected final function setCounter ($counter) {
+       protected final function setCounter (int $counter) {
                // Set it
                $this->totalEntries = $counter;
        }
@@ -201,7 +201,7 @@ abstract class BaseAbstractFile extends BaseFrameworkSystem implements FilePoint
         * @throws      InvalidResourceException        If there is being set
         *                                                                                      an invalid file resource
         */
-       public function writeToFile ($dataStream) {
+       public function writeToFile (string $dataStream) {
                // Call pointer instance
                return $this->getPointerInstance()->writeToFile($dataStream);
        }
index b00b8a08d4ce43e67f2ac183ddc30ceb7a34cde2..0a8b447e52d0b5e1558c859bf8e59bcd597ffe7f 100644 (file)
@@ -125,10 +125,7 @@ class FrameworkRawFileInputPointer extends BaseFileIo implements InputPointer {
         * @param       $bytes  Amount of bytes to read
         * @return      $data   Data read from file
         */
-       public function read ($bytes = NULL) {
-               // $bytes shall be integer
-               assert(is_int($bytes));
-
+       public function read (int $bytes = NULL) {
                // Try to read given characters
                $data = $this->getFileObject()->fread($bytes);
 
index 03de7f424128a0f9f0906ea3104a1cf410dfc5e0..642e555fef2a9d2454019e0875ed2668b5c1d1af 100644 (file)
@@ -120,7 +120,7 @@ class FrameworkTextFileInputPointer extends BaseFileIo implements InputPointer {
         * @throws      NullPointerException    If the file pointer instance is not set by setFileObject()
         * @throws      InvalidResourceException        If there is no object being set
         */
-       public function read ($bytes = NULL) {
+       public function read (int $bytes = NULL) {
                // Some sanity checks
                if (is_null($this->getFileObject())) {
                        // Pointer not initialized
index a54112964135e21ae5e42d87e23fb9968e346d62..b67025c31edb4c1698780a53df33743119266ee2 100644 (file)
@@ -133,7 +133,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         * @param       $dataStream             The data stream we shall write to the file
         * @return      mixed                   Number of writes bytes or false on error
         */
-       public function writeToFile ($dataStream) {
+       public function writeToFile (string $dataStream) {
                // Validate the pointer
                $this->validateFilePointer();
 
@@ -148,7 +148,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         * @param       $data                   Data to be written
         * @return      mixed                   Number of writes bytes or false on error
         */
-       public function writeAtPosition ($seekPosition, $data) {
+       public function writeAtPosition (int $seekPosition, string $data) {
                // First seek to it
                $this->seek($seekPosition);
 
@@ -200,7 +200,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         * @param       $bytes  Amount of bytes to read
         * @return      $data   Data read from file
         */
-       public function read ($bytes = NULL) {
+       public function read (int $bytes = NULL) {
                // Validate the pointer
                $this->validateFilePointer();
 
index fc3ecd3a13c9773ae15d12ed11d8d96d386d9aef..e7453802acc5704342a1bcf2bc0a22fba876d5df 100644 (file)
@@ -86,7 +86,7 @@ class FrameworkRawFileOutputPointer extends BaseFileIo implements OutputPointer
         * @throws      NullPointerException    If the file pointer instance is not set by setFileObject()
         * @throws      LogicException  If there is no object being set
         */
-       public function writeToFile ($dataStream) {
+       public function writeToFile (string $dataStream) {
                if (is_null($this->getFileObject())) {
                        // Pointer not initialized
                        throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
@@ -119,7 +119,7 @@ class FrameworkRawFileOutputPointer extends BaseFileIo implements OutputPointer
         * @return      mixed                   Number of writes bytes or false on error
         * @throws      UnsupportedOperationException   If this method is called
         */
-       public function writeAtPosition ($seedPosition, $data) {
+       public function writeAtPosition (int $seedPosition, string $data) {
                throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
        }
 
index d53e7354ad6153546b08147afc80cb2067cc108b..b4b8b8dadfc14bdd56f04455faa8d07b9b2075bf 100644 (file)
@@ -89,7 +89,7 @@ class FrameworkTextFileOutputPointer extends BaseFileIo implements OutputPointer
         * @throws      NullPointerException    If the file pointer instance is not set by setFileObject()
         * @throws      LogicException  If there is no object being set
         */
-       public function writeToFile ($dataStream) {
+       public function writeToFile (string $dataStream) {
                if (is_null($this->getFileObject())) {
                        // Pointer not initialized
                        throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
@@ -122,7 +122,7 @@ class FrameworkTextFileOutputPointer extends BaseFileIo implements OutputPointer
         * @return      mixed                   Number of writes bytes or false on error
         * @throws      UnsupportedOperationException   If this method is called
         */
-       public function writeAtPosition ($seedPosition, $data) {
+       public function writeAtPosition (int $seedPosition, string $data) {
                throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
        }
 
index 0a10498440c538694aac313349475e5b9cd407b0..218b9b74f4e8e8d44fdd9b9b8913ed211a747555 100644 (file)
@@ -10,6 +10,7 @@ use Org\Mxchange\CoreFramework\Stacker\Filesystem\BaseFileStack;
 use Org\Mxchange\CoreFramework\Stacker\Index\IndexableStack;
 
 // Import SPL stuff
+use \InvalidArgumentException;
 use \SplFileInfo;
 
 /**
@@ -107,9 +108,16 @@ class FileStackIndex extends BaseIndex implements IndexableStack, Registerable {
         *
         * @param       $length                 Length of raw data
         * @return      $seekPosition   Found next gap's seek position
+        * @throws      InvalidArgumentException        If the parameter is not valid
+        * @todo        Unfinished work
         */
        public function searchNextGap (int $length) {
-               $this->partialStub('length=' . $length);
+               // Validate parameter
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: length=%d - CALLED!', $length));
+               if ($length <= 0) {
+                       // Throw IAE
+                       throw new InvalidArgumentException(sprintf('length=%d is not valid', $length));
+               }
        }
 
 }
index ff14542e96be872985e4a662d20f1501a7b11f50..360996c23476f309587c5573d73c1eb342663952 100644 (file)
@@ -353,7 +353,7 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator
         */
        public function searchNextGap (int $length) {
                // Call block instance
-               print $this->getBlockInstance()->__toString() . PHP_EOL;
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: length=%d - CALLED!', $length));
                return $this->getBlockInstance()->searchNextGap($length);
        }
 
index faa1b0dc513f7b3463fef1a69c8988be2afba879..1960259d04732e4c089505674822aca946646acc 100644 (file)
@@ -609,7 +609,8 @@ abstract class BaseFileStack extends BaseStacker {
         * @throws      UnsupportedOperationException   This method is not (and maybe never will be) supported
         */
        public function searchNextGap (int $length) {
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: length=%s', $length));
+               // Not supported here
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: length=%d - CALLED!', $length));
                throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
        }
 
@@ -647,7 +648,14 @@ abstract class BaseFileStack extends BaseStacker {
                $gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData));
 
                // Gap position cannot be smaller than header length + 1
-               assert($gapPosition > $this->getIteratorInstance()->getHeaderSize());
+               if ($gapPosition <= $this->getIteratorInstance()->getHeaderSize()) {
+                       // Improper gap position
+                       throw new UnexpectedValueException(sprintf('gapPosition[%s]=%d is not larger than headerSize=%d',
+                               gettype($gapPosition),
+                               $gapPosition,
+                               $this->getIteratorInstance()->getHeaderSize()
+                       ));
+               }
 
                // Then write the data at that gap
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,gapPosition=%s', $groupId, $hash, $gapPosition));
index 092da082f26f42592bcd974b0444a7a606b74d30..faa6415720ecaca04388cb1851062774c9b9797c 100644 (file)
@@ -52,6 +52,6 @@ interface InputPointer extends StreamableInput, FilePointer {
         *                                                                      is not set by setFileObject()
         * @throws      InvalidResourceException        If there is being set
         */
-       function read ($bytes = NULL);
+       function read (int $bytes = NULL);
 
 }
index f9fb9bd2cb4884ca971aff954a4c0c2bc0031987..a43557bd9d419224a24bdab296ecd14ac2888a1a 100644 (file)
@@ -39,7 +39,7 @@ interface OutputPointer extends StreamableOutput, FilePointer {
         * @throws      InvalidResourceException        If there is being set
         *                                                                                      an invalid file resource
         */
-       function writeToFile ($dataStream);
+       function writeToFile (string $dataStream);
 
        /**
         * Writes at given position by seeking to it.
@@ -48,6 +48,6 @@ interface OutputPointer extends StreamableOutput, FilePointer {
         * @param       $data                   Data to be written
         * @return      mixed                   Number of writes bytes or false on error
         */
-       function writeAtPosition ($seedPosition, $data);
+       function writeAtPosition (int $seedPosition, string $data);
 
 }