From b714de498fe9dd4024ca17d1fa1fa08db9e845bc Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 2 Jun 2022 16:31:35 +0900 Subject: [PATCH] Add print boolean stand allone support function in the Debug\Support add printBool to print out bool as string. Same as printAsString with bool alone but you can control prefix name, and true/false string names Add printArray alias to prAr --- 4dev/tests/CoreLibsDebugSupportTest.php | 106 +++++++++++++++++++++++- www/admin/class_test.debug.php | 4 + www/lib/CoreLibs/Debug/Support.php | 48 ++++++++++- 3 files changed, 151 insertions(+), 7 deletions(-) diff --git a/4dev/tests/CoreLibsDebugSupportTest.php b/4dev/tests/CoreLibsDebugSupportTest.php index 891e9ae6..306741ba 100644 --- a/4dev/tests/CoreLibsDebugSupportTest.php +++ b/4dev/tests/CoreLibsDebugSupportTest.php @@ -45,7 +45,7 @@ final class CoreLibsDebugSupportTest extends TestCase * * @return array */ - public function printArProvider(): array + public function printArrayProvider(): array { return [ 'empty array' => [ @@ -62,6 +62,51 @@ final class CoreLibsDebugSupportTest extends TestCase ]; } + /** + * Undocumented function + * + * @return array + */ + public function printBoolProvider(): array + { + return [ + 'true input default' => [ + 0 => true, + 1 => [], + 2 => 'true' + ], + 'false input default' => [ + 0 => false, + 1 => [], + 2 => 'false' + ], + 'false input param name' => [ + 0 => false, + 1 => [ + 'name' => 'param test' + ], + 2 => 'param test: false' + ], + 'true input param name, true override' => [ + 0 => true, + 1 => [ + 'name' => 'param test', + 'true' => 'ok' + ], + 2 => 'param test: ok' + ], + 'false input param name, true override, false override' => [ + 0 => false, + 1 => [ + 'name' => 'param test', + 'true' => 'ok', + 'false' => 'not', + ], + 2 => 'param test: not' + ], + ]; + } + /** * Undocumented function * @@ -184,8 +229,9 @@ final class CoreLibsDebugSupportTest extends TestCase * Undocumented function * * @cover ::printAr - * @dataProvider printArProvider - * @testdox printAr $input will be $expected [$_dataName] + * @cover ::printArray + * @dataProvider printArrayProvider + * @testdox printAr/printArray $input will be $expected [$_dataName] * * @param array $input * @param string $expected @@ -195,7 +241,59 @@ final class CoreLibsDebugSupportTest extends TestCase { $this->assertEquals( $expected, - \CoreLibs\Debug\Support::printAr($input) + \CoreLibs\Debug\Support::printAr($input), + 'assert printAr' + ); + $this->assertEquals( + $expected, + \CoreLibs\Debug\Support::printArray($input), + 'assert printArray' + ); + } + + /** + * Undocumented function + * + * @cover ::printBool + * @dataProvider printBoolProvider + * @testdox printBool $input will be $expected [$_dataName] + * + * @param bool $input + * @param array $params + * @param string $expected + * @return void + */ + public function testPrintBool(bool $input, array $params, string $expected): void + { + if ( + isset($params['name']) && + isset($params['true']) && + isset($params['false']) + ) { + $string = \CoreLibs\Debug\Support::printBool( + $input, + $params['name'], + $params['true'], + $params['false'] + ); + } elseif (isset($params['name']) && isset($params['true'])) { + $string = \CoreLibs\Debug\Support::printBool( + $input, + $params['name'], + $params['true'] + ); + } elseif (isset($params['name'])) { + $string = \CoreLibs\Debug\Support::printBool( + $input, + $params['name'] + ); + } else { + $string = \CoreLibs\Debug\Support::printBool($input); + } + $this->assertEquals( + $expected, + $string, + 'assert printBool' ); } diff --git a/www/admin/class_test.debug.php b/www/admin/class_test.debug.php index a7834ca2..694a26b1 100644 --- a/www/admin/class_test.debug.php +++ b/www/admin/class_test.debug.php @@ -66,6 +66,10 @@ print "S::GETCALLERMETHOD: " . test() . "
"; print "S::GETCALLERMETHODLIST:
" . print_r(test2(), true) . "

"; print "S::PRINTAR: " . DebugSupport::printAr(['Foo', 'Bar']) . "
"; print "V-S::PRINTAR: " . $debug_support_class::printAr(['Foo', 'Bar']) . "
"; +print "S::PRINTBOOL(default): " . DebugSupport::printBool(true) . "
"; +print "S::PRINTBOOL(name): " . DebugSupport::printBool(true, 'Name') . "
"; +print "S::PRINTBOOL(name, ok): " . DebugSupport::printBool(true, 'Name', 'ok') . "
"; +print "S::PRINTBOOL(name, ok, not): " . DebugSupport::printBool(false, 'Name', 'ok', 'not') . "
"; print "S::DEBUSTRING(s): " . DebugSupport::debugString('SET') . "
"; print "S::DEBUSTRING(''): " . DebugSupport::debugString('') . "
"; print "S::DEBUSTRING(,s): " . DebugSupport::debugString(null, '{-}') . "
"; diff --git a/www/lib/CoreLibs/Debug/Support.php b/www/lib/CoreLibs/Debug/Support.php index 284e01f6..5d2ba60b 100644 --- a/www/lib/CoreLibs/Debug/Support.php +++ b/www/lib/CoreLibs/Debug/Support.php @@ -12,7 +12,9 @@ class Support { /** * wrapper around microtime function to print out y-m-d h:i:s.ms - * @param int $set_microtime -1 to set micro time, 0 for none, positive for rounding + * + * @param int $set_microtime -1 to set micro time, 0 for none, + * positive for rounding * @return string formated datetime string with microtime */ public static function printTime(int $set_microtime = -1): string @@ -31,6 +33,7 @@ class Support /** * prints a html formatted (pre) array + * * @param array $array any array * @param bool $no_html set to true to use ##HTMLPRE## * @return string formatted array for output with
 tag added
@@ -44,12 +47,47 @@ class Support
 		}
 	}
 
+	/**
+	 * alternate name for printAr function
+	 *
+	 * @param  array $array   any array
+	 * @param  bool         $no_html set to true to use ##HTMLPRE##
+	 * @return string                formatted array for output with 
 tag added
+	 */
+	public static function printArray(array $array, bool $no_html = false): string
+	{
+		return self::printAr($array, $no_html);
+	}
+
+	/**
+	 * convert bool value to string
+	 * if $name is set prefix with nae
+	 * default true: true, false: false
+	 *
+	 * @param  bool   $bool  Variable to convert
+	 * @param  string $name  [default: ''] Prefix name
+	 * @param  string $true  [default: true] True string
+	 * @param  string $false [default: false] False string
+	 * @return string        String with converted bool text for debug
+	 */
+	public static function printBool(
+		bool $bool,
+		string $name = '',
+		string $true = 'true',
+		string $false = 'false'
+	): string {
+		$string = (!empty($name) ? '' . $name . ': ' : '')
+			. ($bool ? $true : $false);
+		return $string;
+	}
+
 	/**
 	 * print out any data as string
 	 * will convert boolean to TRUE/FALSE
 	 * if object return get_class
 	 * for array use printAr function, can be controlled with no_html for
 	 * Debug\Logging compatible output
+	 *
 	 * @param  mixed  $mixed
 	 * @param  bool   $no_html set to true to use ##HTMLPRE##
 	 * @return string
@@ -57,7 +95,7 @@ class Support
 	public static function printToString($mixed, bool $no_html = false): string
 	{
 		if (is_bool($mixed)) {
-			return $mixed ? 'TRUE' : 'FALSE';
+			return self::printBool($mixed, '', 'TRUE', 'FALSE');
 		} elseif (is_resource($mixed)) {
 			return (string)$mixed;
 		} elseif (is_object($mixed)) {
@@ -74,9 +112,11 @@ class Support
 	/**
 	 * if there is a need to find out which parent method called a child method,
 	 * eg for debugging, this function does this
+	 *
 	 * call this method in the child method and you get the parent function that called
 	 * @param  int    $level debug level, default 1
-	 * @return ?string       null or the function that called the function where this method is called
+	 * @return ?string       null or the function that called the function
+	 *                       where this method is called
 	 */
 	public static function getCallerMethod(int $level = 1): ?string
 	{
@@ -95,6 +135,7 @@ class Support
 	 * called is last in order
 	 * Will start with start_level to skip unwanted from stack
 	 * Defaults to skip level 0 wich is this methid
+	 *
 	 * @param  integer $start_level From what level on, as defaul starts with 1
 	 *                              to exclude self
 	 * @return array         All method names in list where max is last called
@@ -119,6 +160,7 @@ class Support
 	 * was called
 	 * gets top level class
 	 * loops over the debug backtrace until if finds the first class (from the end)
+	 *
 	 * @return string Class name with namespace
 	 */
 	public static function getCallerClass(): string