]> git.mxchange.org Git - core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Tue, 24 Nov 2020 06:15:51 +0000 (07:15 +0100)
committerRoland Häder <roland@mxchange.org>
Tue, 24 Nov 2020 06:15:51 +0000 (07:15 +0100)
- rewrote more assert() lines to thrown exceptions

Signed-off-by: Roland Häder <roland@mxchange.org>
framework/main/classes/stacker/file/class_BaseFileStack.php
framework/main/exceptions/class_
framework/main/exceptions/stacker/class_InvalidMagicException.php [new file with mode: 0644]

index 388f33ee822a052c85e4fca86a77434bdff26f23..6c1f3d1cfabfc3bbf4325dbd956af6d82aa1af24 100644 (file)
@@ -5,6 +5,7 @@ namespace Org\Mxchange\CoreFramework\Stacker\Filesystem;
 // Import framework stuff
 use Org\Mxchange\CoreFramework\Factory\Filesystem\Stack\FileStackIndexFactory;
 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
+use Org\Mxchange\CoreFramework\FileStack\InvalidMagicException;
 use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile;
 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
 use Org\Mxchange\CoreFramework\Index\Indexable;
@@ -14,6 +15,7 @@ use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
 
 // Import SPL stuff
 use \SplFileInfo;
+use \UnexpectedValueException;
 
 /**
  * A general file-based stack class
@@ -38,6 +40,9 @@ use \SplFileInfo;
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 abstract class BaseFileStack extends BaseStacker {
+       // Exception codes
+       const EXCEPTION_BAD_MAGIC = 0xe100;
+
        /**
         * Magic for this stack
         */
@@ -98,11 +103,12 @@ abstract class BaseFileStack extends BaseStacker {
         *
         * @return      void
         * @todo        To hard assertions here, better rewrite them to exceptions
+        * @throws      UnexpectedValueException        If header is not proper length
+        * @throws      InvalidMagicException   If a bad magic was found
         */
        public function readFileHeader () {
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: CALLED!', __METHOD__, __LINE__));
-
                // First rewind to beginning as the header sits at the beginning ...
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: CALLED!', __METHOD__, __LINE__));
                $this->getIteratorInstance()->rewind();
 
                // Then read it (see constructor for calculation)
@@ -110,13 +116,24 @@ abstract class BaseFileStack extends BaseStacker {
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Read %d bytes (%d wanted).', strlen($data), $this->getIteratorInstance()->getHeaderSize()));
 
                // Have all requested bytes been read?
-               assert(strlen($data) == $this->getIteratorInstance()->getHeaderSize());
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
+               if (strlen($data) != $this->getIteratorInstance()->getHeaderSize()) {
+                       // Bad data length
+                       throw new UnexpectedValueException(sprintf('data(%d)=%s does not match iteratorInstance->headerSize=%d',
+                               strlen($data),
+                               $data,
+                               $this->getIteratorInstance()->getHeaderSize()
+                       ));
+               }
 
                // Last character must be the separator
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: data(-1)=%s', dechex(ord(substr($data, -1, 1)))));
-               assert(substr($data, -1, 1) == chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES));
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
+               if (substr($data, -1, 1) !== chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)) {
+                       // Not valid separator
+                       throw new UnexpectedValueException(sprintf('data=%s does not have separator=%s at the end.',
+                               $data,
+                               BaseBinaryFile::SEPARATOR_HEADER_ENTRIES
+                       ));
+               }
 
                // Okay, then remove it
                $data = substr($data, 0, -1);
@@ -138,23 +155,39 @@ abstract class BaseFileStack extends BaseStacker {
 
                // Check if the array has only 3 elements
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: header(%d)=%s', count($header), print_r($header, true)));
-               assert(count($header) == 3);
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
-
-               // Check magic
-               assert($header[0] == self::STACK_MAGIC);
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
+               if (count($header) != 3) {
+                       // Header array count is not expected
+                       throw new UnexpectedValueException(sprintf('data=%s has %d elements, expected 3',
+                               $data,
+                               count($header)
+                       ));
+               } elseif ($header[0] != self::STACK_MAGIC) {
+                       // Bad magic
+                       throw new InvalidMagicException($data, self::EXCEPTION_BAD_MAGIC);
+               }
 
                // Check length of count and seek position
-               assert(strlen($header[1]) == BaseBinaryFile::LENGTH_COUNT);
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
-               assert(strlen($header[2]) == BaseBinaryFile::LENGTH_POSITION);
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
+               if (strlen($header[1]) != BaseBinaryFile::LENGTH_COUNT) {
+                       // Count length not valid
+                       throw new UnexpectedValueException(sprintf('header[1](%d)=%s is not expected %d length',
+                               strlen($header[1]),
+                               $header[1],
+                               BaseBinaryFile::LENGTH_COUNT
+                       ));
+               } elseif (strlen($header[1]) != BaseBinaryFile::LENGTH_POSITION) {
+                       // Position length not valid
+                       throw new UnexpectedValueException(sprintf('header[2](%d)=%s is not expected %d length',
+                               strlen($header[1]),
+                               $header[1],
+                               BaseBinaryFile::LENGTH_POSITION
+                       ));
+               }
 
                // Decode count and seek position
                $header[1] = hex2bin($header[1]);
                $header[2] = hex2bin($header[2]);
 
+               // Trace message
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: EXIT!', __METHOD__, __LINE__));
        }
 
@@ -164,9 +197,8 @@ abstract class BaseFileStack extends BaseStacker {
         * @return      void
         */
        public function flushFileHeader () {
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: CALLED!', __METHOD__, __LINE__));
-
                // Put all informations together
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: CALLED!', __METHOD__, __LINE__));
                $header = sprintf('%s%s%s%s%s%s',
                        // Magic
                        self::STACK_MAGIC,
@@ -190,6 +222,7 @@ abstract class BaseFileStack extends BaseStacker {
                // Write it to disk (header is always at seek position 0)
                $this->getIteratorInstance()->writeData(0, $header, false);
 
+               // Trace message
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: EXIT!', __METHOD__, __LINE__));
        }
 
index c6ce622851e162a0f2b497211a98c94bfeb05afc..8b8d392bc1a85e7fe588c1ffad8f400f4df25bf7 100644 (file)
@@ -29,7 +29,7 @@ class ???Exception extends FrameworkException {
         * @param       $code           Error code
         * @return      void
         */
-       public function __construct ($message, $code) {
+       public function __construct (string $message, int $code) {
                // Call parent exception constructor
                parent::__construct($message, $code);
        }
diff --git a/framework/main/exceptions/stacker/class_InvalidMagicException.php b/framework/main/exceptions/stacker/class_InvalidMagicException.php
new file mode 100644 (file)
index 0000000..ade8fe2
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+// Own namespace
+namespace Org\Mxchange\CoreFramework\FileStack;
+
+// Import framework stuff
+use Org\Mxchange\CoreFramework\Generic\FrameworkException;
+
+/**
+ * Invalid/bad magic found
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.ship-simu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class InvalidMagicException extends FrameworkException {
+       /**
+        * Constructor with data string
+        *
+        * @param       $data   Raw data
+        * @param       $code   Error code
+        * @return      void
+        */
+       public function __construct (string $data, int $code) {
+               // Construct message
+               $message = sprintf('data(%d)=%s has a bad magic, expected: %s',
+                       strlen($data),
+                       $data,
+                       BaseFileStack::STACK_MAGIC
+               );
+
+               // Call parent exception constructor
+               parent::__construct($message, $code);
+       }
+
+}