From 1bd45d8a8ada928cb18a7a0eed3c6c5f0beebdbe Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 13 Jan 2022 16:11:19 +0900 Subject: [PATCH] phpan/phpstan checks and fixes --- 4dev/checking/phan.sh | 0 4dev/checking/phpstan.sh | 0 4dev/tests/CoreLibsDebugLoggingTest.php | 71 +++++++++++++++++++++- README.md | 13 ++-- www/admin/class_test.json.php | 5 +- www/lib/CoreLibs/Basic.php | 24 ++++---- www/lib/CoreLibs/Combined/ArrayHandler.php | 1 + www/lib/CoreLibs/Create/Uids.php | 2 +- www/lib/CoreLibs/Debug/FileWriter.php | 4 +- 9 files changed, 97 insertions(+), 23 deletions(-) mode change 100644 => 100755 4dev/checking/phan.sh mode change 100644 => 100755 4dev/checking/phpstan.sh diff --git a/4dev/checking/phan.sh b/4dev/checking/phan.sh old mode 100644 new mode 100755 diff --git a/4dev/checking/phpstan.sh b/4dev/checking/phpstan.sh old mode 100644 new mode 100755 diff --git a/4dev/tests/CoreLibsDebugLoggingTest.php b/4dev/tests/CoreLibsDebugLoggingTest.php index 6bb5d1c5..949b5f14 100644 --- a/4dev/tests/CoreLibsDebugLoggingTest.php +++ b/4dev/tests/CoreLibsDebugLoggingTest.php @@ -13,12 +13,81 @@ use PHPUnit\Framework\TestCase; */ final class CoreLibsDebugLoggingTest extends TestCase { + public $log; + + public function optionsProvider(): array + { + return [ + 'log folder set' => [ + [ + 'log_folder' => '/tmp' + ], + [ + 'log_folder' => '/tmp/' + ], + [] + ], + 'nothing set' => [ + null, + [ + 'log_folder' => getcwd() . DIRECTORY_SEPARATOR + ], + [] + ], + 'no options set, constant set' => [ + null, + [ + 'log_folder' => '/tmp/' + ], + [ + 'constant' => [ + 'BASE' => '/tmp', + 'LOG' => '/' + ] + ] + ], + ]; + } + // init tests // - __construct call with options + + /** + * Undocumented function + * + * @dataProvider optionsProvider + * @testdox init test [$_dataName] + * + * @param array|null $options + * @param array $expected + * @param array $override + * @return void + */ + public function testClassInit(?array $options, array $expected, array $override): void + { + if (!empty($override['constant'])) { + foreach ($override['constant'] as $var => $value) { + define($var, $value); + } + } + if ($options === null) { + $this->log = new \CoreLibs\Debug\Logging(); + } else { + $this->log = new \CoreLibs\Debug\Logging($options); + } + // check that settings match + // print "LOG: " . $this->log->getSetting('log_folder') . "\n"; + $this->assertEquals( + $expected['log_folder'], + $this->log->getSetting('log_folder') + ); + } + // setting tests // - basicSetLogId - // - getLogLevelAll + // - getLogId // - setLogLevelAll + // - getLogLevelAll // - debugFor // - setLogLevel // - getLogLevel diff --git a/README.md b/README.md index 961a46c2..56934e00 100644 --- a/README.md +++ b/README.md @@ -30,16 +30,21 @@ The old non namepsace format layout. This is fully deprecated and will no longer be maintaned. last tested PHP 5.6 and PHP 7.0 -### namespace +### development -The new namespace branch. This is the development area for the master branch +Any current development is done here ## Static checks -With phpstan +With phpstan (`4dev/checking/phpstan.sh`) `phpstan` -With phan +With phan (`4dev/checking/phan.sh`) `phan --progress-bar -C --analyze-twice` pslam is setup but not configured + +## Unit tests + +With phpunit (`4dev/checking/phpunit.sh`) +`phpunit -c $phpunit.xml 4dev/tests/` diff --git a/www/admin/class_test.json.php b/www/admin/class_test.json.php index 09cc1e78..15f93e20 100644 --- a/www/admin/class_test.json.php +++ b/www/admin/class_test.json.php @@ -29,12 +29,11 @@ if (!defined('SET_SESSION_NAME')) { $LOG_FILE_ID = 'classTest-json'; ob_end_flush(); -use CoreLibs\Check\Json; -// use CoreLibs\Check\Jason; +use CoreLibs\Convert\Json; use CoreLibs\Debug\Support as DgS; $basic = new CoreLibs\Basic(); -$json_class = 'CoreLibs\Check\Json'; +$json_class = 'CoreLibs\Convert\Json'; // define a list of from to color sets for conversion test diff --git a/www/lib/CoreLibs/Basic.php b/www/lib/CoreLibs/Basic.php index be83677a..54aa4f6e 100644 --- a/www/lib/CoreLibs/Basic.php +++ b/www/lib/CoreLibs/Basic.php @@ -1238,10 +1238,10 @@ class Basic * @param int $green green 0-255 * @param int $blue blue 0-255 * @param bool $hex_prefix default true, prefix with "#" - * @return string rgb in hex values with leading # if set + * @return string|bool rgb in hex values with leading # if set * @deprecated use \CoreLibs\Convert\Colors::rgb2hex() instead */ - public static function rgb2hex(int $red, int $green, int $blue, bool $hex_prefix = true): string + public static function rgb2hex(int $red, int $green, int $blue, bool $hex_prefix = true) { trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Convert\Colors::rgb2hex()', E_USER_DEPRECATED); return \CoreLibs\Convert\Colors::rgb2hex($red, $green, $blue, $hex_prefix); @@ -1252,10 +1252,10 @@ class Basic * @param int $red red 0-255 * @param int $green green 0-255 * @param int $blue blue 0-255 - * @return string hex rgb string + * @return string|bool hex rgb string * @deprecated use rgb2hex instead */ - public static function rgb2html(int $red, int $green, int $blue): string + public static function rgb2html(int $red, int $green, int $blue) { trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Convert\Colors::rgb2hex()', E_USER_DEPRECATED); // check that each color is between 0 and 255 @@ -1269,10 +1269,10 @@ class Basic * @param int $red red 0-255 * @param int $green green 0-255 * @param int $blue blue 0-255 - * @return array Hue, Sat, Brightness/Value + * @return array|bool Hue, Sat, Brightness/Value * @deprecated use \CoreLibs\Convert\Colors::rgb2hsb() instead */ - public static function rgb2hsb(int $red, int $green, int $blue): array + public static function rgb2hsb(int $red, int $green, int $blue) { trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Convert\Colors::rgb2hsb()', E_USER_DEPRECATED); return \CoreLibs\Convert\Colors::rgb2hsb($red, $green, $blue); @@ -1283,10 +1283,10 @@ class Basic * @param int $H hue 0-360 * @param float $S saturation 0-1 (float) * @param float $V brightness/value 0-1 (float) - * @return array 0 red/1 green/2 blue array + * @return array|bool 0 red/1 green/2 blue array * @deprecated use \CoreLibs\Convert\Colors::hsb2rgb() instead */ - public static function hsb2rgb(int $H, float $S, float $V): array + public static function hsb2rgb(int $H, float $S, float $V) { trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Convert\Colors::hsb2rgb()', E_USER_DEPRECATED); return \CoreLibs\Convert\Colors::hsb2rgb($H, (int)round($S * 100), (int)round($V * 100)); @@ -1299,10 +1299,10 @@ class Basic * @param int $r red 0-255 * @param int $g green 0-255 * @param int $b blue 0-255 - * @return array hue/sat/luminance + * @return array|bool hue/sat/luminance * @deprecated use \CoreLibs\Convert\Colors::rgb2hsl() instead */ - public static function rgb2hsl(int $r, int $g, int $b): array + public static function rgb2hsl(int $r, int $g, int $b) { trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Convert\Colors::rgb2hsl()', E_USER_DEPRECATED); return \CoreLibs\Convert\Colors::rgb2hsb($r, $g, $b); @@ -1313,10 +1313,10 @@ class Basic * @param int $h hue: 0-360 (degrees) * @param float $s saturation: 0-1 * @param float $l luminance: 0-1 - * @return array red/blue/green 0-255 each + * @return array|bool red/blue/green 0-255 each * @deprecated use \CoreLibs\Convert\Colors::hsl2rgb() instead */ - public static function hsl2rgb(int $h, float $s, float $l): array + public static function hsl2rgb(int $h, float $s, float $l) { trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Convert\Colors::hsl2rgb()', E_USER_DEPRECATED); return \CoreLibs\Convert\Colors::hsl2rgb($h, $s * 100, $l * 100); diff --git a/www/lib/CoreLibs/Combined/ArrayHandler.php b/www/lib/CoreLibs/Combined/ArrayHandler.php index a970385d..18cb3666 100644 --- a/www/lib/CoreLibs/Combined/ArrayHandler.php +++ b/www/lib/CoreLibs/Combined/ArrayHandler.php @@ -330,6 +330,7 @@ class ArrayHandler /** * will loop through an array recursivly and write the array keys back * @param array $array multidemnsional array to flatten + * @param array $return recoursive pass on array of keys * @return array flattened keys array */ public static function flattenArrayKey(array $array, array $return = []): array diff --git a/www/lib/CoreLibs/Create/Uids.php b/www/lib/CoreLibs/Create/Uids.php index baf023f2..ef3b5a13 100644 --- a/www/lib/CoreLibs/Create/Uids.php +++ b/www/lib/CoreLibs/Create/Uids.php @@ -60,7 +60,7 @@ class Uids // fallback to this hash type $hash = self::FALLBACK_HASH; if ( - defined('DEFAULT_HASH') && !empty(DEFAULT_HASH) && + defined('DEFAULT_HASH') && in_array(DEFAULT_HASH, hash_algos()) ) { $hash = DEFAULT_HASH; diff --git a/www/lib/CoreLibs/Debug/FileWriter.php b/www/lib/CoreLibs/Debug/FileWriter.php index 1436efc5..d091371e 100644 --- a/www/lib/CoreLibs/Debug/FileWriter.php +++ b/www/lib/CoreLibs/Debug/FileWriter.php @@ -13,6 +13,7 @@ class FileWriter { /** @var string */ private static $debug_filename = 'debug_file.log'; // where to write output + /** @var string */ private static $debug_folder; /** @@ -71,8 +72,7 @@ class FileWriter // if empty try to set base log folder if ( empty(self::$debug_folder) && - defined('BASE') && !empty(BASE) && - defined('LOG') && !empty(LOG) + defined('BASE') && defined('LOG') ) { self::$debug_folder = BASE . LOG; }