From 08af17bce88239268c90ae00331129b438fabdcb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 20 Dec 2020 11:56:00 +0100 Subject: [PATCH] Rewrite: - renamed $configEntry to $configKey - added missing type-hintes for primitive variables - added condition-checks for string/int parameter and thrown an IAE when the condition is not met MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../class_CommunicatorFactory.php | 20 +++++++++++++---- .../factories/dht/class_DhtObjectFactory.php | 11 +++++++++- .../info/class_ConnectionInfoFactory.php | 22 +++++++++---------- .../fragmenter/class_FragmenterFactory.php | 11 +++++++++- .../producer/class_ProducerFactory.php | 2 +- .../class_HubcoinRewardFeature.php | 2 +- .../listener/class_BaseListenerDecorator.php | 2 +- .../hub/classes/miner/class_BaseHubMiner.php | 6 ++--- .../package/deliverable/class_PackageData.php | 2 +- application/hub/classes/source/class_ | 4 +--- .../hub/classes/tools/hub/class_HubTools.php | 2 +- .../tools/node/class_NodeLocatorUtils.php | 4 ++-- .../ids/class_InvalidSessionIdException.php | 2 +- .../package/class_DeliverablePackage.php | 2 +- ...class_DiscoverableUniversalNodeLocator.php | 2 +- .../locator/class_LocateableNode.php | 2 +- .../hub/interfaces/pool/class_Poolable.php | 2 +- .../interfaces/receiver/class_Receivable.php | 2 +- .../resolver/class_ProtocolResolver.php | 2 +- .../resolver/state/class_StateResolver.php | 2 +- 20 files changed, 66 insertions(+), 38 deletions(-) diff --git a/application/hub/classes/factories/communicator/class_CommunicatorFactory.php b/application/hub/classes/factories/communicator/class_CommunicatorFactory.php index c3fd37488..c0276d45c 100644 --- a/application/hub/classes/factories/communicator/class_CommunicatorFactory.php +++ b/application/hub/classes/factories/communicator/class_CommunicatorFactory.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 communicator * @@ -45,18 +48,27 @@ class CommunicatorFactory extends BaseFactory { * be generated and stored in registry, else the communicator from the * registry will be returned. * - * @param $configEntry A configuration entry naming the real class' name - * @parasm $communicatorType Type of the communicator, can currently be 'node' + * @param $configKey A configuration key naming the real class' name + * @parasm $communicatorType Type of the communicator, can currently be 'node' * @return $communicatorInstance An instance of a Communicator class */ - public static final function createCommunicatorInstance ($configEntry, $communicatorType) { + public static final function createCommunicatorInstance (string $configKey, string $communicatorType) { + // Validate parameter + if (empty($configKey)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "configKey" is empty'); + } elseif (empty($communicatorType)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "communicatorType" is empty'); + } + // If there is no communicator? if (GenericRegistry::getRegistry()->instanceExists($communicatorType . '_communicator')) { // Get communicator from registry $communicatorInstance = GenericRegistry::getRegistry()->getInstance($communicatorType . '_communicator'); } else { // Get the communicator instance - $communicatorInstance = ObjectFactory::createObjectByConfiguredName($configEntry); + $communicatorInstance = ObjectFactory::createObjectByConfiguredName($configKey); // Add it to the registry GenericRegistry::getRegistry()->addInstance($communicatorType . '_communicator', $communicatorInstance); diff --git a/application/hub/classes/factories/dht/class_DhtObjectFactory.php b/application/hub/classes/factories/dht/class_DhtObjectFactory.php index e1400ee12..241eaa7c7 100644 --- a/application/hub/classes/factories/dht/class_DhtObjectFactory.php +++ b/application/hub/classes/factories/dht/class_DhtObjectFactory.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 DHT objects * @@ -46,7 +49,13 @@ class DhtObjectFactory extends BaseFactory { * @param $prefix Prefix for DHT class name and registry key * @return $dhtInstance An instance of a DHT object class */ - public static final function createDhtInstance ($prefix) { + public static final function createDhtInstance (string $prefix) { + // Validate parameter + if (empty($prefix)) { + // Throe IAE + throw new InvalidArgumentException('Parameter "prefix" is empty'); + } + // Set instance name $name = $prefix . '_dht'; diff --git a/application/hub/classes/factories/info/class_ConnectionInfoFactory.php b/application/hub/classes/factories/info/class_ConnectionInfoFactory.php index 1a2c7501d..718d23b64 100644 --- a/application/hub/classes/factories/info/class_ConnectionInfoFactory.php +++ b/application/hub/classes/factories/info/class_ConnectionInfoFactory.php @@ -62,30 +62,30 @@ class ConnectionInfoFactory extends BaseFactory { * Returns a singleton (registry-based) ShareableInfo instance * * @param $protocolName Name of protocol (e.g. 'tcp') - * @param $type Connection type ('incoming', 'server', 'helper', ...) + * @param $connectionType Connection type ('incoming', 'server', 'helper', ...) * @return $infoInstance An instance of a ShareableInfo class * @throws InvalidArgumentException If one of the arguments are not valid * @todo Also validate protocol to be sure if there is really a protocol handler for it available */ - public static final function createConnectionInfoInstance (string $protocolName, string $type) { + public static final function createConnectionInfoInstance (string $protocolName, string $connectionType) { // Init factory instance - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO-FACTORY: protocolName=%s,type=%s - CALLED!', $protocolName, $type)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO-FACTORY: protocolName=%s,connectionType=%s - CALLED!', $protocolName, $connectionType)); $factoryInstance = new ConnectionInfoFactory(); // Validate parameter ... if (empty($protocolName)) { // Empty parameter - throw new InvalidArgumentException('Parameter "protocolName" cannot be empty.'); - } elseif (empty($type)) { + throw new InvalidArgumentException('Parameter "protocolName" is empty'); + } elseif (empty($connectionType)) { // Empty parameter - throw new InvalidArgumentException('Parameter "type" cannot be empty.'); - } elseif (!in_array($type, self::$validTypes, TRUE)) { + throw new InvalidArgumentException('Parameter "connectionType" is empty'); + } elseif (!in_array($connectionType, self::$validTypes, TRUE)) { // Not valid type - throw new InvalidArgumentException(sprintf('type=%s is not valid.', $type)); + throw new InvalidArgumentException(sprintf('connectionType=%s is not valid.', $connectionType)); } // Generate key - $key = sprintf('connection_info_%s_%s', $protocolName, $type); + $key = sprintf('connection_info_%s_%s', $protocolName, $connectionType); // If there is no info? /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO-FACTORY: key=%s', $key)); @@ -95,8 +95,8 @@ class ConnectionInfoFactory extends BaseFactory { $infoInstance = GenericRegistry::getRegistry()->getInstance($key); } else { // Get the info instance - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO-FACTORY: Creating info instance for type=%s ...', $type)); - $infoInstance = ObjectFactory::createObjectByConfiguredName('connection_info_class', array($type)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO-FACTORY: Creating info instance for connectionType=%s ...', $connectionType)); + $infoInstance = ObjectFactory::createObjectByConfiguredName('connection_info_class', array($connectionType)); // Add it to the registry /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CONNECTION-INFO-FACTORY: Adding key=%s,infoInstance=%s ...', $key, $infoInstance->__toString())); diff --git a/application/hub/classes/factories/package/fragmenter/class_FragmenterFactory.php b/application/hub/classes/factories/package/fragmenter/class_FragmenterFactory.php index 50b252b2b..c149e39b9 100644 --- a/application/hub/classes/factories/package/fragmenter/class_FragmenterFactory.php +++ b/application/hub/classes/factories/package/fragmenter/class_FragmenterFactory.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 fragmenter * @@ -48,7 +51,13 @@ class FragmenterFactory extends BaseFactory { * @param $fragmenterType The fragmenter's type * @return $fragmenterInstance A fragmenter instance */ - public static final function createFragmenterInstance ($fragmenterType) { + public static final function createFragmenterInstance (string $fragmenterType) { + // Validate parameter + if (empty($fragmenterType)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "fragmenterType" is empty'); + } + // If there is no fragmenter? if (GenericRegistry::getRegistry()->instanceExists($fragmenterType . '_fragmenter')) { // Get fragmenter from registry diff --git a/application/hub/classes/factories/producer/class_ProducerFactory.php b/application/hub/classes/factories/producer/class_ProducerFactory.php index 37037b75f..40319c0af 100644 --- a/application/hub/classes/factories/producer/class_ProducerFactory.php +++ b/application/hub/classes/factories/producer/class_ProducerFactory.php @@ -49,7 +49,7 @@ class ProducerFactory extends BaseFactory { * @parasm $producerType Type of the producer, can be 'key', 'test_unit', etc. * @return $producerInstance A producer instance */ - public static final function createProducerInstance ($configEntry, $producerType) { + public static final function createProducerInstance (string $configEntry, string $producerType) { // If there is no producer? if (GenericRegistry::getRegistry()->instanceExists($producerType . '_producer')) { // Get producer from registry diff --git a/application/hub/classes/feature/hubcoin_reward/class_HubcoinRewardFeature.php b/application/hub/classes/feature/hubcoin_reward/class_HubcoinRewardFeature.php index 4a15cb00d..d24c15ec1 100644 --- a/application/hub/classes/feature/hubcoin_reward/class_HubcoinRewardFeature.php +++ b/application/hub/classes/feature/hubcoin_reward/class_HubcoinRewardFeature.php @@ -102,7 +102,7 @@ class HubcoinRewardFeature extends BaseFeature implements Feature { * @param $hash Previously generated hash for valdiation * @return $isValid Whether the given hash matches a new one from given data */ - public function featureMethodCheckHash ($data, $hash) { + public function featureMethodCheckHash ($data, string $hash) { // Make sure the feature is available //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: data()=%d,hash=%s - CALLED!', __METHOD__, __LINE__, strlen($data), $hash)); assert(FrameworkFeature::isFeatureAvailable('hubcoin_reward')); diff --git a/application/hub/classes/listener/class_BaseListenerDecorator.php b/application/hub/classes/listener/class_BaseListenerDecorator.php index 5f10363d1..4a67a578c 100644 --- a/application/hub/classes/listener/class_BaseListenerDecorator.php +++ b/application/hub/classes/listener/class_BaseListenerDecorator.php @@ -138,7 +138,7 @@ abstract class BaseListenerDecorator extends BaseDecorator implements Visitable * @param $listenerType The listener's type (hub/peer) * @return void */ - protected final function setListenerType ($listenerType) { + protected final function setListenerType (string $listenerType) { $this->listenerType = $listenerType; } diff --git a/application/hub/classes/miner/class_BaseHubMiner.php b/application/hub/classes/miner/class_BaseHubMiner.php index 14fbafbc9..00be586b6 100644 --- a/application/hub/classes/miner/class_BaseHubMiner.php +++ b/application/hub/classes/miner/class_BaseHubMiner.php @@ -118,8 +118,8 @@ abstract class BaseHubMiner extends BaseHubSystem implements Updateable { * @param $version Version number of this miner * @return void */ - protected final function setVersion ($version) { - $this->version = (string) $version; + protected final function setVersion (string $version) { + $this->version = $version; } /** @@ -231,7 +231,7 @@ abstract class BaseHubMiner extends BaseHubSystem implements Updateable { * @throws DatabaseUpdateSupportException If this class does not support database updates * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem */ - public function updateDatabaseField ($fieldName, $fieldValue) { + public function updateDatabaseField (string $fieldName, $fieldValue) { // Unfinished $this->partialStub('Unfinished!'); return; diff --git a/application/hub/classes/package/deliverable/class_PackageData.php b/application/hub/classes/package/deliverable/class_PackageData.php index d6e83316d..d43eec428 100644 --- a/application/hub/classes/package/deliverable/class_PackageData.php +++ b/application/hub/classes/package/deliverable/class_PackageData.php @@ -378,7 +378,7 @@ class PackageData extends BaseHubSystem implements DeliverablePackage, Registera * @param $rawData Raw data * @return void */ - public final function setRawData ($rawData) { + public final function setRawData (string $rawData) { $this->rawData = $rawData; } diff --git a/application/hub/classes/source/class_ b/application/hub/classes/source/class_ index 4e71b5687..8f753dde0 100644 --- a/application/hub/classes/source/class_ +++ b/application/hub/classes/source/class_ @@ -44,7 +44,5 @@ class ???Source extends BaseSource implements Source!!! { // Return the prepared instance return $sourceInstance; } -} -// [EOF] -?> +} diff --git a/application/hub/classes/tools/hub/class_HubTools.php b/application/hub/classes/tools/hub/class_HubTools.php index e0cb5172b..a2fd37eb4 100644 --- a/application/hub/classes/tools/hub/class_HubTools.php +++ b/application/hub/classes/tools/hub/class_HubTools.php @@ -211,7 +211,7 @@ class HubTools extends BaseHubSystem { * @throws InvalidSessionIdException If the provided session id is invalid (and no Universal Node Locator) * @throws NoValidHostnameException If the provided hostname cannot be resolved into an IP address */ - public static function resolveSessionIdToUnl ($address) { + public static function resolveSessionIdToUnl (string $address) { // Get an own instance $selfInstance = self::getSelfInstance(); diff --git a/application/hub/classes/tools/node/class_NodeLocatorUtils.php b/application/hub/classes/tools/node/class_NodeLocatorUtils.php index 296ea93df..567df65e3 100644 --- a/application/hub/classes/tools/node/class_NodeLocatorUtils.php +++ b/application/hub/classes/tools/node/class_NodeLocatorUtils.php @@ -39,7 +39,7 @@ class NodeLocatorUtils extends BaseHubSystem { * * @return void */ - private function __construct () { + protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); } @@ -52,7 +52,7 @@ class NodeLocatorUtils extends BaseHubSystem { * @param $unl Universal Node Locator to validate * @return $isValid Whether the UNL is valid */ - public static function isValidUniversalNodeLocator ($unl) { + public static function isValidUniversalNodeLocator (string $unl) { // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('UNIVERSAL-NODE-LOCATOR: unl=' . $unl . ' - CALLED!'); diff --git a/application/hub/exceptions/ids/class_InvalidSessionIdException.php b/application/hub/exceptions/ids/class_InvalidSessionIdException.php index 7d154da73..9b62f94d2 100644 --- a/application/hub/exceptions/ids/class_InvalidSessionIdException.php +++ b/application/hub/exceptions/ids/class_InvalidSessionIdException.php @@ -35,7 +35,7 @@ class InvalidSessionIdException extends FrameworkException { * @param $code Error code * @return void */ - public function __construct ($sessionId, $code) { + public function __construct (string $sessionId, int $code) { // Construct the message $message = sprintf('Session id %s is invalid.', $sessionId diff --git a/application/hub/interfaces/delivery/package/class_DeliverablePackage.php b/application/hub/interfaces/delivery/package/class_DeliverablePackage.php index d64e69ae3..cb561c203 100644 --- a/application/hub/interfaces/delivery/package/class_DeliverablePackage.php +++ b/application/hub/interfaces/delivery/package/class_DeliverablePackage.php @@ -221,7 +221,7 @@ interface DeliverablePackage extends HubInterface { * @param $rawData Raw data * @return void */ - function setRawData ($rawData); + function setRawData (string $rawData); /** * Getter for error code diff --git a/application/hub/interfaces/discovery/unl/class_DiscoverableUniversalNodeLocator.php b/application/hub/interfaces/discovery/unl/class_DiscoverableUniversalNodeLocator.php index 9dbe8b0ea..7a76c3c00 100644 --- a/application/hub/interfaces/discovery/unl/class_DiscoverableUniversalNodeLocator.php +++ b/application/hub/interfaces/discovery/unl/class_DiscoverableUniversalNodeLocator.php @@ -51,6 +51,6 @@ interface DiscoverableUniversalNodeLocator extends Discoverable { * @param $configKey Key for address to get (valid: internal,external) * @return $unl Universal node locator */ - function discoverUniversalNodeLocatorByConfiguredAddress ($configKey); + function discoverUniversalNodeLocatorByConfiguredAddress (string $configKey); } diff --git a/application/hub/interfaces/locator/class_LocateableNode.php b/application/hub/interfaces/locator/class_LocateableNode.php index bd1b9257d..6ed21b3da 100644 --- a/application/hub/interfaces/locator/class_LocateableNode.php +++ b/application/hub/interfaces/locator/class_LocateableNode.php @@ -89,6 +89,6 @@ interface LocateableNode extends HubInterface { * @param $unl UNL string * @return void */ - function parseStringAsUnl ($unl); + function parseStringAsUnl (string $unl); } diff --git a/application/hub/interfaces/pool/class_Poolable.php b/application/hub/interfaces/pool/class_Poolable.php index 3d23de7bb..ea587b8bb 100644 --- a/application/hub/interfaces/pool/class_Poolable.php +++ b/application/hub/interfaces/pool/class_Poolable.php @@ -60,6 +60,6 @@ interface Poolable extends HubInterface { * @param $poolType Pool type (e.g. 'socket_listen', 'network_listen') * @return $iteratorInstance An instance of a Iterator class */ - function createListIteratorInstance ($poolType); + function createListIteratorInstance (string $poolType); } diff --git a/application/hub/interfaces/receiver/class_Receivable.php b/application/hub/interfaces/receiver/class_Receivable.php index eeb849f4e..b8bb9d387 100644 --- a/application/hub/interfaces/receiver/class_Receivable.php +++ b/application/hub/interfaces/receiver/class_Receivable.php @@ -121,7 +121,7 @@ interface Receivable extends HubInterface { * @param $rawPackageContent The raw package content to be "decoded" * @return $packageInstance An instance of a DeliverablePackage class */ - function decodeRawContent ($rawPackageContent); + function decodeRawContent (string $rawPackageContent); /** * Checks whether the assembler has pending data left diff --git a/application/hub/interfaces/resolver/class_ProtocolResolver.php b/application/hub/interfaces/resolver/class_ProtocolResolver.php index db67741d8..4cc417f1d 100644 --- a/application/hub/interfaces/resolver/class_ProtocolResolver.php +++ b/application/hub/interfaces/resolver/class_ProtocolResolver.php @@ -44,6 +44,6 @@ interface ProtocolResolver extends HubInterface { * @param $configKey Configuration key for UNL address (valid: internal,external) * @return $unl Universal node locator */ - function resolveUniversalNodeLocatorFromConfigKey ($configKey); + function resolveUniversalNodeLocatorFromConfigKey (string $configKey); } diff --git a/application/hub/interfaces/resolver/state/class_StateResolver.php b/application/hub/interfaces/resolver/state/class_StateResolver.php index 9a59ebc58..b6c58e509 100644 --- a/application/hub/interfaces/resolver/state/class_StateResolver.php +++ b/application/hub/interfaces/resolver/state/class_StateResolver.php @@ -35,6 +35,6 @@ interface StateResolver extends Resolver { * @return $isValid Whether the given state is valid * @throws EmptyVariableException Thrown if given state is not set */ - function isStateValid ($stateName); + function isStateValid (string $stateName); } -- 2.39.5