From 1823fd369b57df357efe3056d1f0a1699759b15e Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Mon, 11 May 2026 15:55:33 +0900 Subject: [PATCH] File getMimeType throws exception if no mime type can be set, fixes for phpstan checks --- .phan/config.php | 4 +-- src/Check/File.php | 12 +++++-- src/Combined/DateTime.php | 4 +-- src/Convert/Byte.php | 2 +- src/Convert/Color/Color.php | 12 +++---- src/Convert/Json.php | 6 +++- src/DB/IO.php | 18 +++++++---- src/DB/Options/Convert.php | 2 +- src/Debug/Support.php | 4 +-- src/Logging/Logger/Flag.php | 2 +- src/Output/Form/Elements.php | 10 +++--- .../Convert/CoreLibsConvertJsonTest.php | 32 +++++++++++++++++++ test/phpunit/DB/CoreLibsDBIOTest.php | 1 - test/phpunit/bootstrap.php | 2 +- 14 files changed, 79 insertions(+), 32 deletions(-) diff --git a/.phan/config.php b/.phan/config.php index dc112e6..6174e3f 100644 --- a/.phan/config.php +++ b/.phan/config.php @@ -54,8 +54,8 @@ return [ // Note that the **only** effect of choosing `'5.6'` is to infer that functions removed in php 7.0 exist. // (See `backward_compatibility_checks` for additional options) // Automatically inferred from composer.json requirement for "php" of ">=8.2" - 'target_php_version' => '8.2', - "minimum_target_php_version" => "8.2", + 'target_php_version' => '8.3', + "minimum_target_php_version" => "8.3", // turn color on (-C) "color_issue_messages_if_supported" => true, diff --git a/src/Check/File.php b/src/Check/File.php index c8bdc53..a3f4244 100644 --- a/src/Check/File.php +++ b/src/Check/File.php @@ -58,15 +58,21 @@ class File * else returns '' for any other finfo read problem * * @param string $read_file File to read, relative or absolute path - * @return string + * @return string mime type + * @throws \UnexpectedValueException if file cannot be read or is not a file + * @throws \RangeException if we cannot get a mime type and throw exception is on */ public static function getMimeType(string $read_file): string { $finfo = new \finfo(FILEINFO_MIME_TYPE); - if (!is_file($read_file)) { + if (!is_file($read_file) || !is_readable($read_file)) { throw new \UnexpectedValueException('[getMimeType] File not found: ' . $read_file); } - return $finfo->file($read_file) ?: ''; + $mime_type = $finfo->file($read_file); + if ($mime_type === false) { + throw new \RangeException('[getMimeType] Cannot get mime type for: ' . $read_file); + } + return $mime_type; } } diff --git a/src/Combined/DateTime.php b/src/Combined/DateTime.php index dab0658..18e6619 100644 --- a/src/Combined/DateTime.php +++ b/src/Combined/DateTime.php @@ -133,7 +133,7 @@ class DateTime } else { for ($i = 0, $iMax = count($timegroups); $i < $iMax; $i++) { $output = floor((float)$timestamp / $timegroups[$i]); - $timestamp = (float)$timestamp % $timegroups[$i]; + $timestamp = $timestamp % $timegroups[$i]; // output has days|hours|min|sec if ($output || $time_string) { $time_string .= $output . $labels[$i] . (($i + 1) != count($timegroups) ? ' ' : ''); @@ -453,7 +453,7 @@ class DateTime } if (isset($matches[11]) && is_numeric($matches[11])) { // for milliseconds, we need to divide by 1000 and add them - $timestamp += (float)($matches[11] / 1000); + $timestamp += ((float)$matches[11] / 1000); } if ($negative) { // cast to float so we can do a negative multiplication diff --git a/src/Convert/Byte.php b/src/Convert/Byte.php index a9ddbd9..651d70f 100644 --- a/src/Convert/Byte.php +++ b/src/Convert/Byte.php @@ -154,7 +154,7 @@ class Byte if (!empty($matches[1])) { $number_negative = true; } - if (isset($matches[2]) && isset($matches[3])) { + if (isset($matches[2])) { // remove all non valid characters from the number $number = preg_replace('/[^0-9\.]/', '', $matches[2]); // final clean up and convert to float diff --git a/src/Convert/Color/Color.php b/src/Convert/Color/Color.php index 016d71a..02dca1e 100644 --- a/src/Convert/Color/Color.php +++ b/src/Convert/Color/Color.php @@ -957,9 +957,9 @@ class Color * Undocumented function * * @param HWB $hwb - * @return Lch + * @return LCH */ - public static function hwbToLch(HWB $hwb): Lch + public static function hwbToLch(HWB $hwb): LCH { return self::rgbToLch( self::hwbToRgb($hwb) @@ -1078,10 +1078,10 @@ class Color /** * OkLab to Lch (CIE) * - * @param LAB $lab + * @param Lab $lab * @return LCH */ - public static function okLabToLch(LAB $lab): LCH + public static function okLabToLch(Lab $lab): LCH { return self::labToLch( self::okLabToLab($lab) @@ -1092,9 +1092,9 @@ class Color * Lch (CIE) to OkLab * * @param LCH $lch - * @return LAB + * @return Lab */ - public static function lchToOkLab(LCH $lch): LAB + public static function lchToOkLab(LCH $lch): Lab { return self::labToOkLab( self::lchToLab($lch) diff --git a/src/Convert/Json.php b/src/Convert/Json.php index 32c4236..bddbcae 100644 --- a/src/Convert/Json.php +++ b/src/Convert/Json.php @@ -74,11 +74,15 @@ class Json * Weill set the internval last error state and info can be read with jsonGetLastError * * @param string $json - * @param int $flags only JSON_INVALID_UTF8_IGNORE is currently allowed + * @param int $flags only JSON_INVALID_UTF8_IGNORE is currently allowed * @return bool + * @throws \InvalidArgumentException if flags is invalid */ public static function jsonValidate(string $json, int $flags = 0): bool { + if ($flags != 0 && $flags != JSON_INVALID_UTF8_IGNORE) { + throw new \InvalidArgumentException("For flag only 0 or JSON_INVALID_UTF8_IGNORE is allwed: " . $flags); + } $json_valid = json_validate($json, flags:$flags); self::$json_last_error = json_last_error(); return $json_valid; diff --git a/src/DB/IO.php b/src/DB/IO.php index 732d7a2..12673fc 100644 --- a/src/DB/IO.php +++ b/src/DB/IO.php @@ -2562,21 +2562,27 @@ class IO // set field names $this->cursor_ext[$query_hash]['field_names'] = []; for ($i = 0; $i < $this->cursor_ext[$query_hash]['num_fields']; $i++) { - $this->cursor_ext[$query_hash]['field_names'][] = - $this->db_functions->__dbFieldName( + if ( + ($field_name = $this->db_functions->__dbFieldName( $this->cursor_ext[$query_hash]['cursor'], $i - ); + )) !== false + ) { + $this->cursor_ext[$query_hash]['field_names'][] = $field_name; + } } $this->field_names = $this->cursor_ext[$query_hash]['field_names']; // field types $this->cursor_ext[$query_hash]['field_types'] = []; for ($i = 0; $i < $this->cursor_ext[$query_hash]['num_fields']; $i++) { - $this->cursor_ext[$query_hash]['field_types'][] = - $this->db_functions->__dbFieldType( + if ( + ($field_type = $this->db_functions->__dbFieldType( $this->cursor_ext[$query_hash]['cursor'], $i - ); + )) !== false + ) { + $this->cursor_ext[$query_hash]['field_types'][] = $field_type; + } } $this->field_types = $this->cursor_ext[$query_hash]['field_types']; // combined name => type diff --git a/src/DB/Options/Convert.php b/src/DB/Options/Convert.php index 5e4cefd..c5bd01a 100644 --- a/src/DB/Options/Convert.php +++ b/src/DB/Options/Convert.php @@ -51,7 +51,7 @@ enum Convert: int /** * Get internal name from int value * - * @param int $value + * @param int $value * @return self */ public static function fromValue(int $value): self diff --git a/src/Debug/Support.php b/src/Debug/Support.php index beabd0a..a7d8610 100644 --- a/src/Debug/Support.php +++ b/src/Debug/Support.php @@ -134,8 +134,8 @@ class Support * Convert bool value to string value. Short name alias for printBool * * @param bool $bool Bool value to be transformed - * @param string $true [='true'] Override default string 'true' - * @param string $false [=false'] Override default string 'false' + * @param string $true [=true] Override default string 'true' + * @param string $false [=false] Override default string 'false' * @return string $true or $false string for true/false bool */ public static function prBl( diff --git a/src/Logging/Logger/Flag.php b/src/Logging/Logger/Flag.php index 6fb8fc4..3a3ce97 100644 --- a/src/Logging/Logger/Flag.php +++ b/src/Logging/Logger/Flag.php @@ -63,7 +63,7 @@ enum Flag: int /** * Get internal name from int value * - * @param int $value + * @param value-of $value * @return self */ public static function fromValue(int $value): self diff --git a/src/Output/Form/Elements.php b/src/Output/Form/Elements.php index 6fbd38e..7f25e12 100644 --- a/src/Output/Form/Elements.php +++ b/src/Output/Form/Elements.php @@ -178,11 +178,11 @@ class Elements "/(href=\")?(\>)?\b($protRegex)([\w\.\-?&=+%#~,;\/]+)\b([\.\-?&=+%#~,;\/]*)(\|([^\||^#]+)(#([^\|]+))?\|)?/", function ($matches) { return self::createUrl( - $matches[1] ?? '', - $matches[2] ?? '', - $matches[3] ?? '', - $matches[4] ?? '', - $matches[5] ?? '', + $matches[1], + $matches[2], + $matches[3], + $matches[4], + $matches[5], $matches[7] ?? '', $matches[9] ?? '' ); diff --git a/test/phpunit/Convert/CoreLibsConvertJsonTest.php b/test/phpunit/Convert/CoreLibsConvertJsonTest.php index cdfc74a..0363357 100644 --- a/test/phpunit/Convert/CoreLibsConvertJsonTest.php +++ b/test/phpunit/Convert/CoreLibsConvertJsonTest.php @@ -277,6 +277,38 @@ final class CoreLibsConvertJsonTest extends TestCase ); } + /** + * Check if invalid flag throws exception + * + * @covers ::jsonValidate + * @testdox jsonValidate test exception thrown on invalid flag + * + * @return void + */ + public function testJsonValidateInvalidFlag(): void + { + $this->expectException("\InvalidArgumentException"); + \CoreLibs\Convert\Json::jsonValidate('{"valid": "json"}', 5); + } + + /** + * Check if valid flag throws no exception + * + * @covers ::jsonValidate + * @testdox jsonValidate test no exception on valid flag + * + * @return void + */ + public function testJsonValidateValidflag(): void + { + $this->assertTrue( + \CoreLibs\Convert\Json::jsonValidate('{"valid": "json"}', 0) + ); + $this->assertTrue( + \CoreLibs\Convert\Json::jsonValidate('{"valid": "json"}', JSON_INVALID_UTF8_IGNORE) + ); + } + /** * Undocumented function * diff --git a/test/phpunit/DB/CoreLibsDBIOTest.php b/test/phpunit/DB/CoreLibsDBIOTest.php index f9e66c6..3e00db5 100644 --- a/test/phpunit/DB/CoreLibsDBIOTest.php +++ b/test/phpunit/DB/CoreLibsDBIOTest.php @@ -45,7 +45,6 @@ use CoreLibs\DB\Support\ConvertPlaceholder; * Test class for DB\IO + DB\SQL\PgSQL * This will only test the PgSQL parts * @coversDefaultClass \CoreLibs\DB\IO - * @coversDefaultClass \CoreLibs\DB\SQL\PgSQL * @testdox \CoreLibs\DB\IO method tests for SQL\PgSQL */ final class CoreLibsDBIOTest extends TestCase diff --git a/test/phpunit/bootstrap.php b/test/phpunit/bootstrap.php index ab99d7a..58ea663 100644 --- a/test/phpunit/bootstrap.php +++ b/test/phpunit/bootstrap.php @@ -1,7 +1,7 @@