From 5f72bf4f321d989b96a6ef6a3d970bbd5c283a24 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 21 Aug 2025 21:16:10 +0200 Subject: [PATCH] Continued: - introduced `is<$Foo>InstanceSet()` functions instead of testing result from `get<$Foo>Instance()` against NULL or `instanceof Foo` - added a few missing type-hints --- application/tests/class_ApplicationHelper.php | 2 +- .../bootstrap/class_FrameworkBootstrap.php | 11 ++++++- .../application/class_BaseApplication.php | 9 ++++++ .../io/file/handler/class_IoHandler.php | 10 +++--- .../compressor/class_CompressorChannel.php | 22 +++++++------ .../debug/class_DebugMiddleware.php | 32 +++++++++---------- .../middleware/io/class_FileIoHandler.php | 2 +- .../input/class_FileInputStreamerTrait.php | 2 +- .../output/class_FileOutputStreamerTrait.php | 2 +- .../output/class_OutputStreamerTrait.php | 9 ++++++ 10 files changed, 65 insertions(+), 36 deletions(-) diff --git a/application/tests/class_ApplicationHelper.php b/application/tests/class_ApplicationHelper.php index 2a4a1353..c72818ec 100644 --- a/application/tests/class_ApplicationHelper.php +++ b/application/tests/class_ApplicationHelper.php @@ -70,7 +70,7 @@ class ApplicationHelper extends BaseApplication implements ManageableApplication */ public static final function getSelfInstance (): ManageableApplication { // Is the instance there? - if (is_null(self::getApplicationInstance())) { + if (!self::isApplicationInstanceSet()) { // Then set it self::setApplicationInstance(new ApplicationHelper()); } diff --git a/framework/bootstrap/class_FrameworkBootstrap.php b/framework/bootstrap/class_FrameworkBootstrap.php index dfb76d58..d303eccf 100644 --- a/framework/bootstrap/class_FrameworkBootstrap.php +++ b/framework/bootstrap/class_FrameworkBootstrap.php @@ -449,7 +449,7 @@ final class FrameworkBootstrap { // Is the database instance already set? //* NOISY-DEBUG: */ printf('[%s:%d]: self::databaseInstance[]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype(self::getDatabaseInstance())); - if (self::getDatabaseInstance() instanceof DatabaseConnector) { + if (self::isDatabaseInstanceSet()) { // Yes, then abort here throw new BadMethodCallException('Method called twice.'); } @@ -813,6 +813,15 @@ final class FrameworkBootstrap { return self::$databaseInstance; } + /** + * Checks wether a database instance is set + * + * @return $isset Whether a database instance is set + */ + public static function isDatabaseInstanceSet (): bool { + return (self::$databaseInstance instanceof DatabaseConnection); + } + /** * Private getter for language instance * diff --git a/framework/main/classes/application/class_BaseApplication.php b/framework/main/classes/application/class_BaseApplication.php index 21799d5a..0c8643cf 100644 --- a/framework/main/classes/application/class_BaseApplication.php +++ b/framework/main/classes/application/class_BaseApplication.php @@ -85,6 +85,15 @@ abstract class BaseApplication extends BaseFrameworkSystem { self::$applicationInstance = $applicationInstance; } + /** + * Checks if application instance is set + * + * @return $isset Whether application instance is set + */ + public static final function isApplicationInstanceSet (): bool { + return (self::$applicationInstance instanceof ManageableApplication); + } + /** * Setter for controller instance (this surely breaks a bit the MVC patterm) * diff --git a/framework/main/interfaces/io/file/handler/class_IoHandler.php b/framework/main/interfaces/io/file/handler/class_IoHandler.php index 4e3817b3..ceed81e4 100644 --- a/framework/main/interfaces/io/file/handler/class_IoHandler.php +++ b/framework/main/interfaces/io/file/handler/class_IoHandler.php @@ -39,14 +39,14 @@ interface IoHandler extends FileInputStreamer, FileOutputStreamer { * @param $inputStreamerInstance The *real* file-input class * @return void */ - function setInputStreamerInstance (FileInputStreamer $inputStreamerInstance); + function setInputStreamerInstance (FileInputStreamer $inputStreamerInstance): void; /** * Getter for the *real* file input instance * * @return $inputStream The *real* file-input class */ - function getInputStreamerInstance (); + function getInputStreamerInstance (): FileInputStreamer; /** * Setter for the *real* file output instance @@ -54,14 +54,14 @@ interface IoHandler extends FileInputStreamer, FileOutputStreamer { * @param $outputStreamerInstance The *real* file-output class * @return void */ - function setOutputStreamerInstance (FileOutputStreamer $outputStreamerInstance); + function setOutputStreamerInstance (FileOutputStreamer $outputStreamerInstance): void; /** * Getter for the *real* file output instance * * @return $outputStream The *real* file-output class */ - function getOutputStreamerInstance (); + function getOutputStreamerInstance (): FileOutputStreamer; /** * Saves a file with data by using the current output stream @@ -71,6 +71,6 @@ interface IoHandler extends FileInputStreamer, FileOutputStreamer { * @param $objectInstance An instance of a FrameworkInterface class (default: NULL) * @return void */ - function saveStreamToFile (SplFileInfo $infoInstance, string $dataStream, FrameworkInterface $objectInstance = NULL); + function saveStreamToFile (SplFileInfo $infoInstance, string $dataStream, FrameworkInterface $objectInstance = NULL): void; } diff --git a/framework/main/middleware/compressor/class_CompressorChannel.php b/framework/main/middleware/compressor/class_CompressorChannel.php index 34b2ef9b..f66276ef 100644 --- a/framework/main/middleware/compressor/class_CompressorChannel.php +++ b/framework/main/middleware/compressor/class_CompressorChannel.php @@ -57,10 +57,7 @@ class CompressorChannel extends BaseMiddleware implements Registerable { $compressorInstance = new CompressorChannel(); // Is the compressor handler set? - if ( - (is_null($compressorInstance->getCompressor())) - || (!$compressorInstance->getCompressor() instanceof Compressor) - ) { + if (!$compressorInstance->isCompressorSet()) { // Init base directory $baseDir = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('framework_base_path') . @@ -111,14 +108,10 @@ class CompressorChannel extends BaseMiddleware implements Registerable { } // Check again if there is a compressor - if ( - (is_null($compressorInstance->getCompressor())) - || (!is_object($compressorInstance->getCompressor())) - || (!$compressorInstance instanceof Compressor) - ) { + if (!$compressorInstance->isCompressorSet()) { // Set the null compressor handler. This should not be configureable! // @TODO Is there a configurable fall-back compressor needed, or is NullCompressor okay? - $compressorInstance->setCompressor(ObjectFactory::createObjectByName('Org\Mxchange\CoreFramework\Compressor\Null\NullCompressor')); + $compressorInstance->setCompressor(ObjectFactory::createObjectByName(Org\Mxchange\CoreFramework\Compressor\Null\NullCompressor::class)); } // Return the compressor instance @@ -144,6 +137,15 @@ class CompressorChannel extends BaseMiddleware implements Registerable { $this->compressor = $compressorInstance; } + /** + * Checks wether a compressor instance is set + * + * @return $isset Whether a compressor instance is set + */ + public final function isCompressorSet (): bool { + return ($this->compressor instanceof Compressor); + } + /** * Getter for the file extension of the current compressor */ diff --git a/framework/main/middleware/debug/class_DebugMiddleware.php b/framework/main/middleware/debug/class_DebugMiddleware.php index d5ec854d..84183bc8 100644 --- a/framework/main/middleware/debug/class_DebugMiddleware.php +++ b/framework/main/middleware/debug/class_DebugMiddleware.php @@ -71,7 +71,7 @@ class DebugMiddleware extends BaseMiddleware implements Registerable, Logger { * @return $debugInstance An instance of this middleware class * @throws InvalidArgumentException If a parameter has an invalid value */ - public static final function createDebugMiddleware (string $outputClass, string $className) { + public static final function createDebugMiddleware (string $outputClass, string $className): Logger { // Check parameter //* NOISY-DEBUG: */ printf('[%s:%d]: outputClass=%s,className=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $outputClass, $className); if (empty($outputClass)) { @@ -91,7 +91,7 @@ class DebugMiddleware extends BaseMiddleware implements Registerable, Logger { // Is there a valid output instance provided? //* NOISY-DEBUG: */ printf('[%s:%d]: outputClass=%s' . PHP_EOL, __METHOD__, __LINE__, $outputClass); - if (class_exists($outputClass) && is_null(self::$selfInstance->getOutputInstance())) { + if (class_exists($outputClass) && !self::$selfInstance->isOutputInstanceSet()) { // A name for a debug output class has been provided so we try to get it //* NOISY-DEBUG: */ printf('[%s:%d]: Initializing outputClass=%s ...' . PHP_EOL, __METHOD__, __LINE__, $outputClass); $outputInstance = ObjectFactory::createObjectByName($outputClass); @@ -102,7 +102,7 @@ class DebugMiddleware extends BaseMiddleware implements Registerable, Logger { } // Is the output class loadable and an output instance is set? - if (class_exists($outputClass) && !is_null(self::$selfInstance->getOutputInstance())) { + if (class_exists($outputClass) && self::$selfInstance->isOutputInstanceSet()) { // Then set class name //* NOISY-DEBUG: */ printf('[%s:%d]: Setting className=%s as logger class ...' . PHP_EOL, __METHOD__, __LINE__, $className); self::$selfInstance->getOutputInstance()->setLoggerClassName($className); @@ -123,7 +123,7 @@ class DebugMiddleware extends BaseMiddleware implements Registerable, Logger { * @return void * @throws NullPointerException If this->outputInstance is NULL */ - private function outputMessage (string $logLevel, string $message, bool $stripTags = false) { + private function outputMessage (string $logLevel, string $message, bool $stripTags = false): void { // Get backtrace //* NOISY-DEBUG: */ printf('[%s:%d]: logLevel=%s,message=%s,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $logLevel, $message, intval($stripTags)); $backtrace = debug_backtrace(!DEBUG_BACKTRACE_PROVIDE_OBJECT); @@ -173,7 +173,7 @@ class DebugMiddleware extends BaseMiddleware implements Registerable, Logger { * * @return $selfInstance An instance of this class */ - public static final function getSelfInstance() { + public static final function getSelfInstance(): Logger { return self::$selfInstance; } @@ -190,13 +190,13 @@ class DebugMiddleware extends BaseMiddleware implements Registerable, Logger { * @throws NullPointerException If this->outputInstance is NULL * @todo Remove $doPrint parameter */ - public function traceMessage (string $message, bool $doPrint = true, bool $stripTags = false) { + public function traceMessage (string $message, bool $doPrint = true, bool $stripTags = false): void { // Check parameter //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags)); if (empty($message)) { // Throw IAE throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); - } elseif (is_null($this->getOutputInstance())) { + } elseif (!$this->isOutputInstanceSet()) { // Should not be NULL throw new NullPointerException($this, FrameworkInterface::EXCEPTION_IS_NULL_POINTER); } @@ -222,13 +222,13 @@ class DebugMiddleware extends BaseMiddleware implements Registerable, Logger { * @throws NullPointerException If this->outputInstance is NULL * @todo Remove $doPrint parameter */ - public function debugMessage (string $message, bool $doPrint = true, bool $stripTags = false) { + public function debugMessage (string $message, bool $doPrint = true, bool $stripTags = false): void { // Check parameter //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags)); if (empty($message)) { // Throw IAE throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); - } elseif (is_null($this->getOutputInstance())) { + } elseif (!$this->isOutputInstanceSet()) { // Should not be NULL throw new NullPointerException($this, FrameworkInterface::EXCEPTION_IS_NULL_POINTER); } @@ -254,13 +254,13 @@ class DebugMiddleware extends BaseMiddleware implements Registerable, Logger { * @throws NullPointerException If this->outputInstance is NULL * @todo Remove $doPrint parameter */ - public function infoMessage (string $message, bool $doPrint = true, bool $stripTags = false) { + public function infoMessage (string $message, bool $doPrint = true, bool $stripTags = false): void { // Check parameter //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags)); if (empty($message)) { // Throw IAE throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); - } elseif (is_null($this->getOutputInstance())) { + } elseif (!$this->isOutputInstanceSet()) { // Should not be NULL throw new NullPointerException($this, FrameworkInterface::EXCEPTION_IS_NULL_POINTER); } @@ -286,13 +286,13 @@ class DebugMiddleware extends BaseMiddleware implements Registerable, Logger { * @throws NullPointerException If this->outputInstance is NULL * @todo Remove $doPrint parameter */ - public function warningMessage (string $message, bool $doPrint = true, bool $stripTags = false) { + public function warningMessage (string $message, bool $doPrint = true, bool $stripTags = false): void { // Check parameter //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags)); if (empty($message)) { // Throw IAE throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); - } elseif (is_null($this->getOutputInstance())) { + } elseif (!$this->isOutputInstanceSet()) { // Should not be NULL throw new NullPointerException($this, FrameworkInterface::EXCEPTION_IS_NULL_POINTER); } @@ -311,7 +311,7 @@ class DebugMiddleware extends BaseMiddleware implements Registerable, Logger { * @param $message An optional message to display * @return void */ - public function partialStub (string $message = '') { + public function partialStub (string $message = ''): void { // Init variable //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message); $stubMessage = 'Partial stub!'; @@ -344,13 +344,13 @@ class DebugMiddleware extends BaseMiddleware implements Registerable, Logger { * @todo Remove $doPrint parameter * @todo When all old method invocations are fixed, renamed this do deprecatedMessage */ - public function debugOutput (string $message, bool $doPrint = true, bool $stripTags = false) { + public function debugOutput (string $message, bool $doPrint = true, bool $stripTags = false): void { // Check parameter //* NOISY-DEBUG: */ printf('[%s:%d]: message=%s,doPrint=%d,stripTags=%d - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $message, intval($doPrint), intval($stripTags)); if (empty($message)) { // Throw IAE throw new InvalidArgumentException('Parameter "message" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); - } elseif (is_null($this->getOutputInstance())) { + } elseif (!$this->isOutputInstanceSet()) { // Should not be NULL throw new NullPointerException($this, FrameworkInterface::EXCEPTION_IS_NULL_POINTER); } diff --git a/framework/main/middleware/io/class_FileIoHandler.php b/framework/main/middleware/io/class_FileIoHandler.php index e5f3c90f..723308a5 100644 --- a/framework/main/middleware/io/class_FileIoHandler.php +++ b/framework/main/middleware/io/class_FileIoHandler.php @@ -121,7 +121,7 @@ class FileIoHandler extends BaseMiddleware implements IoHandler { * @return void * @throws InvalidArgumentException If a parameter has an invalid value */ - public function saveStreamToFile (SplFileInfo $infoInstance, string $dataStream, FrameworkInterface $objectInstance = NULL) { + public function saveStreamToFile (SplFileInfo $infoInstance, string $dataStream, FrameworkInterface $objectInstance = NULL): void { // Check parameters /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-IO-HANDLER: infoInstance=%s,dataStream()=%d,objectInstance[]=%s - CALLED!', $infoInstance->__toString(), strlen($dataStream), gettype($objectInstance))); if (empty($dataStream)) { diff --git a/framework/main/traits/streamer/file/input/class_FileInputStreamerTrait.php b/framework/main/traits/streamer/file/input/class_FileInputStreamerTrait.php index a90c863e..5b4467bd 100644 --- a/framework/main/traits/streamer/file/input/class_FileInputStreamerTrait.php +++ b/framework/main/traits/streamer/file/input/class_FileInputStreamerTrait.php @@ -39,7 +39,7 @@ trait FileInputStreamerTrait { * @param $inputStreamerInstance The *real* file-input class * @return void */ - public final function setInputStreamerInstance (FileInputStreamer $inputStreamerInstance) { + public final function setInputStreamerInstance (FileInputStreamer $inputStreamerInstance): void { $this->inputStreamerInstance = $inputStreamerInstance; } diff --git a/framework/main/traits/streamer/file/output/class_FileOutputStreamerTrait.php b/framework/main/traits/streamer/file/output/class_FileOutputStreamerTrait.php index ac58b7a9..ef37efb4 100644 --- a/framework/main/traits/streamer/file/output/class_FileOutputStreamerTrait.php +++ b/framework/main/traits/streamer/file/output/class_FileOutputStreamerTrait.php @@ -39,7 +39,7 @@ trait FileOutputStreamerTrait { * @param $outputStreamerInstance The *real* file-output class * @return void */ - public final function setOutputStreamerInstance (FileOutputStreamer $outputStreamerInstance) { + public final function setOutputStreamerInstance (FileOutputStreamer $outputStreamerInstance): void { $this->outputStreamerInstance = $outputStreamerInstance; } diff --git a/framework/main/traits/streamer/output/class_OutputStreamerTrait.php b/framework/main/traits/streamer/output/class_OutputStreamerTrait.php index f875e4fd..decccac7 100644 --- a/framework/main/traits/streamer/output/class_OutputStreamerTrait.php +++ b/framework/main/traits/streamer/output/class_OutputStreamerTrait.php @@ -52,4 +52,13 @@ trait OutputStreamerTrait { return $this->outputInstance; } + /** + * Checks if this output instance is set + * + * @return $isset Whether this output instance is set + */ + protected final function isOutputInstanceSet (): bool { + return ($this->outputInstance instanceof OutputStreamer); + } + } -- 2.39.5