From 87dfd6c713ecc5119f20f20ced6fc158d6b14a6c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 24 Aug 2025 11:42:33 +0200 Subject: [PATCH] Continued: - moved getPrintableState() to own StateUtils class - renamed method to generatePrintableState() --- .../classes/class_BaseFrameworkSystem.php | 62 ++------------ .../classes/utils/states/class_StateUtils.php | 82 +++++++++++++++++++ 2 files changed, 88 insertions(+), 56 deletions(-) create mode 100644 framework/main/classes/utils/states/class_StateUtils.php diff --git a/framework/main/classes/class_BaseFrameworkSystem.php b/framework/main/classes/class_BaseFrameworkSystem.php index f14443e6..c00b40df 100644 --- a/framework/main/classes/class_BaseFrameworkSystem.php +++ b/framework/main/classes/class_BaseFrameworkSystem.php @@ -18,7 +18,6 @@ use Org\Mxchange\CoreFramework\Manager\ManageableApplication; use Org\Mxchange\CoreFramework\Middleware\Debug\DebugMiddleware; use Org\Mxchange\CoreFramework\Registry\Object\ObjectRegistry; use Org\Mxchange\CoreFramework\Result\Database\CachedDatabaseResult; -use Org\Mxchange\CoreFramework\State\Stateable; use Org\Mxchange\CoreFramework\Stream\Output\OutputStreamer; use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils; @@ -225,7 +224,7 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac public static final function __callStatic (string $methodName, array $args = NULL): void { // Init argument string and class name //* PRINT-DEBUG: */ printf('[%s:%d]: methodName=%s,args[]=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $methodName, gettype($args)); - $argsString = ''; + $argsString = 'NULLL'; $className = 'unknown'; // Is self-instance set? @@ -237,11 +236,8 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac throw new InvalidArgumentException(sprintf('self::instance[%s] is not expected.', gettype(self::$selfInstance)), self::EXCEPTION_SELF_INSTANCE); } - // Is it NULL, empty or an array? - if (is_null($args)) { - // No arguments - $argsString = 'NULL'; - } elseif (is_array($args)) { + // Are arguments being set? + if (is_array($args)) { // Start braces $argsString = '('; @@ -268,12 +264,9 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac // Is an other object, maybe no __toString() available $argsString .= $reflection->getName(); - } elseif ($arg === true) { - // ... is boolean 'true' - $argsString .= 'true'; - } elseif ($arg === false) { - // ... is boolean 'false' - $argsString .= 'false'; + } elseif (is_bool($arg)) { + // ... is boolean + $argsString .= $arg === true ? 'true' : 'false'; } // Comma for next one @@ -297,9 +290,6 @@ abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterfac $methodName, $argsString )); - - // Return nothing - return; } /** @@ -1857,46 +1847,6 @@ Loaded includes: // Return it //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-FRAMEWORK-SYSTEM: fileInfoInstance=%s - EXIT!', $fileInfoInstance->__toString())); return $fileInfoInstance; - } - - /** - * "Getter" for a printable state name - * - * @return $stateName Name of the node's state in a printable format - * @throws BadMethodCallException If this instance doesn't have a callable getter for stateInstance - * @deprecated Monolithic method, should be moved to proper classes - */ - public final function getPrintableState (): string { - // Check if getter is there - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-FRAMEWORK-SYSTEM: CALLED!'); - if (!is_callable($this, 'getStateInstance')) { - // Throw BMCE - throw new BadMethodCallException(sprintf('this=%s has no callable getter for stateInstance', $this->__toString()), FrameworkInterface::EXCEPTION_BAD_METHOD_CALL); - } - - // This method is deprecated - $this->deprecationWarning('Monolithic method, should be moved to proper classes'); - - // Default is 'null' - $stateInstance = NULL; - $stateName = 'null'; - - // Check if state instance is there - if ($this->isStateInstanceSet()) { - // Get the state instance - $stateInstance = $this->getStateInstance(); - } - - // Is it an instance of Stateable? - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-FRAMEWORK-SYSTEM: stateInstance[]=%s', gettype($stateInstance))); - if ($stateInstance instanceof Stateable) { - // Then use that state name - $stateName = $stateInstance->getStateName(); - } - - // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-FRAMEWORK-SYSTEM: stateName=%s - EXIT!', $stateName)); - return $stateName; } } diff --git a/framework/main/classes/utils/states/class_StateUtils.php b/framework/main/classes/utils/states/class_StateUtils.php new file mode 100644 index 00000000..97c967fe --- /dev/null +++ b/framework/main/classes/utils/states/class_StateUtils.php @@ -0,0 +1,82 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class StateUtils extends BaseFrameworkSystem { + /** + * Utilities classes don't have instances of themselves somewhere + * + * @return void + */ + private function __construct () { + // No objects of utilities classes + } + + /** + * Generates a printable (string) name for given instance's state instance + * + * @param $instance An instance of a FrameworkInterface class + * @return $stateName Name of the node's state in a printable format + * @throws BadMethodCallException If this instance doesn't have a callable getter for stateInstance + */ + public static final function generatePrintableState (FrameworkInterface $instance): string { + // Check if getter is there + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-FRAMEWORK-SYSTEM: CALLED!'); + if (!is_callable($instance, 'getStateInstance')) { + // Throw BMCE + throw new BadMethodCallException(sprintf('instance=%s has no callable getter for stateInstance', $instance->__toString()), FrameworkInterface::EXCEPTION_BAD_METHOD_CALL); + } + + // Default is 'null' + $stateInstance = NULL; + $stateName = 'null'; + + // Check if state instance is there + if ($instance->isStateInstanceSet()) { + // Get the state instance + $stateInstance = $instance->getStateInstance(); + } + + // Is it an instance of Stateable? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-FRAMEWORK-SYSTEM: stateInstance[]=%s', gettype($stateInstance))); + if ($stateInstance instanceof Stateable) { + // Then use that state name + $stateName = $stateInstance->getStateName(); + } + + // Return result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-FRAMEWORK-SYSTEM: stateName=%s - EXIT!', $stateName)); + return $stateName; + } + +} -- 2.39.5