From: Roland Häder Date: Sun, 20 Dec 2020 10:58:54 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=364ccbef3ec6f786d206c3423b5cf6a724ad732e;p=core.git Continued: - added type-hints for primitive variables - added condition-checks for string/int parameter and when condition is not met an IAE is being thrown Signed-off-by: Roland Häder --- diff --git a/framework/main/classes/class_BaseFrameworkSystem.php b/framework/main/classes/class_BaseFrameworkSystem.php index 6810e724..7c6a73f9 100644 --- a/framework/main/classes/class_BaseFrameworkSystem.php +++ b/framework/main/classes/class_BaseFrameworkSystem.php @@ -618,8 +618,11 @@ Loaded includes: * @deprecated Not fully, as the new Logger facilities are not finished yet. */ public final static function createDebugInstance (string $className, int $lineNumber = NULL) { - // Is the instance set? - if (!GenericRegistry::getRegistry()->instanceExists('debug')) { + // Validate parameter + if (empty($className)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "className" is empty'); + } elseif (!GenericRegistry::getRegistry()->instanceExists('debug')) { // Init debug instance $debugInstance = NULL; diff --git a/framework/main/classes/factories/stacks/class_FileStackFactory.php b/framework/main/classes/factories/stacks/class_FileStackFactory.php index 086dd817..e4e5e53a 100644 --- a/framework/main/classes/factories/stacks/class_FileStackFactory.php +++ b/framework/main/classes/factories/stacks/class_FileStackFactory.php @@ -9,6 +9,7 @@ use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory; use Org\Mxchange\CoreFramework\Registry\GenericRegistry; // Import SPL stuff +use \InvalidArgumentException; use \SplFileInfo; /** @@ -52,6 +53,16 @@ class FileStackFactory extends BaseFactory { * @return $stackInstance An instance of a StackableFile class */ public static final function createFileStackInstance (string $prefix, string $stackName) { + // Validate parameter + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-FACTORY: prefix=%s,stackName=%s - CALLED!', $prefix, $stackName)); + if (empty($prefix)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "prefix" is empty'); + } elseif (empty($stackName)) { + // Throw it again + throw new InvalidArgumentException('Paramter "stackName" is empty'); + } + // Construct file stack name $fileInfoInstance = new SplFileInfo(sprintf('%s%s/%s.%s', FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('root_base_path'), @@ -73,6 +84,7 @@ class FileStackFactory extends BaseFactory { } // Return the instance + //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-FACTORY: stackInstance=%s - EXIT!', $stackInstance->__toString())); return $stackInstance; } diff --git a/framework/main/classes/factories/xml/class_XmlTemplateEngineFactory.php b/framework/main/classes/factories/xml/class_XmlTemplateEngineFactory.php index 039b333a..7aafdffb 100644 --- a/framework/main/classes/factories/xml/class_XmlTemplateEngineFactory.php +++ b/framework/main/classes/factories/xml/class_XmlTemplateEngineFactory.php @@ -7,6 +7,9 @@ use Org\Mxchange\CoreFramework\Factory\BaseFactory; use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory; use Org\Mxchange\CoreFramework\Registry\GenericRegistry; +// Import SPL stuff +use \InvalidArgumentException; + /** * A factory class for XML template engines. All instances generated by this * factory does have language support disabled and XML-compacting enabled (to @@ -51,6 +54,13 @@ class XmlTemplateEngineFactory extends BaseFactory { * @return $templateInstance A template engine instance */ public static final function createXmlTemplateEngineInstance (string $configKey) { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('XML-TEMPLATE-ENGINE-FACTORY: configKey=%s - CALLED!', $configKey)); + if (empty($configKey)) { + // Throw IAE + throw new InvalidArgumentException('Paramter "configKey" is empty'); + } + // Do we have an instance in the registry? if (GenericRegistry::getRegistry()->instanceExists($configKey)) { // Then use this instance @@ -73,6 +83,7 @@ class XmlTemplateEngineFactory extends BaseFactory { } // Return the instance + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('XML-TEMPLATE-ENGINE-FACTORY: templateInstance=%s - EXIT!', $templateInstance->__toString())); return $templateInstance; } diff --git a/framework/main/classes/file_directories/class_BaseFileIo.php b/framework/main/classes/file_directories/class_BaseFileIo.php index 6036db00..5eee4163 100644 --- a/framework/main/classes/file_directories/class_BaseFileIo.php +++ b/framework/main/classes/file_directories/class_BaseFileIo.php @@ -184,14 +184,15 @@ abstract class BaseFileIo extends BaseFrameworkSystem implements FilePointer, Cl $seekStatus = $this->seek(0, SEEK_END); // Get position again (which is the end of the file) - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: seekStatus=%d', $seekStatus)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: seekStatus[%s]=%d', gettype($seekStatus), $seekStatus)); $size = $this->determineSeekPosition(); // Reset seek position to old + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: size[%s]=%d', gettype($size), $size)); $this->seek($seekPosition); // Return size - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: size=%s - EXIT!', $size)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: size=%d - EXIT!', $size)); return $size; } diff --git a/framework/main/classes/helper/class_BaseHelper.php b/framework/main/classes/helper/class_BaseHelper.php index 7590ff4a..b44a0991 100644 --- a/framework/main/classes/helper/class_BaseHelper.php +++ b/framework/main/classes/helper/class_BaseHelper.php @@ -214,7 +214,13 @@ abstract class BaseHelper extends BaseFrameworkSystem { * @throws NullPointerException If recovery of requested value instance failed */ public function prefetchValueInstance (string $registryKey, string $extraKey = NULL) { + // Validate parameter //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('O:'.$registryKey.'/'.$extraKey); + if (empty($registryKey)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "registryKey" is empty'); + } + try { // Get the required instance $this->valueInstance = GenericRegistry::getRegistry()->getInstance($registryKey); @@ -234,13 +240,13 @@ abstract class BaseHelper extends BaseFrameworkSystem { $this->extraInstance = ObjectFactory::createObjectByConfiguredName($extraKey . '_class', array($this->valueInstance)); } //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($extraKey.'='.$this->extraInstance.' - EXTRA!'); - } // END - if + } // Is the value instance valid? if (is_null($this->valueInstance)) { // Get the requested instance $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($this->extraInstance)); - } // END - if + } } /** @@ -255,12 +261,15 @@ abstract class BaseHelper extends BaseFrameworkSystem { * @throws HelperGroupAlreadyCreatedException If the group was already created before */ protected function openGroupByIdContent (string $groupId, string $content, string $tag) { - //* DEBUG: */ echo "OPEN:groupId={$groupId},content=
".htmlentities($content)."
\n"; // Is the group already there? - if (isset($this->groups[$groupId])) { + //* DEBUG: */ echo "OPEN:groupId={$groupId},content=
".htmlentities($content)."
\n"; + if (empty($groupId)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "groupId" is empty'); + } elseif (isset($this->groups[$groupId])) { // Then throw an exception here throw new HelperGroupAlreadyCreatedException(array($this, $groupId), self::EXCEPTION_GROUP_ALREADY_FOUND); - } // END - if + } // Count one up $this->totalCounter++; @@ -294,13 +303,13 @@ abstract class BaseHelper extends BaseFrameworkSystem { if ($this->ifSubGroupOpenedPreviously()) { // Close it automatically $this->closePreviousSubGroupByContent(); - } // END - if + } // Check if any group was opened before if ($this->ifGroupOpenedPreviously() === false) { // Then throw an exception throw new HelperNoPreviousOpenedGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED); - } // END - if + } // Get previous group $groupId = $this->getPreviousGroupId(); @@ -313,7 +322,7 @@ abstract class BaseHelper extends BaseFrameworkSystem { $groupId, $this->groups[$groupId]['tag'] ); - } // END - if + } // Add content to it and mark it as closed $this->groups[$groupId]['content'] .= sprintf( @@ -344,10 +353,13 @@ abstract class BaseHelper extends BaseFrameworkSystem { protected function openSubGroupByIdContent (string $subGroupId, string $content, string $tag) { //* DEBUG: */ echo "OPEN:subGroupId={$subGroupId},content=".htmlentities($content)."
\n"; // Is the group already there? - if (isset($this->subGroups[$subGroupId])) { + if (empty($subGroupId)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "subGroupId" is empty'); + } elseif (isset($this->subGroups[$subGroupId])) { // Then throw an exception here throw new HelperSubGroupAlreadyCreatedException(array($this, $subGroupId), self::EXCEPTION_SUB_GROUP_ALREADY_FOUND); - } // END - if + } // Count one up $this->totalCounter++; @@ -375,7 +387,7 @@ abstract class BaseHelper extends BaseFrameworkSystem { if ($this->ifSubGroupOpenedPreviously() === false) { // Then throw an exception throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED); - } // END - if + } // Get previous sub group $subGroupId = $this->getPreviousSubGroupId(); @@ -384,7 +396,7 @@ abstract class BaseHelper extends BaseFrameworkSystem { if ((empty($content)) && (!empty($this->subGroups[$subGroupId]['tag']))) { // Get it from opener $content = sprintf('', $subGroupId, $this->subGroups[$subGroupId]['tag']); - } // END - if + } // Add content to it and mark it as closed $this->subGroups[$subGroupId]['content'] .= sprintf('%s' . PHP_EOL, $subGroupId, strlen($content), $this->subGroups[$subGroupId]['tag'], $content); @@ -409,7 +421,7 @@ abstract class BaseHelper extends BaseFrameworkSystem { if (isset($this->groups['header'])) { // Then add it $content .= $this->groups['header']['content'] . PHP_EOL; - } // END - if + } // Initiate content $content .= $this->getContent(); @@ -431,13 +443,13 @@ abstract class BaseHelper extends BaseFrameworkSystem { // Something went wrong $this->debugInstance(__METHOD__ . '(): Something unexpected happened here.'); } - } // END - for + } // Is footer content there? if (isset($this->groups['footer'])) { // Then add it $content .= $this->groups['footer']['content'] . PHP_EOL; - } // END - if + } // Return it //* DEBUG: */ echo "content=
".htmlentities($content)."
(".strlen($content).")
\n"; @@ -473,7 +485,7 @@ abstract class BaseHelper extends BaseFrameworkSystem { if (is_null($this->getValueInstance())) { // Throws an exception here throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); - } // END - if + } // Is the field set? if ($this->getValueInstance()->isFieldSet($fieldName)) { diff --git a/framework/main/classes/lists/class_BaseList.php b/framework/main/classes/lists/class_BaseList.php index 5cf4c15c..acad90d5 100644 --- a/framework/main/classes/lists/class_BaseList.php +++ b/framework/main/classes/lists/class_BaseList.php @@ -10,6 +10,7 @@ use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait; use Org\Mxchange\CoreFramework\Visitor\Visitable; // Import SPL stuff +use \InvalidArgumentException; use \IteratorAggregate; use \Countable; @@ -99,7 +100,14 @@ abstract class BaseList extends BaseFrameworkSystem implements IteratorAggregate * @return $isset Whether the group is valid */ public function isGroupSet (string $groupName) { + // Validate parameter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName); + if (empty($groupName)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "groupName" is empty'); + } + + // Check on existence ... return isset($this->listGroups[$groupName]); } @@ -111,9 +119,12 @@ abstract class BaseList extends BaseFrameworkSystem implements IteratorAggregate * @throws ListGroupAlreadyAddedException If the given group is already added */ public function addGroup (string $groupName) { + // Validate parameter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!'); - // Is the group already added? - if ($this->isGroupSet($groupName)) { + if (empty($groupName)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "groupName" is empty'); + } elseif ($this->isGroupSet($groupName)) { // Throw the exception here throw new ListGroupAlreadyAddedException(array($this, $groupName), self::EXCEPTION_GROUP_ALREADY_ADDED); } @@ -133,11 +144,15 @@ abstract class BaseList extends BaseFrameworkSystem implements IteratorAggregate * @throws NoListGroupException If the given group is not found */ public function addInstance (string $groupName, string $subGroup, Visitable $visitableInstance) { - // Debug message + // Validate parameter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName . ',subGroup=' . $subGroup . ',visitableInstance=' . $visitableInstance->__toString() . ' - CALLED!'); - - // Is the group there? - if (!$this->isGroupSet($groupName)) { + if (empty($groupName)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "groupName" is empty'); + } elseif (empty($subGroup)) { + // Throw it again + throw new InvalidArgumentException('Parameter "subGroup" is empty'); + } elseif (!$this->isGroupSet($groupName)) { // Throw the exception here throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND); } @@ -167,35 +182,34 @@ abstract class BaseList extends BaseFrameworkSystem implements IteratorAggregate /** * Gets an array from given list * - * @param $list The requested list + * @param $groupName The requested list * @return $array The requested array * @throws NoListGroupException If the given group is not found */ - public final function getArrayFromList ($list) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ' - CALLED!'); - + public final function getArrayFromList ($groupName) { // Is the group there? - if ((!is_null($list)) && (!$this->isGroupSet($list))) { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName[' . gettype($groupName) . ']=' . $groupName . ' - CALLED!'); + if (empty($groupName)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "groupName" is empty'); + } elseif ((!is_null($groupName)) && (!$this->isGroupSet($groupName))) { // Throw the exception here - throw new NoListGroupException(array($this, $list), self::EXCEPTION_GROUP_NOT_FOUND); + throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND); } // Init array $array = []; // Is there another list? - if (!is_null($list)) { + if (!is_null($groupName)) { // Then get it as well - $array = $this->listGroups[$list]->getArrayFromList(NULL); + $array = $this->listGroups[$groupName]->getArrayFromList(NULL); } // Walk through all entries foreach ($this->listIndex as $hash) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: hash=' . $hash); - // Is the list entry set? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: hash=' . $hash); if ($this->isHashValid($hash)) { // Add it array_push($array, $this->listEntries[$hash]); @@ -203,10 +217,8 @@ abstract class BaseList extends BaseFrameworkSystem implements IteratorAggregate } } - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',list[' . gettype($list) . ']=' . $list . ',[]=' . count($array) . ' - EXIT!'); - // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName[' . gettype($groupName) . ']=' . $groupName . ',[]=' . count($array) . ' - EXIT!'); return $array; } @@ -219,11 +231,12 @@ abstract class BaseList extends BaseFrameworkSystem implements IteratorAggregate * @throws NoListGroupException If the given group is not found */ public function addEntry (string $groupName, $entry) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!'); - // Is the group already added? - if (!$this->isGroupSet($groupName)) { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!'); + if (empty($groupName)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "groupName" is empty'); + } elseif (!$this->isGroupSet($groupName)) { // Throw the exception here throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND); } @@ -256,11 +269,12 @@ abstract class BaseList extends BaseFrameworkSystem implements IteratorAggregate * @throws NoListGroupException If the given group is not found */ public function removeEntry (string $groupName, $entry) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!'); - // Is the group already added? - if (!$this->isGroupSet($groupName)) { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-LIST: this=' . $this->__toString() . ',groupName=' . $groupName . ' - CALLED!'); + if (empty($groupName)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "groupName" is empty'); + } elseif (!$this->isGroupSet($groupName)) { // Throw the exception here throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND); } @@ -351,7 +365,10 @@ abstract class BaseList extends BaseFrameworkSystem implements IteratorAggregate */ protected function clearGroup (string $groupName) { // Does this group exist? - if (!$this->isGroupSet($groupName)) { + if (empty($groupName)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "groupName" is empty'); + } elseif (!$this->isGroupSet($groupName)) { // Throw the exception here throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND); } @@ -379,7 +396,13 @@ abstract class BaseList extends BaseFrameworkSystem implements IteratorAggregate * @param $hash The hash we should validate * @return $isValid Whether the given hash is valid */ - public final function isHashValid ($hash) { + public final function isHashValid (string $hash) { + // Validate parameter + if (empty($hash)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "hash" is empty'); + } + // Check it $isValid = ((in_array($hash, $this->listIndex)) && (isset($this->listEntries[$hash]))); @@ -434,7 +457,10 @@ abstract class BaseList extends BaseFrameworkSystem implements IteratorAggregate */ public function getArrayFromProtocolInstance (string $groupName) { // Is the group valid? - if (!$this->isGroupSet($groupName)) { + if (empty($groupName)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "groupName" is empty'); + } elseif (!$this->isGroupSet($groupName)) { // Throw the exception here throw new NoListGroupException(array($this, $groupName), self::EXCEPTION_GROUP_NOT_FOUND); } @@ -478,9 +504,12 @@ abstract class BaseList extends BaseFrameworkSystem implements IteratorAggregate * @return void * @throws InvalidListHashException If the solved hash index is invalid */ - public function updateCurrentEntryByHash ($hash, array $entryArray) { + public function updateCurrentEntryByHash (string $hash, array $entryArray) { // Is the hash valid? - if (!$this->isHashValid($hash)) { + if (empty($hash)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "hash" is empty'); + } elseif (!$this->isHashValid($hash)) { // Throw an exception here, hashIndex is unknown at this point throw new InvalidListHashException(array($this, $hash, -999), self::EXCEPTION_INVALID_HASH); } diff --git a/framework/main/classes/registry/class_BaseRegistry.php b/framework/main/classes/registry/class_BaseRegistry.php index 8d8a76ab..bb04f70a 100644 --- a/framework/main/classes/registry/class_BaseRegistry.php +++ b/framework/main/classes/registry/class_BaseRegistry.php @@ -9,6 +9,7 @@ use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait; // Import SPL stuff +use \InvalidArgumentExeption; use \IteratorAggregate; /** @@ -105,6 +106,13 @@ abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Reg * @return void */ public function addInstance (string $instanceKey, Registerable $objectInstance) { + // Validate parameter + if (empty($instanceKey)) { + // Throw IAE + throw new InvalidArgumentExeption('Parameter "instanceKey" is empty'); + } + + // Set entry in generic array $this->setGenericArrayKey('registry', 'instance', $instanceKey, $objectInstance); } diff --git a/framework/main/interfaces/lists/class_Listable.php b/framework/main/interfaces/lists/class_Listable.php index 4844d0a4..639686bf 100644 --- a/framework/main/interfaces/lists/class_Listable.php +++ b/framework/main/interfaces/lists/class_Listable.php @@ -78,7 +78,7 @@ interface Listable extends FrameworkInterface, IteratorAggregate { * @return void * @throws InvalidListHashException If the solved hash index is invalid */ - function updateCurrentEntryByHash ($hash, array $entryArray); + function updateCurrentEntryByHash (string $hash, array $entryArray); /** * "Getter" for an iterator instance of this list