Exceptions change in Check\Colors, add in Cmbined\ArrayHandler

Chech\Colors now throws correct exceptions for wrong values
Combined\ArrayHandler will throw errors and not return false
This commit is contained in:
Clemens Schwaighofer
2023-08-31 12:07:28 +09:00
parent cd81d15d9a
commit 2fe37bf92a
6 changed files with 42 additions and 39 deletions

View File

@@ -321,7 +321,7 @@ final class CoreLibsCheckColorsTest extends TestCase
*/ */
public function testValidateColorException(int $flag): void public function testValidateColorException(int $flag): void
{ {
$this->expectException(\Exception::class); $this->expectException(\UnexpectedValueException::class);
\CoreLibs\Check\Colors::validateColor('#ffffff', $flag); \CoreLibs\Check\Colors::validateColor('#ffffff', $flag);
} }
} }

View File

@@ -518,17 +518,20 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase
return [ return [
// error <2 arguments // error <2 arguments
'too view arguments' => [ 'too view arguments' => [
'ArgumentCountError',
'arrayMergeRecursive needs two or more array arguments', 'arrayMergeRecursive needs two or more array arguments',
[1] [1]
], ],
// error <2 arrays // error <2 arrays
'only one array' => [ 'only one array' => [
'ArgumentCountError',
'arrayMergeRecursive needs two or more array arguments', 'arrayMergeRecursive needs two or more array arguments',
[1], [1],
true, true,
], ],
// error element is not array // error element is not array
'non array between array' => [ 'non array between array' => [
'TypeError',
'arrayMergeRecursive encountered a non array argument', 'arrayMergeRecursive encountered a non array argument',
[1], [1],
'string', 'string',
@@ -947,18 +950,20 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase
*/ */
public function testArrayMergeRecursiveWarningA(): void public function testArrayMergeRecursiveWarningA(): void
{ {
set_error_handler( // set_error_handler(
static function (int $errno, string $errstr): never { // static function (int $errno, string $errstr): never {
throw new Exception($errstr, $errno); // throw new Exception($errstr, $errno);
}, // },
E_USER_WARNING // E_USER_WARNING
); // );
$arrays = func_get_args(); $arrays = func_get_args();
// first is expected warning // first is expected warning
$exception = array_shift($arrays);
$warning = array_shift($arrays); $warning = array_shift($arrays);
// phpunit 10.0 compatible // phpunit 10.0 compatible
$this->expectException($exception);
$this->expectExceptionMessage($warning); $this->expectExceptionMessage($warning);
\CoreLibs\Combined\ArrayHandler::arrayMergeRecursive(...$arrays); \CoreLibs\Combined\ArrayHandler::arrayMergeRecursive(...$arrays);

View File

@@ -99,7 +99,7 @@ echo "<hr>";
try { try {
$check = Colors::validateColor('#ab12cd', 99); $check = Colors::validateColor('#ab12cd', 99);
print "No Exception"; print "No Exception";
} catch (\Exception $e) { } catch (\UnexpectedValueException $e) {
print "ERROR: " . $e->getCode() . ": " . $e->getMessage() . "<br>"; print "ERROR: " . $e->getCode() . ": " . $e->getMessage() . "<br>";
} }

View File

@@ -41,6 +41,7 @@ class Colors
* @param int|false $rgb_flag flag to check for rgb * @param int|false $rgb_flag flag to check for rgb
* @param int|false $hsl_flag flag to check for hsl type * @param int|false $hsl_flag flag to check for hsl type
* @return bool True if no error, False if error * @return bool True if no error, False if error
* @throws \UnexpectedValueException 1: cannot extract color from string
*/ */
private static function rgbHslContentCheck( private static function rgbHslContentCheck(
string $color, string $color,
@@ -52,7 +53,7 @@ class Colors
if ( if (
!is_array($color_list = preg_split("/,\s*/", $matches[1] ?? '')) !is_array($color_list = preg_split("/,\s*/", $matches[1] ?? ''))
) { ) {
throw new \Exception("Could not extract color list from rgg/hsl", 3); throw new \UnexpectedValueException("Could not extract color list from rgg/hsl", 1);
} }
// based on rgb/hsl settings check that entries are valid // based on rgb/hsl settings check that entries are valid
// rgb: either 0-255 OR 0-100% // rgb: either 0-255 OR 0-100%
@@ -124,7 +125,8 @@ class Colors
* @param int $flags defaults to ALL, else use | to combined from * @param int $flags defaults to ALL, else use | to combined from
* HEX_RGB, HEX_RGBA, RGB, RGBA, HSL, HSLA * HEX_RGB, HEX_RGBA, RGB, RGBA, HSL, HSLA
* @return bool True if valid, False if not * @return bool True if valid, False if not
* @throws Exception 1: no valid flag set * @throws \UnexpectedValueException 1: no valid flag set
* @throws \InvalidArgumentException 2: no regex block set
*/ */
public static function validateColor(string $color, int $flags = self::ALL): bool public static function validateColor(string $color, int $flags = self::ALL): bool
{ {
@@ -152,10 +154,10 @@ class Colors
} }
// wrong flag set // wrong flag set
if ($flags > self::ALL) { if ($flags > self::ALL) {
throw new \Exception("Invalid flags parameter: $flags", 1); throw new \UnexpectedValueException("Invalid flags parameter: $flags", 1);
} }
if (!count($regex_blocks)) { if (!count($regex_blocks)) {
throw new \Exception("No regex blocks set: $flags", 2); throw new \InvalidArgumentException("No regex blocks set: $flags", 2);
} }
// build regex // build regex

View File

@@ -90,27 +90,26 @@ class Encoding
$temp = mb_convert_encoding($string, $to_encoding, $from_encoding); $temp = mb_convert_encoding($string, $to_encoding, $from_encoding);
$compare = mb_convert_encoding($temp, $from_encoding, $to_encoding); $compare = mb_convert_encoding($temp, $from_encoding, $to_encoding);
// if string does not match anymore we have a convert problem // if string does not match anymore we have a convert problem
if ($string != $compare) { if ($string == $compare) {
$failed = [];
// go through each character and find the ones that do not match
for ($i = 0, $iMax = mb_strlen($string, $from_encoding); $i < $iMax; $i++) {
$char = mb_substr($string, $i, 1, $from_encoding);
$r_char = mb_substr($compare, $i, 1, $from_encoding);
// the ord 194 is a hack to fix the IE7/IE8
// bug with line break and illegal character
if (
(($char != $r_char && (!self::$mb_error_char ||
in_array(self::$mb_error_char, ['none', 'long', 'entity']))) ||
($char != $r_char && $r_char == self::$mb_error_char && self::$mb_error_char)) &&
ord($char) != 194
) {
$failed[] = $char;
}
}
return $failed;
} else {
return false; return false;
} }
$failed = [];
// go through each character and find the ones that do not match
for ($i = 0, $iMax = mb_strlen($string, $from_encoding); $i < $iMax; $i++) {
$char = mb_substr($string, $i, 1, $from_encoding);
$r_char = mb_substr($compare, $i, 1, $from_encoding);
// the ord 194 is a hack to fix the IE7/IE8
// bug with line break and illegal character
if (
(($char != $r_char && (!self::$mb_error_char ||
in_array(self::$mb_error_char, ['none', 'long', 'entity']))) ||
($char != $r_char && $r_char == self::$mb_error_char && self::$mb_error_char)) &&
ord($char) != 194
) {
$failed[] = $char;
}
}
return $failed;
} }
} }

View File

@@ -245,14 +245,13 @@ class ArrayHandler
* bool key flag: true: handle keys as string or int * bool key flag: true: handle keys as string or int
* default false: all keys are string * default false: all keys are string
* *
* @return array<mixed>|false merged array * @return array<mixed> merged array
*/ */
public static function arrayMergeRecursive(): array|false public static function arrayMergeRecursive(): array
{ {
// croak on not enough arguemnts (we need at least two) // croak on not enough arguemnts (we need at least two)
if (func_num_args() < 2) { if (func_num_args() < 2) {
trigger_error(__FUNCTION__ . ' needs two or more array arguments', E_USER_WARNING); throw new \ArgumentCountError(__FUNCTION__ . ' needs two or more array arguments');
return false;
} }
// default key is not string // default key is not string
$key_is_string = false; $key_is_string = false;
@@ -265,15 +264,13 @@ class ArrayHandler
} }
// check that arrays count is at least two, else we don't have enough to do anything // check that arrays count is at least two, else we don't have enough to do anything
if (count($arrays) < 2) { if (count($arrays) < 2) {
trigger_error(__FUNCTION__ . ' needs two or more array arguments', E_USER_WARNING); throw new \ArgumentCountError(__FUNCTION__ . ' needs two or more array arguments');
return false;
} }
$merged = []; $merged = [];
while ($arrays) { while ($arrays) {
$array = array_shift($arrays); $array = array_shift($arrays);
if (!is_array($array)) { if (!is_array($array)) {
trigger_error(__FUNCTION__ . ' encountered a non array argument', E_USER_WARNING); throw new \TypeError(__FUNCTION__ . ' encountered a non array argument');
return false;
} }
if (!$array) { if (!$array) {
continue; continue;