From 196ab0fc031d6987c87e8c5d257c39f664ec9151 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 6 Dec 2020 05:20:03 +0100 Subject: [PATCH] Continued: - added more parameter validation - added commented-out noisy debug lines - rewrote more assert() calls to thrown exceptions (not private methods) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../main/classes/index/class_BaseIndex.php | 32 ++-- .../classes/stacker/class_BaseStacker.php | 142 ++++++++++++++---- .../stacker/fifo/class_FiFoStacker.php | 41 ++++- .../stacker/file/class_BaseFileStack.php | 105 ++++++++++--- .../stacker/file/fifo/class_FiFoFileStack.php | 74 ++++++++- .../stacker/filo/class_FiLoStacker.php | 40 ++++- 6 files changed, 366 insertions(+), 68 deletions(-) diff --git a/framework/main/classes/index/class_BaseIndex.php b/framework/main/classes/index/class_BaseIndex.php index 6f0c5a14..0392f36b 100644 --- a/framework/main/classes/index/class_BaseIndex.php +++ b/framework/main/classes/index/class_BaseIndex.php @@ -76,7 +76,7 @@ abstract class BaseIndex extends BaseFrameworkSystem { * Reads the file header * * @return void - * @throws UnexpectedValueException If header length is invalid + * @throws UnexpectedValueException If header length or count of elements is invalid */ public function readFileHeader () { // First rewind to beginning as the header sits at the beginning ... @@ -87,7 +87,7 @@ abstract class BaseIndex extends BaseFrameworkSystem { $data = $this->getIteratorInstance()->read($this->getIteratorInstance()->getHeaderSize()); // Have all requested bytes been read? - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Read %d bytes (%d wanted).', strlen($data), $this->getIteratorInstance()->getHeaderSize())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: Read %d bytes (%d wanted).', strlen($data), $this->getIteratorInstance()->getHeaderSize())); if (strlen($data) != $this->getIteratorInstance()->getHeaderSize()) { // Invalid header length throw new UnexpectedValueException(sprintf('data(%d)=%s is not expected length %d', @@ -117,22 +117,26 @@ abstract class BaseIndex extends BaseFrameworkSystem { */ $header = explode(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA), $data); - // Set it here - $this->getIteratorInstance()->setHeader($header); - // Check if the array has only 3 elements - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('header(%d)=%s', count($header), print_r($header, true))); - assert(count($header) == 2); - - // Check magic - assert($header[0] == self::INDEX_MAGIC); - - // Check length of count - assert(strlen($header[1]) == BaseBinaryFile::LENGTH_COUNT); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: header()=%d', count($header))); + //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: header(%d)=%s', count($header), print_r($header, true))); + if (count($header) != 2) { + // Bad header + throw new UnexpectedValueException(sprintf('header()=%d is not expected value 2', count($header))); + } elseif ($header[0] !== self::INDEX_MAGIC) { + // Magic must be in first element + throw new UnexpectedValueException(sprintf('header[0]=%s is not the expected magic (%s)', $header[0], self::INDEX_MAGIC)); + } elseif (strlen($header[1]) != BaseBinaryFile::LENGTH_COUNT) { + // Length of total entries not matching + throw new UnexpectedValueException(sprintf('header[1](%d)=%s does not have expected length %d', strlen($header[1]), $header[1], BaseBinaryFile::LENGTH_COUNT)); + } // Decode count $header[1] = hex2bin($header[1]); + // Set it here + $this->getIteratorInstance()->setHeader($header); + // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: EXIT!'); } @@ -175,7 +179,7 @@ abstract class BaseIndex extends BaseFrameworkSystem { */ protected function initIndex (SplFileInfo $fileInfoInstance) { // Get a file i/o pointer instance for index file - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('fileInfoInstance[%s]=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: fileInfoInstance[%s]=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance)); $fileInstance = ObjectFactory::createObjectByConfiguredName('index_file_class', array($fileInfoInstance, $this)); // Get iterator instance diff --git a/framework/main/classes/stacker/class_BaseStacker.php b/framework/main/classes/stacker/class_BaseStacker.php index 9e44c9b4..70c6a229 100644 --- a/framework/main/classes/stacker/class_BaseStacker.php +++ b/framework/main/classes/stacker/class_BaseStacker.php @@ -8,6 +8,7 @@ use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; // Import SPL stuff use \BadMethodCallException; +use \InvalidArgumentException; /** * A general Stacker @@ -55,30 +56,51 @@ abstract class BaseStacker extends BaseFrameworkSystem { * @param $stackerName Name of the stack * @param $forceReInit Force re-initialization * @return void + * @throws InvalidArgumentException If a parameter is invalid * @throws AlreadyInitializedStackerException If the stack is already initialized */ public function initStack (string $stackerName, bool $forceReInit = false) { - // Is the stack already initialized? - if (($forceReInit === false) && ($this->isStackInitialized($stackerName))) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s,forceReInit=%d - CALLED!', $stackerName, intval($forceReInit))); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } elseif (($forceReInit === false) && ($this->isStackInitialized($stackerName))) { // Then throw the exception + // @TODO Change to BMCE throw new AlreadyInitializedStackerException(array($this, $stackerName, $forceReInit), self::EXCEPTION_STACKER_ALREADY_INITIALIZED); - } // END - if + } // Initialize the given stack $this->initGenericArrayKey('stacks', $stackerName, 'entries', $forceReInit); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-STACKER: EXIT!'); } /** * Initializes all stacks * * @return void + * @throws InvalidArgumentException If a parameter is invalid */ public function initStacks (array $stacks, bool $forceReInit = false) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stacks()=%d,forceReInit=%d - CALLED!', count($stacks), intval($forceReInit))); + if (count($stacks) == 0) { + // No empty stack name + throw new InvalidArgumentException('Array "stacks" is empty'); + } + // "Walk" through all (more will be added as needed foreach ($stacks as $stackerName) { // Init this stack + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Calling this->initStack(%s,%d) ...', $stackerName, intval($forceReInit))); $this->initStack($stackerName, $forceReInit); - } // END - foreach + } + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-STACKER: EXIT!'); } /** @@ -86,12 +108,21 @@ abstract class BaseStacker extends BaseFrameworkSystem { * * @param $stackerName Name of the stack * @return $isInitialized Whether the stack is initialized + * @throws InvalidArgumentException If a parameter is invalid */ public function isStackInitialized (string $stackerName) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } + // Is is there? $isInitialized = ($this->isValidGenericArrayKey('stacks', $stackerName, 'entries')); // Return result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: isInitialized=%d - EXIT!', intval($isInitialized))); return $isInitialized; } @@ -100,19 +131,25 @@ abstract class BaseStacker extends BaseFrameworkSystem { * * @param $stackerName Name of the stack * @return $isFull Whether the stack is full + * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If given stack is missing */ protected function isStackFull (string $stackerName) { - // Is the stack not yet initialized? - if (!$this->isStackInitialized($stackerName)) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } elseif (!$this->isStackInitialized($stackerName)) { // Throw an exception throw new BadMethodCallException(sprintf('stackerName=%s not yet initialized but method called.', $stackerName), self::EXCEPTION_NO_STACKER_FOUND); - } // END - if + } // So, is the stack full? $isFull = (($this->getStackCount($stackerName)) == FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('stacker_' . $stackerName . '_max_size')); // Return result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: isFull=%d - EXIT!', intval($isFull))); return $isFull; } @@ -121,19 +158,25 @@ abstract class BaseStacker extends BaseFrameworkSystem { * * @param $stackerName Name of the stack * @return $isEmpty Whether the stack is empty + * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If given stack is missing */ public function isStackEmpty (string $stackerName) { - // Is the stack not yet initialized? - if (!$this->isStackInitialized($stackerName)) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } elseif (!$this->isStackInitialized($stackerName)) { // Throw an exception throw new BadMethodCallException(sprintf('stackerName=%s not yet initialized but method called.', $stackerName), self::EXCEPTION_NO_STACKER_FOUND); - } // END - if + } // So, is the stack empty? $isEmpty = (($this->getStackCount($stackerName)) == 0); // Return result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: isEmpty=%d - EXIT!', intval($isEmpty))); return $isEmpty; } @@ -142,19 +185,25 @@ abstract class BaseStacker extends BaseFrameworkSystem { * * @param $stackerName Name of the stack * @return $count Size of stack (array count) + * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If given stack is missing */ public function getStackCount (string $stackerName) { - // Is the stack not yet initialized? - if (!$this->isStackInitialized($stackerName)) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } elseif (!$this->isStackInitialized($stackerName)) { // Throw an exception throw new BadMethodCallException(sprintf('stackerName=%s not yet initialized but method called.', $stackerName), self::EXCEPTION_NO_STACKER_FOUND); - } // END - if + } // Now, count the array of entries $count = $this->countGenericArrayElements('stacks', $stackerName, 'entries'); // Return result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: count=%d - EXIT!', $count)); return $count; } @@ -164,13 +213,19 @@ abstract class BaseStacker extends BaseFrameworkSystem { * @param $stackerName Name of the stack * @param $value Value to add to this stacker * @return void + * @throws InvalidArgumentException If a parameter is invalid + * @throws BadMethodCallException If given stack is missing * @throws FullStackerException Thrown if the stack is full */ protected function addValue (string $stackerName, $value) { - // Is the stack not yet initialized or full? - if (!$this->isStackInitialized($stackerName)) { - // Then do it here - $this->initStack($stackerName); + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s,value[]=%s - CALLED!', $stackerName, gettype($value))); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } elseif (!$this->isStackInitialized($stackerName)) { + // Throw an exception + throw new BadMethodCallException(sprintf('stackerName=%s not yet initialized but method called.', $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } elseif ($this->isStackFull($stackerName)) { // Stacker is full throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL); @@ -178,6 +233,9 @@ abstract class BaseStacker extends BaseFrameworkSystem { // Now add the value to the stack $this->pushValueToGenericArrayKey('stacks', $stackerName, 'entries', $value); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-STACKER: EXIT!'); } /** @@ -185,12 +243,17 @@ abstract class BaseStacker extends BaseFrameworkSystem { * * @param $stackerName Name of the stack * @return $value Value of last added value + * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If the named stacker was not found * @throws BadMethodCallException If the named stacker is empty */ protected function getLastValue (string $stackerName) { - // Is the stack not yet initialized or full? - if (!$this->isStackInitialized($stackerName)) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } elseif (!$this->isStackInitialized($stackerName)) { // Throw an exception throw new BadMethodCallException(sprintf('stackerName=%s not yet initialized but method called.', $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } elseif ($this->isStackEmpty($stackerName)) { @@ -202,6 +265,7 @@ abstract class BaseStacker extends BaseFrameworkSystem { $value = $this->getGenericArrayElement('stacks', $stackerName, 'entries', $this->getStackCount($stackerName) - 1); // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value))); return $value; } @@ -210,12 +274,17 @@ abstract class BaseStacker extends BaseFrameworkSystem { * * @param $stackerName Name of the stack * @return $value Value of last added value + * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If the named stacker was not found * @throws BadMethodCallException If the named stacker is empty */ protected function getFirstValue (string $stackerName) { - // Is the stack not yet initialized or full? - if (!$this->isStackInitialized($stackerName)) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } elseif (!$this->isStackInitialized($stackerName)) { // Throw an exception throw new BadMethodCallException(sprintf('stackerName=%s not yet initialized but method called.', $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } elseif ($this->isStackEmpty($stackerName)) { @@ -227,6 +296,7 @@ abstract class BaseStacker extends BaseFrameworkSystem { $value = $this->getGenericArrayElement('stacks', $stackerName, 'entries', 0); // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value))); return $value; } @@ -235,12 +305,17 @@ abstract class BaseStacker extends BaseFrameworkSystem { * * @param $stackerName Name of the stack * @return $value Value "poped" from array + * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If the named stacker was not found * @throws BadMethodCallException If the named stacker is empty */ protected function popLast (string $stackerName) { - // Is the stack not yet initialized or full? - if (!$this->isStackInitialized($stackerName)) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } elseif (!$this->isStackInitialized($stackerName)) { // Throw an exception throw new BadMethodCallException(sprintf('stackerName=%s not yet initialized but method called.', $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } elseif ($this->isStackEmpty($stackerName)) { @@ -249,7 +324,11 @@ abstract class BaseStacker extends BaseFrameworkSystem { } // Now, remove the last entry, we don't care about the return value here, see elseif() block above - return $this->popGenericArrayElement('stacks', $stackerName, 'entries'); + $value = $this->popGenericArrayElement('stacks', $stackerName, 'entries'); + + // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value))); + return $value; } /** @@ -257,12 +336,17 @@ abstract class BaseStacker extends BaseFrameworkSystem { * * @param $stackerName Name of the stack * @return $value Value "shifted" from array + * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If the named stacker was not found * @throws BadMethodCallException If the named stacker is empty */ protected function popFirst (string $stackerName) { - // Is the stack not yet initialized or full? - if (!$this->isStackInitialized($stackerName)) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } elseif (!$this->isStackInitialized($stackerName)) { // Throw an exception throw new BadMethodCallException(sprintf('stackerName=%s not yet initialized but method called.', $stackerName), self::EXCEPTION_NO_STACKER_FOUND); } elseif ($this->isStackEmpty($stackerName)) { @@ -271,7 +355,11 @@ abstract class BaseStacker extends BaseFrameworkSystem { } // Now, remove the last entry, we don't care about the return value here, see elseif() block above - return $this->shiftGenericArrayElement('stacks', $stackerName, 'entries'); + $value = $this->shiftGenericArrayElement('stacks', $stackerName, 'entries'); + + // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value))); + return $value; } } diff --git a/framework/main/classes/stacker/fifo/class_FiFoStacker.php b/framework/main/classes/stacker/fifo/class_FiFoStacker.php index 1e4743f8..aafe1178 100644 --- a/framework/main/classes/stacker/fifo/class_FiFoStacker.php +++ b/framework/main/classes/stacker/fifo/class_FiFoStacker.php @@ -2,6 +2,9 @@ // Own namespace namespace Org\Mxchange\CoreFramework\Stack; +// Import SPL stuff +use \InvalidArgumentException; + /** * A FiFo Stacker class * @@ -57,11 +60,23 @@ class FiFoStacker extends BaseStacker implements Stackable { * @param $stackerName Name of the stack * @param $value Value to push on it * @return void + * @throws InvalidArgumentException If a parameter is invalid * @throws StackerFullException If the stack is full */ public function pushNamed (string $stackerName, $value) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s,value[]=%s - CALLED!', $stackerName, gettype($value))); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } + // Call the protected method + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Calling parent::addValue(%s,%s) ...', $stackerName, gettype($value))); parent::addValue($stackerName, $value); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-STACKER: EXIT!'); } /** @@ -69,17 +84,28 @@ class FiFoStacker extends BaseStacker implements Stackable { * * @param $stackerName Name of the stack * @return $value Value of the current stack entry + * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If the named stacker was not found * @throws BadMethodCallException If the named stacker is empty */ public function popNamed (string $stackerName) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } + // Get the value + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Calling this->getNamed(%s) ...', $stackerName)); $value = $this->getNamed($stackerName); // Call the protected method + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Calling parent::popFirst(%s) ...', $stackerName)); parent::popFirst($stackerName); // Return the value + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value))); return $value; } @@ -88,12 +114,25 @@ class FiFoStacker extends BaseStacker implements Stackable { * * @param $stackerName Name of the stack * @return $value Value of last added value + * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If the named stacker was not found * @throws BadMethodCallException If the named stacker is empty */ public function getNamed (string $stackerName) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } + // Call the protected method - return parent::getFirstValue($stackerName); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: Calling parent::getFirstValue(%s) ...', $stackerName)); + $value = parent::getFirstValue($stackerName); + + // Return value + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-STACKER: value[]=%s - EXIT!', gettype($value))); + return $value; } } diff --git a/framework/main/classes/stacker/file/class_BaseFileStack.php b/framework/main/classes/stacker/file/class_BaseFileStack.php index 0052a8cb..5513c777 100644 --- a/framework/main/classes/stacker/file/class_BaseFileStack.php +++ b/framework/main/classes/stacker/file/class_BaseFileStack.php @@ -70,7 +70,7 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { */ public function readFileHeader () { // 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__)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: CALLED!'); $this->getIteratorInstance()->rewind(); // Then read it (see constructor for calculation) @@ -160,7 +160,7 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { */ public function flushFileHeader () { // Put all informations together - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: CALLED!', __METHOD__, __LINE__)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: CALLED!'); $header = sprintf('%s%s%s%s%s%s', // Magic StackableFile::STACK_MAGIC, @@ -185,7 +185,7 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { $this->getIteratorInstance()->writeData(0, $header, false); // Trace message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: EXIT!', __METHOD__, __LINE__)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: EXIT!'); } /** @@ -250,6 +250,9 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { // And set it here $this->setIndexInstance($indexInstance); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: EXIT!'); } /** @@ -259,12 +262,16 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { * @param $value Value to add to this stacker * @return void * @throws FullStackerException If the stack is full + * @throws InvalidArgumentException If a parameter is not valid * @throws InvalidArgumentException Not all variable types are wanted here */ protected function addValue (string $stackerName, $value) { - // Do some tests - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('stackerName=%s,value[%s]=%s - CALLED!', $stackerName, gettype($value), print_r($value, true))); - if ($this->isStackFull($stackerName)) { + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s,value[%s]=%s - CALLED!', $stackerName, gettype($value), print_r($value, true))); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } elseif ($this->isStackFull($stackerName)) { // Stacker is full throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL); } elseif (is_resource($value) || is_object($value)) { @@ -280,6 +287,9 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { // Add the hash and gap position to the index $this->getIndexInstance()->addHashToIndex($stackerName, $data); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: EXIT!'); } /** @@ -287,20 +297,26 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { * * @param $stackerName Name of the stack * @return $value Value of last added value + * @throws InvalidArgumentException If a parameter is not valid * @throws BadMethodCallException If the stack is empty */ protected function getLastValue (string $stackerName) { - // Is the stack not yet initialized or full? - if ($this->isStackEmpty($stackerName)) { + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } elseif ($this->isStackEmpty($stackerName)) { // Throw an exception throw new BadMethodCallException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY); - } // END - if + } // Now get the last value /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName); $value = NULL; // Return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: value[]=%s - EXIT!', gettype($value))); return $value; } @@ -309,20 +325,26 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { * * @param $stackerName Name of the stack * @return $value Value of last added value + * @throws InvalidArgumentException If a parameter is not valid * @throws BadMethodCallException If the stack is empty */ protected function getFirstValue (string $stackerName) { - // Is the stack not yet initialized or full? - if ($this->isStackEmpty($stackerName)) { + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } elseif ($this->isStackEmpty($stackerName)) { // Throw an exception throw new BadMethodCallException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY); - } // END - if + } // Now get the first value /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName); $value = NULL; // Return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: value[]=%s - EXIT!', gettype($value))); return $value; } @@ -331,14 +353,19 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { * * @param $stackerName Name of the stack * @return $value Value "poped" from array + * @throws InvalidArgumentException If a parameter is not valid * @throws BadMethodCallException If the stack is empty */ protected function popLast (string $stackerName) { - // Is the stack not yet initialized or full? - if ($this->isStackEmpty($stackerName)) { + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } elseif ($this->isStackEmpty($stackerName)) { // Throw an exception throw new BadMethodCallException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY); - } // END - if + } // Now, remove the last entry, we don't care about the return value here, see elseif() block above /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName); @@ -350,14 +377,19 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { * * @param $stackerName Name of the stack * @return $value Value "shifted" from array + * @throws InvalidArgumentException If a parameter is not valid * @throws BadMethodCallException If the named stacker is empty */ protected function popFirst (string $stackerName) { - // Is the stack not yet initialized or full? - if ($this->isStackEmpty($stackerName)) { + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } elseif ($this->isStackEmpty($stackerName)) { // Throw an exception throw new BadMethodCallException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY); - } // END - if + } // Now, remove the last entry, we don't care about the return value here, see elseif() block above /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName); @@ -369,13 +401,22 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { * * @param $stackerName Name of the stack * @return $isFull Whether the stack is full + * @throws InvalidArgumentException If a parameter is not valid */ protected function isStackFull (string $stackerName) { - // File-based stacks will only run full if the disk space is low. + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } + // @TODO Please implement this, returning false + /* NOISY-DEBUG: */ $this->partialStub('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName); $isFull = false; // Return result + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: isFull=%d - EXIT!', intval($isFull))); return $isFull; } @@ -384,13 +425,22 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { * * @param $stackerName Name of the stack * @return $isEmpty Whether the stack is empty + * @throws InvalidArgumentException If a parameter is not valid * @throws BadMethodCallException If given stack is missing */ public function isStackEmpty (string $stackerName) { + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } + // So, is the stack empty? $isEmpty = (($this->getStackCount($stackerName)) == 0); // Return result + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: isEmpty=%d - EXIT!', intval($isEmpty))); return $isEmpty; } @@ -445,7 +495,12 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { */ public function getStackCount (string $stackerName) { // Now, simply return the found count value, this must be up-to-date then! - return $this->getIteratorInstance()->getCounter(); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: stackerName=%s - CALLED!', $stackerName)); + $count = $this->getIteratorInstance()->getCounter(); + + // Return count + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: count=%d - EXIT!', $count)); + return $count; } /** @@ -455,6 +510,7 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { */ public function calculateMinimumBlockLength () { // Calulcate it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: CALLED!'); $length = // Length of entry group BaseBinaryFile::LENGTH_GROUP + strlen(chr(BaseBinaryFile::SEPARATOR_GROUP_HASH)) + @@ -464,6 +520,7 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { strlen(chr(BaseBinaryFile::SEPARATOR_ENTRIES)); // Return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: length=%d - EXIT!', $length)); return $length; } @@ -590,7 +647,12 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { */ public function getFileSize () { // Call iterator's method - return $this->getIteratorInstance()->getFileSize(); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: CALLED!'); + $size = $this->getIteratorInstance()->getFileSize(); + + // Return size + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: size=%d - EXIT!', $size)); + return $size; } /** @@ -617,6 +679,7 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { $gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData)); // Gap position cannot be smaller than header length + 1 + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: gapPosition=%d', $gapPosition)); if ($gapPosition <= $this->getIteratorInstance()->getHeaderSize()) { // Improper gap position throw new UnexpectedValueException(sprintf('gapPosition[%s]=%d is not larger than headerSize=%d', diff --git a/framework/main/classes/stacker/file/fifo/class_FiFoFileStack.php b/framework/main/classes/stacker/file/fifo/class_FiFoFileStack.php index 5db1e2af..4fec8355 100644 --- a/framework/main/classes/stacker/file/fifo/class_FiFoFileStack.php +++ b/framework/main/classes/stacker/file/fifo/class_FiFoFileStack.php @@ -9,6 +9,7 @@ use Org\Mxchange\CoreFramework\Stack\File\BaseFileStack; use Org\Mxchange\CoreFramework\Stack\File\StackableFile; // Import SPL stuff +use \InvalidArgumentException; use \SplFileInfo; /** @@ -50,15 +51,25 @@ class FiFoFileStack extends BaseFileStack implements StackableFile, Calculatable * @param $fileInfoInstance An instance of a SplFileInfo class * @param $type Type of this stack (e.g. url_source for URL sources) * @return $stackInstance An instance of a StackableFile class + * @throws InvalidArgumentException If a parameter is invalid */ - public final static function createFiFoFileStack (SplFileInfo $fileInfoInstance, $type) { + public final static function createFiFoFileStack (SplFileInfo $fileInfoInstance, string $type) { + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: fileInfoInstance[%s]=%s,type=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance, $type)); + if (empty($type)) { + // No empty type + throw new InvalidArgumentException('Parameter "type" is empty'); + } + // Get new instance $stackInstance = new FiFoFileStack(); // Init this stack + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: Calling stackInstance->initFileStack([%s]=%s,%s) ...', get_class($fileInfoInstance), $fileInfoInstance, $type)); $stackInstance->initFileStack($fileInfoInstance, $type); // Return the prepared instance + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackInstance=%s - EXIT!', $stackInstance->__toString())); return $stackInstance; } @@ -68,11 +79,26 @@ class FiFoFileStack extends BaseFileStack implements StackableFile, Calculatable * @param $stackerName Name of the stack * @param $value Value to push on it * @return void + * @throws InvalidArgumentException If a parameter is invalid * @throws StackerFullException If the stack is full */ public function pushNamed (string $stackerName, $value) { + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s,value[]=%s - CALLED!', $stackerName, gettype($value))); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } elseif (is_object($value) || is_resource($value)) { + // Those types for $value are not allowed + throw new InvalidArgumentException(sprintf('value[]=%s is not valid', gettype($value))); + } + // Call the protected method + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: Calling parent::addValue(%s,%s) ...', $stackerName, gettype($value))); parent::addValue($stackerName, $value); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: EXIT!'); } /** @@ -80,17 +106,27 @@ class FiFoFileStack extends BaseFileStack implements StackableFile, Calculatable * * @param $stackerName Name of the stack * @return $value Value of the current stack entry + * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If the named stacker was not found * @throws BadMethodCallException If the named stacker is empty */ public function popNamed (string $stackerName) { + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } + // Get the value + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: this->getNamed(%s) ...', $stackerName)); $value = $this->getNamed($stackerName); // Call the protected method parent::popFirst($stackerName); // Return the value + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: value[]=%s - EXIT!', gettype($value))); return $value; } @@ -99,12 +135,25 @@ class FiFoFileStack extends BaseFileStack implements StackableFile, Calculatable * * @param $stackerName Name of the stack * @return $value Value of last added value + * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If the named stacker was not found * @throws BadMethodCallException If the named stacker is empty */ public function getNamed (string $stackerName) { + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } + // Call the protected method - return parent::getFirstValue($stackerName); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: parent::getFirstValue(%s) ...', $stackerName)); + $value = parent::getFirstValue($stackerName); + + // Return the value + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: value[]=%s - EXIT!', gettype($value))); + return $value; } /** @@ -112,9 +161,21 @@ class FiFoFileStack extends BaseFileStack implements StackableFile, Calculatable * * @param $seekPosition Seek position in file * @return $status Status of this operation + * @throws InvalidArgumentException If a parameter is invalid */ - public function seek ($seekPosition) { + public function seek (int $seekPosition) { + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: seekPosition=%d - CALLED!', $seekPosition)); + if ($seekPosition < 0) { + // Invalid seek position + throw new InvalidArgumentException(sprintf('seekPosition=%d is not valid', $seekPosition)); + } + + // @TODO Unfinished method or invoke inner iterator's method? $this->partialStub('seekPosition=' . $seekPosition); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: EXIT!'); } /** @@ -124,7 +185,12 @@ class FiFoFileStack extends BaseFileStack implements StackableFile, Calculatable */ public function size () { // Call the iterator instance - return $this->getIteratorInstance()->size(); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: CALLED!'); + $size = $this->getIteratorInstance()->size(); + + // Return size + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: size=%d - EXIT!', $size)); + return $size; } } diff --git a/framework/main/classes/stacker/filo/class_FiLoStacker.php b/framework/main/classes/stacker/filo/class_FiLoStacker.php index 78c61013..bc70761f 100644 --- a/framework/main/classes/stacker/filo/class_FiLoStacker.php +++ b/framework/main/classes/stacker/filo/class_FiLoStacker.php @@ -42,12 +42,14 @@ class FiLoStacker extends BaseStacker implements Stackable { */ public static final function createFiLoStacker () { // Get a new instance + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILO-FILE-STACK: CALLED!'); $stackInstance = new FiLoStacker(); // Init the generic stacker $stackInstance->initStack('generic'); // Return the prepared instance + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: stackInstance=%s - EXIT!', $stackInstance->__toString())); return $stackInstance; } @@ -57,11 +59,23 @@ class FiLoStacker extends BaseStacker implements Stackable { * @param $stackerName Name of the stack * @param $value Value to push on it * @return void + * @throws InvalidArgumentException If a parameter is invalid * @throws StackerFullException If the stack is full */ public function pushNamed (string $stackerName, $value) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: stackerName=%s,value[]=%s - CALLED!', $stackerName, gettype($value))); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } + // Call the protected method + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: Calling parent::addValue(%s,%s) ...', $stackerName, gettype($value))); parent::addValue($stackerName, $value); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILO-FILE-STACK: EXIT!'); } /** @@ -69,17 +83,28 @@ class FiLoStacker extends BaseStacker implements Stackable { * * @param $stackerName Name of the stack * @return $value Value of the current stack entry + * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If the named stacker was not found * @throws BadMethodCallException If the named stacker is empty */ public function popNamed (string $stackerName) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } + // Get the value + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: this->getNamed(%s) ...', $stackerName)); $value = $this->getNamed($stackerName); // Call the protected method + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: parent::popLast(%s) ...', $stackerName)); parent::popLast($stackerName); // Return the value + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: value[]=%s - EXIT!', gettype($value))); return $value; } @@ -88,12 +113,25 @@ class FiLoStacker extends BaseStacker implements Stackable { * * @param $stackerName Name of the stack * @return $value Value of last added value + * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If the named stacker was not found * @throws BadMethodCallException If the named stacker is empty */ public function getNamed (string $stackerName) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: stackerName=%s - CALLED!', $stackerName)); + if (empty($stackerName)) { + // No empty stack name + throw new InvalidArgumentException('Parameter "stackerName" is empty'); + } + // Call the protected method - return parent::getLastValue($stackerName); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: parent::getLastValue(%s) ...', $stackerName)); + $value = parent::getLastValue($stackerName); + + // Return the value + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILO-FILE-STACK: value[]=%s - EXIT!', gettype($value))); + return $value; } } -- 2.39.5