From 9aafc2b0b062e10d0fa5d61bfb551938fcf422e2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 28 Nov 2020 05:16:53 +0100 Subject: [PATCH 1/1] Continued: - added checks on $length parameter - added type-hints for primitive variables MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../file_directories/binary/class_BaseBinaryFile.php | 8 +++++++- .../file_directories/class_BaseAbstractFile.php | 4 ++-- .../input/raw/class_FrameworkRawFileInputPointer.php | 5 +---- .../text/class_FrameworkTextFileInputPointer.php | 2 +- .../io/class_FrameworkFileInputOutputPointer.php | 6 +++--- .../raw/class_FrameworkRawFileOutputPointer.php | 4 ++-- .../text/class_FrameworkTextFileOutputPointer.php | 4 ++-- .../index/file_stack/class_FileStackIndex.php | 10 +++++++++- .../classes/iterator/file/class_FileIterator.php | 2 +- .../classes/stacker/file/class_BaseFileStack.php | 12 ++++++++++-- .../interfaces/io/pointer/class_InputPointer.php | 2 +- .../interfaces/io/pointer/class_OutputPointer.php | 4 ++-- 12 files changed, 41 insertions(+), 22 deletions(-) diff --git a/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php b/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php index 819e78a6..6ba224e4 100644 --- a/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php +++ b/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php @@ -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); } diff --git a/framework/main/classes/file_directories/class_BaseAbstractFile.php b/framework/main/classes/file_directories/class_BaseAbstractFile.php index 69b15b06..ced4f119 100644 --- a/framework/main/classes/file_directories/class_BaseAbstractFile.php +++ b/framework/main/classes/file_directories/class_BaseAbstractFile.php @@ -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); } diff --git a/framework/main/classes/file_directories/input/raw/class_FrameworkRawFileInputPointer.php b/framework/main/classes/file_directories/input/raw/class_FrameworkRawFileInputPointer.php index b00b8a08..0a8b447e 100644 --- a/framework/main/classes/file_directories/input/raw/class_FrameworkRawFileInputPointer.php +++ b/framework/main/classes/file_directories/input/raw/class_FrameworkRawFileInputPointer.php @@ -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); diff --git a/framework/main/classes/file_directories/input/text/class_FrameworkTextFileInputPointer.php b/framework/main/classes/file_directories/input/text/class_FrameworkTextFileInputPointer.php index 03de7f42..642e555f 100644 --- a/framework/main/classes/file_directories/input/text/class_FrameworkTextFileInputPointer.php +++ b/framework/main/classes/file_directories/input/text/class_FrameworkTextFileInputPointer.php @@ -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 diff --git a/framework/main/classes/file_directories/io/class_FrameworkFileInputOutputPointer.php b/framework/main/classes/file_directories/io/class_FrameworkFileInputOutputPointer.php index a5411296..b67025c3 100644 --- a/framework/main/classes/file_directories/io/class_FrameworkFileInputOutputPointer.php +++ b/framework/main/classes/file_directories/io/class_FrameworkFileInputOutputPointer.php @@ -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(); diff --git a/framework/main/classes/file_directories/output/raw/class_FrameworkRawFileOutputPointer.php b/framework/main/classes/file_directories/output/raw/class_FrameworkRawFileOutputPointer.php index fc3ecd3a..e7453802 100644 --- a/framework/main/classes/file_directories/output/raw/class_FrameworkRawFileOutputPointer.php +++ b/framework/main/classes/file_directories/output/raw/class_FrameworkRawFileOutputPointer.php @@ -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); } diff --git a/framework/main/classes/file_directories/output/text/class_FrameworkTextFileOutputPointer.php b/framework/main/classes/file_directories/output/text/class_FrameworkTextFileOutputPointer.php index d53e7354..b4b8b8da 100644 --- a/framework/main/classes/file_directories/output/text/class_FrameworkTextFileOutputPointer.php +++ b/framework/main/classes/file_directories/output/text/class_FrameworkTextFileOutputPointer.php @@ -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); } diff --git a/framework/main/classes/index/file_stack/class_FileStackIndex.php b/framework/main/classes/index/file_stack/class_FileStackIndex.php index 0a104984..218b9b74 100644 --- a/framework/main/classes/index/file_stack/class_FileStackIndex.php +++ b/framework/main/classes/index/file_stack/class_FileStackIndex.php @@ -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)); + } } } diff --git a/framework/main/classes/iterator/file/class_FileIterator.php b/framework/main/classes/iterator/file/class_FileIterator.php index ff14542e..360996c2 100644 --- a/framework/main/classes/iterator/file/class_FileIterator.php +++ b/framework/main/classes/iterator/file/class_FileIterator.php @@ -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); } diff --git a/framework/main/classes/stacker/file/class_BaseFileStack.php b/framework/main/classes/stacker/file/class_BaseFileStack.php index faa1b0dc..1960259d 100644 --- a/framework/main/classes/stacker/file/class_BaseFileStack.php +++ b/framework/main/classes/stacker/file/class_BaseFileStack.php @@ -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)); diff --git a/framework/main/interfaces/io/pointer/class_InputPointer.php b/framework/main/interfaces/io/pointer/class_InputPointer.php index 092da082..faa64157 100644 --- a/framework/main/interfaces/io/pointer/class_InputPointer.php +++ b/framework/main/interfaces/io/pointer/class_InputPointer.php @@ -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); } diff --git a/framework/main/interfaces/io/pointer/class_OutputPointer.php b/framework/main/interfaces/io/pointer/class_OutputPointer.php index f9fb9bd2..a43557bd 100644 --- a/framework/main/interfaces/io/pointer/class_OutputPointer.php +++ b/framework/main/interfaces/io/pointer/class_OutputPointer.php @@ -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); } -- 2.39.2