diff --git a/phpstan.neon b/phpstan.neon index 20798b5..98b9e02 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -22,6 +22,9 @@ parameters: # - vendor # ignore errores with ignoreErrors: + - + message: '#Expression in empty\(\) is not falsy.#' + path: %currentWorkingDirectory%/src/Language/GetLocale.php #- # this error is ignore because of the PHP 8.0 to 8.1 change for pg_*, only for 8.0 or lower # message: "#^Parameter \\#1 \\$(result|connection) of function pg_\\w+ expects resource(\\|null)?, object\\|resource(\\|bool)? given\\.$#" # path: %currentWorkingDirectory%/www/lib/CoreLibs/DB/SQL/PgSQL.php diff --git a/src/Basic.php b/src/Basic.php index c259bd7..1eb3410 100644 --- a/src/Basic.php +++ b/src/Basic.php @@ -1024,8 +1024,12 @@ class Basic */ public function __sha1Short(string $string, bool $use_sha = false): string { - trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Create\Hash::__sha1Short()', E_USER_DEPRECATED); - return \CoreLibs\Create\Hash::__sha1Short($string, $use_sha); + trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Create\Hash::sha1Short() or ::__crc32b()', E_USER_DEPRECATED); + if ($use_sha) { + return \CoreLibs\Create\Hash::sha1Short($string); + } else { + return \CoreLibs\Create\Hash::__crc32b($string); + } } /** @@ -1040,8 +1044,8 @@ class Basic */ public function __hash(string $string, string $hash_type = 'adler32'): string { - trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Create\Hash::__hash()', E_USER_DEPRECATED); - return \CoreLibs\Create\Hash::__hash($string, $hash_type); + trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Create\Hash::hash()', E_USER_DEPRECATED); + return \CoreLibs\Create\Hash::hash($string, $hash_type); } // *** HASH FUNCTIONS END diff --git a/src/Create/Hash.php b/src/Create/Hash.php index bf83afe..e408f7d 100644 --- a/src/Create/Hash.php +++ b/src/Create/Hash.php @@ -10,9 +10,14 @@ namespace CoreLibs\Create; class Hash { + /** @var string default short hash -> deprecated use STANDARD_HASH_SHORT */ public const DEFAULT_HASH = 'adler32'; + /** @var string default long hash (40 chars) */ public const STANDARD_HASH_LONG = 'ripemd160'; + /** @var string default short hash (8 chars) */ public const STANDARD_HASH_SHORT = 'adler32'; + /** @var string this is the standard hash to use hashStd and hash (64 chars) */ + public const STANDARD_HASH = 'sha256'; /** * checks php version and if >=5.2.7 it will flip the string @@ -20,6 +25,7 @@ class Hash * hash returns false * preg_replace fails for older php version * Use __hash with crc32b or hash('crc32b', ...) for correct output + * For future short hashes use hashShort() instead * * @param string $string string to crc * @return string crc32b hash (old type) @@ -43,19 +49,31 @@ class Hash * replacement for __crc32b call * * @param string $string string to hash - * @param bool $use_sha use sha instead of crc32b (default false) + * @param bool $use_sha use sha1 instead of crc32b (default false) * @return string hash of the string + * @deprecated use __crc32b() for drop in replacement with default, or sha1Short() for use sha true */ public static function __sha1Short(string $string, bool $use_sha = false): string { if ($use_sha) { - // return only the first 9 characters - return substr(hash('sha1', $string), 0, 9); + return self::sha1Short($string); } else { return self::__crc32b($string); } } + /** + * returns a short sha1 + * + * @param string $string string to hash + * @return string hash of the string + */ + public static function sha1Short(string $string): string + { + // return only the first 9 characters + return substr(hash('sha1', $string), 0, 9); + } + /** * replacemend for __crc32b call (alternate) * defaults to adler 32 @@ -65,32 +83,81 @@ class Hash * @param string $string string to hash * @param string $hash_type hash type (default adler32) * @return string hash of the string + * @deprecated use hashShort() of short hashes with adler 32 or hash() for other hash types */ public static function __hash( string $string, - string $hash_type = self::DEFAULT_HASH + string $hash_type = self::STANDARD_HASH_SHORT + ): string { + return self::hash($string, $hash_type); + } + + /** + * creates a hash over string if any valid hash given. + * if no hash type set use sha256 + * + * @param string $string string to ash + * @param string $hash_type hash type (default sha256) + * @return string hash of the string + */ + public static function hash( + string $string, + string $hash_type = self::STANDARD_HASH ): string { - // if not empty, check if in valid list if ( empty($hash_type) || !in_array($hash_type, hash_algos()) ) { // fallback to default hash type if none set or invalid - $hash_type = self::DEFAULT_HASH; + $hash_type = self::STANDARD_HASH; } return hash($hash_type, $string); } /** - * Wrapper function for standard long hashd + * short hash with max length of 8, uses adler32 + * + * @param string $string string to hash + * @return string hash of the string + */ + public static function hashShort(string $string): string + { + return hash(self::STANDARD_HASH_SHORT, $string); + } + + /** + * Wrapper function for standard long hash + * + * @param string $string String to be hashed + * @return string Hashed string + * @deprecated use hashLong() + */ + public static function __hashLong(string $string): string + { + return self::hashLong($string); + } + + /** + * Wrapper function for standard long hash, uses ripmd160 * * @param string $string String to be hashed * @return string Hashed string */ - public static function __hashLong(string $string): string + public static function hashLong(string $string): string { return hash(self::STANDARD_HASH_LONG, $string); } + + /** + * create standard hash basd on STANDAR_HASH, currently sha256 + * + * @param string $string string in + * @return string hash of the string + */ + public static function hashStd(string $string): string + { + return self::hash($string, self::STANDARD_HASH); + } } // __END__ diff --git a/src/DB/IO.php b/src/DB/IO.php index 4720632..a10a089 100644 --- a/src/DB/IO.php +++ b/src/DB/IO.php @@ -4056,7 +4056,7 @@ class IO */ public function dbGetQueryHash(string $query, array $params = []): string { - return Hash::__hashLong( + return Hash::hashLong( $query . ( $params !== [] ? '#' . json_encode($params) : '' diff --git a/test/configs/config.master.php b/test/configs/config.master.php index 2bc5001..a54dbdc 100644 --- a/test/configs/config.master.php +++ b/test/configs/config.master.php @@ -183,8 +183,9 @@ list($HOST_NAME) = array_pad(explode(':', $_SERVER['HTTP_HOST'], 2), 2, null); define('HOST_NAME', $HOST_NAME); // BAIL ON MISSING MASTER SITE CONFIG if (!isset($SITE_CONFIG[HOST_NAME]['location'])) { - echo 'Missing SITE_CONFIG entry for: "' . HOST_NAME . '". Contact Administrator'; - exit; + throw new \InvalidArgumentException( + 'Missing SITE_CONFIG entry for: "' . HOST_NAME . '". Contact Administrator' + ); } // BAIL ON MISSING DB CONFIG: // we have either no db selction for this host but have db config entries @@ -200,8 +201,9 @@ if ( empty($DB_CONFIG[$SITE_CONFIG[HOST_NAME]['db_host']])) ) ) { - echo 'No matching DB config found for: "' . HOST_NAME . '". Contact Administrator'; - exit; + throw new \InvalidArgumentException( + 'No matching DB config found for: "' . HOST_NAME . '". Contact Administrator' + ); } // set SSL on $is_secure = false; diff --git a/test/phpunit/Create/CoreLibsCreateHashTest.php b/test/phpunit/Create/CoreLibsCreateHashTest.php index fcab739..4eb7e69 100644 --- a/test/phpunit/Create/CoreLibsCreateHashTest.php +++ b/test/phpunit/Create/CoreLibsCreateHashTest.php @@ -114,6 +114,22 @@ final class CoreLibsCreateHashTest extends TestCase ]; } + /** + * Undocumented function + * + * @return array + */ + public function hashStandardProvider(): array + { + $hash_source = 'Some String Text'; + return [ + 'Long Hash check: ' . \CoreLibs\Create\Hash::STANDARD_HASH => [ + $hash_source, + hash(\CoreLibs\Create\Hash::STANDARD_HASH, $hash_source) + ], + ]; + } + /** * Undocumented function * @@ -136,9 +152,13 @@ final class CoreLibsCreateHashTest extends TestCase /** * Undocumented function * + * phpcs:disable Generic.Files.LineLength * @covers ::__sha1Short + * @covers ::__crc32b + * @covers ::sha1Short * @dataProvider sha1ShortProvider - * @testdox __sha1Short $input will be $expected (crc32b) and $expected_sha1 (sha1 short) [$_dataName] + * @testdox __sha1Short/__crc32b/sha1short $input will be $expected (crc32b) and $expected_sha1 (sha1 short) [$_dataName] + * phpcs:enable Generic.Files.LineLength * * @param string $input * @param string $expected @@ -149,16 +169,29 @@ final class CoreLibsCreateHashTest extends TestCase // uses crc32b $this->assertEquals( $expected, - \CoreLibs\Create\Hash::__sha1Short($input) + \CoreLibs\Create\Hash::__sha1Short($input), + '__sha1Short depreacted' ); $this->assertEquals( $expected, - \CoreLibs\Create\Hash::__sha1Short($input, false) + \CoreLibs\Create\Hash::__sha1Short($input, false), + '__sha1Short (false) depreacted' + ); + $this->assertEquals( + $expected, + \CoreLibs\Create\Hash::__crc32b($input), + '__crc32b' ); // sha1 type $this->assertEquals( $expected_sha1, - \CoreLibs\Create\Hash::__sha1Short($input, true) + \CoreLibs\Create\Hash::__sha1Short($input, true), + '__sha1Short (true) depreacted' + ); + $this->assertEquals( + $expected_sha1, + \CoreLibs\Create\Hash::sha1Short($input), + 'sha1Short' ); } @@ -166,8 +199,10 @@ final class CoreLibsCreateHashTest extends TestCase * Undocumented function * * @covers ::__hash + * @covers ::hashShort + * @covers ::hashShort * @dataProvider hashProvider - * @testdox __hash $input with $hash_type will be $expected [$_dataName] + * @testdox __hash/hashShort/hash $input with $hash_type will be $expected [$_dataName] * * @param string $input * @param string|null $hash_type @@ -179,12 +214,24 @@ final class CoreLibsCreateHashTest extends TestCase if ($hash_type === null) { $this->assertEquals( $expected, - \CoreLibs\Create\Hash::__hash($input) + \CoreLibs\Create\Hash::__hash($input), + '__hash' + ); + $this->assertEquals( + $expected, + \CoreLibs\Create\Hash::hashShort($input), + 'hashShort' ); } else { $this->assertEquals( $expected, - \CoreLibs\Create\Hash::__hash($input, $hash_type) + \CoreLibs\Create\Hash::__hash($input, $hash_type), + '__hash with hash type' + ); + $this->assertEquals( + $expected, + \CoreLibs\Create\Hash::hash($input, $hash_type), + 'hash with hash type' ); } } @@ -193,8 +240,9 @@ final class CoreLibsCreateHashTest extends TestCase * Undocumented function * * @covers ::__hashLong + * @covers ::hashLong * @dataProvider hashLongProvider - * @testdox __hashLong $input will be $expected [$_dataName] + * @testdox __hashLong/hashLong $input will be $expected [$_dataName] * * @param string $input * @param string $expected @@ -206,6 +254,52 @@ final class CoreLibsCreateHashTest extends TestCase $expected, \CoreLibs\Create\Hash::__hashLong($input) ); + $this->assertEquals( + $expected, + \CoreLibs\Create\Hash::hashLong($input) + ); + } + + /** + * Undocumented function + * + * @covers ::hash + * @covers ::hashStd + * @dataProvider hashStandardProvider + * @testdox hash/hashStd $input will be $expected [$_dataName] + * + * @param string $input + * @param string $expected + * @return void + */ + public function testHashStandard(string $input, string $expected): void + { + $this->assertEquals( + $expected, + \CoreLibs\Create\Hash::hashStd($input) + ); + $this->assertEquals( + $expected, + \CoreLibs\Create\Hash::hash($input) + ); + } + + /** + * Undocumented function + * + * @covers ::hash + * @testdox hash with invalid type [$_dataName] + * + * @return void + */ + public function testInvalidHashType(): void + { + $hash_source = 'Some String Text'; + $expected = hash(\CoreLibs\Create\Hash::STANDARD_HASH, $hash_source); + $this->assertEquals( + $expected, + \CoreLibs\Create\Hash::hash($hash_source, 'DOES_NOT_EXIST') + ); } }