From 7ebdc93486d595a44ee32f8cf998bd7e989fd9c7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 22 Aug 2025 00:57:29 +0200 Subject: [PATCH] Continued: - added more type-hints - copied all public function to interface ManageableLanguage - introduced new function isDirectoryIteratorInstanceSet() to avoid "abusive" invocations of getter --- .../class_FrameworkDirectoryPointer.php | 32 ++++++++++++------- .../classes/language/class_LanguageSystem.php | 14 ++++---- .../io/directory/class_FrameworkDirectory.php | 23 +++++++++---- .../registry/class_IteratableRegistry.php | 2 +- .../language/class_ManageableLanguage.php | 17 +++++++++- 5 files changed, 62 insertions(+), 26 deletions(-) diff --git a/framework/main/classes/file_directories/directory/class_FrameworkDirectoryPointer.php b/framework/main/classes/file_directories/directory/class_FrameworkDirectoryPointer.php index f080e328..7e628ccf 100644 --- a/framework/main/classes/file_directories/directory/class_FrameworkDirectoryPointer.php +++ b/framework/main/classes/file_directories/directory/class_FrameworkDirectoryPointer.php @@ -14,6 +14,7 @@ use Org\Mxchange\CoreFramework\Deprecated\PathIsNoDirectoryException; // Import SPL stuff use \DirectoryIterator; use \InvalidArgumentException; +use \SplFileInfo; /** * A class for directory reading and getting its contents, no recursion! @@ -62,7 +63,7 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework public function __destruct() { // Is there a resource pointer? Then we have to close the directory here! /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FRAMEWORK-DIRECTORY-POINTER: DESTRUCTED!'); - if ($this->getDirectoryIteratorInstance() instanceof DirectoryIterator) { + if ($this->isDirectoryIteratorInstanceSet()) { // Try to close a directory $this->closeDirectory(); } @@ -85,7 +86,7 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework * @throws PathIsNoDirectoryException If the provided path name is not valid * @throws PathReadProtectedException If the provided path name is read-protected */ - public static final function createFrameworkDirectoryPointer (string $pathName) { + public static final function createFrameworkDirectoryPointer (string $pathName): FrameworkDirectory { // Some pre-sanity checks... /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FRAMEWORK-DIRECTORY-POINTER: pathName=%s - CALLED!', $pathName)); if (empty($pathName)) { @@ -122,7 +123,7 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework * * @return $currentEntry Current entry from encapsulated iterator */ - public function readRawDirectory () { + public function readRawDirectory (): mixed { // Can the next entry be read? /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('DIRECTORY-POINTER: CALLED!'); assert($this->getDirectoryIteratorInstance()->valid()); @@ -139,10 +140,10 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework * Read lines from the current directory pointer except some parts * * @param $except Some parts of a directory we want to ignore. Valid: directory and file names, other values will be silently ignored - * @return SplFileInfo An instance of a SplFileInfo class + * @return SplFileInfo An instance of a SplFileInfo class or NULL when no more entries are left * @throws InvalidArgumentException If a parameter has an invalid value */ - public function readDirectoryExcept (array $except = []) { + public function readDirectoryExcept (array $except = []): ?SplFileInfo { // No exceptions given? /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DIRECTORY-POINTER: except()=%d - CALLED!', count($except))); if (count($except) == 0) { @@ -190,7 +191,7 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework * * @return void */ - public function closeDirectory () { + public function closeDirectory (): void { // Close the directory by unsetting it /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('DIRECTORY-POINTER: CALLED!'); $this->unsetDirectoryIteratorInstance(); @@ -206,7 +207,7 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework * @param $iteratorInstance An instanceof a DirectoryIterator class * @return void */ - protected final function setDirectoryIteratorInstance (DirectoryIterator $iteratorInstance) { + protected final function setDirectoryIteratorInstance (DirectoryIterator $iteratorInstance): void { // Set instance $this->iteratorInstance = $iteratorInstance; } @@ -216,10 +217,19 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework * * @return $iteratorInstance The directory pointer which shall be a valid directory resource */ - public final function getDirectoryIteratorInstance () { + public final function getDirectoryIteratorInstance (): DirectoryIterator { return $this->iteratorInstance; } + /** + * Checks if a DirectoryIterator instance is set + * + * @return<>$isset<>Whether a DirectoryIterator instance is set + */ + public final function isDirectoryIteratorInstanceSet (): bool { + return ($this->iteratorInstance instanceof DirectoryIterator); + } + /** * Remove directory iterator instance (effectively closing it) by setting * it to NULL. This will trigger a call on the destructor which will then @@ -228,7 +238,7 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework * @param $iteratorInstance An instanceof a DirectoryIterator class * @return void */ - protected final function unsetDirectoryIteratorInstance () { + protected final function unsetDirectoryIteratorInstance (): void { // "Unset" the instance $this->iteratorInstance = NULL; } @@ -239,7 +249,7 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework * @param $pathName The new path name * @return void */ - protected final function setPathName (string $pathName) { + protected final function setPathName (string $pathName): void { $this->pathName = $pathName; } @@ -248,7 +258,7 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework * * @return $pathName The current path name */ - public final function getPathName () { + public final function getPathName (): string { return $this->pathName; } diff --git a/framework/main/classes/language/class_LanguageSystem.php b/framework/main/classes/language/class_LanguageSystem.php index 44e8801f..f46e8ed2 100644 --- a/framework/main/classes/language/class_LanguageSystem.php +++ b/framework/main/classes/language/class_LanguageSystem.php @@ -80,7 +80,7 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, * @throws InvalidArgumentException If $languageBasePath is * read-protected */ - public static final function createLanguageSystem (string $languageBasePath = '') { + public static final function createLanguageSystem (string $languageBasePath = ''): ManageableLanguage { // Get a new instance $langInstance = new LanguageSystem(); @@ -130,7 +130,7 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, * * @return $selfInstance An instance of this class */ - public static final function getSelfInstance () { + public static final function getSelfInstance (): ManageableLanguage { return self::$selfInstance; } @@ -140,7 +140,7 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, * @param $languageBasePath The relative base path for all language files * @return void */ - protected final function setLanguageBasePath (string $languageBasePath) { + protected final function setLanguageBasePath (string $languageBasePath): void { // And set it $this->languageBasePath = $languageBasePath; } @@ -151,7 +151,7 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, * @param $langCode The language code for the current application * @return void */ - protected final function setLanguageCode (string $langCode) { + protected final function setLanguageCode (string $langCode): void { // And set it (only 2 chars) $this->langCode = substr($langCode, 0, 2); } @@ -161,7 +161,7 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, * * @return void */ - public function initLanguageStrings () { + public function initLanguageStrings (): void { /* * Locale category constants are usually predefined, but may not be * on some systems such as Win32. @@ -193,7 +193,7 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, * * @return $langCode The language code for the current application */ - public final function getLanguageCode () { + public final function getLanguageCode (): string { return $this->langCode; } @@ -203,7 +203,7 @@ class LanguageSystem extends BaseFrameworkSystem implements ManageableLanguage, * @param $messageId The message id we shall find in the cache variable * @return $messageText The plain message text */ - public function getMessage (string $messageId) { + public function getMessage (string $messageId): string { // Default is missing message text $messageText = sprintf('!%s!', $messageId diff --git a/framework/main/interfaces/io/directory/class_FrameworkDirectory.php b/framework/main/interfaces/io/directory/class_FrameworkDirectory.php index 7c50bea7..8c99c929 100644 --- a/framework/main/interfaces/io/directory/class_FrameworkDirectory.php +++ b/framework/main/interfaces/io/directory/class_FrameworkDirectory.php @@ -5,6 +5,10 @@ namespace Org\Mxchange\CoreFramework\Filesystem; // Import framework stuff use Org\Mxchange\CoreFramework\Generic\FrameworkInterface; +// Import SPL stuff +use \DirectoryIterator; +use \SplFileInfo; + /** * An interface for directorties * @@ -33,15 +37,15 @@ interface FrameworkDirectory extends FrameworkInterface { * * @return $currentEntry Current entry from encapsulated iterator */ - function readRawDirectory (); + function readRawDirectory (): mixed; /** * Read lines from the current directory pointer except some parts * * @param $except Some parts of a directory we want to ignore. Valid: directory and file names, other values will be silently ignored - * @return SplFileInfo An instance of a SplFileInfo class + * @return SplFileInfo An instance of a SplFileInfo class or NULL when no more entries are left */ - function readDirectoryExcept (array $except = []); + function readDirectoryExcept (array $except = []): ?SplFileInfo; /** * Close a directory source and set it's instance to null and the path name @@ -49,20 +53,27 @@ interface FrameworkDirectory extends FrameworkInterface { * * @return void */ - function closeDirectory (); + function closeDirectory (): void; /** * Getter for the directory pointer * * @return $iteratorInstance The directory pointer which shall be a valid directory resource */ - function getDirectoryIteratorInstance (); + function getDirectoryIteratorInstance (): DirectoryIterator; + + /** + * Checks if a DirectoryIterator instance is set + * + * @return $isset Whether a DirectoryIterator instance is set + */ + function isDirectoryIteratorInstanceSet (): bool; /** * Getter for path name * * @return $pathName The current path name */ - function getPathName (); + function getPathName (): string; } diff --git a/framework/main/interfaces/iterator/registry/class_IteratableRegistry.php b/framework/main/interfaces/iterator/registry/class_IteratableRegistry.php index b3155197..4ffd58e9 100644 --- a/framework/main/interfaces/iterator/registry/class_IteratableRegistry.php +++ b/framework/main/interfaces/iterator/registry/class_IteratableRegistry.php @@ -40,6 +40,6 @@ interface IteratableRegistry extends FrameworkInterface, Iterator { * @throws LogicException If a registry entry does not implement Registerable * @throws NullPointerException If criteriaKey or criteriaMethod is not set but a call-back instance is set */ - function initIterator (array $onlyRegistries = []); + function initIterator (array $onlyRegistries = []): void; } diff --git a/framework/main/interfaces/language/class_ManageableLanguage.php b/framework/main/interfaces/language/class_ManageableLanguage.php index 06677c4b..95e9197c 100644 --- a/framework/main/interfaces/language/class_ManageableLanguage.php +++ b/framework/main/interfaces/language/class_ManageableLanguage.php @@ -33,6 +33,21 @@ interface ManageableLanguage extends FrameworkInterface { * * @return void */ - function initLanguageStrings(); + function initLanguageStrings(): void; + + /** + * Getter for language code + * + * @return $langCode The language code for the current application + */ + function getLanguageCode (): string; + + /** + * Get the plain message from the cache variable for the given message id + * + * @param $messageId The message id we shall find in the cache variable + * @return $messageText The plain message text + */ + function getMessage (string $messageId): string; } -- 2.39.5