Use more explicit type hints for improved static code analysis

This commit is contained in:
Joshua Ramon Enslin 2024-09-22 16:51:54 +02:00
parent bce4268a70
commit fa2985463f
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
2 changed files with 32 additions and 22 deletions

View File

@ -116,7 +116,7 @@ final class MD_STD_IN_Test extends TestCase {
/** /**
* Data provider for invalid latitudes. * Data provider for invalid latitudes.
* *
* @return array<array{0: mixed, 1: string}> * @return array<array{0: mixed, 1: class-string<Throwable>}>
*/ */
public static function invalid_latitude_provider():array { public static function invalid_latitude_provider():array {
@ -589,7 +589,7 @@ final class MD_STD_IN_Test extends TestCase {
* Function for testing validate_longitude(). * Function for testing validate_longitude().
* *
* @param mixed $to_validate Input to validate. * @param mixed $to_validate Input to validate.
* @param string $exceptionClass Exception class. * @param class-string<Throwable> $exceptionClass Exception class.
* *
* @return void * @return void
*/ */
@ -620,7 +620,7 @@ final class MD_STD_IN_Test extends TestCase {
* Function for testing validate_latitude(). * Function for testing validate_latitude().
* *
* @param mixed $to_validate Input to validate. * @param mixed $to_validate Input to validate.
* @param string $exceptionClass Exception class. * @param class-string<Throwable> $exceptionClass Exception class.
* *
* @return void * @return void
*/ */
@ -677,9 +677,16 @@ final class MD_STD_IN_Test extends TestCase {
*/ */
public function test_ensureStringIsUtf8():void { public function test_ensureStringIsUtf8():void {
if (empty($convToIso8859 = iconv("UTF-8", 'ISO-8859-1//TRANSLIT', "ä"))) {
throw new Exception("Iconv returned empty result");
}
if (empty($convToIso2022 = iconv("UTF-8", 'ISO-2022-JP//TRANSLIT', "ä"))) {
throw new Exception("Iconv returned empty result");
}
self::assertEquals("ä", MD_STD_IN::ensureStringIsUtf8("ä")); self::assertEquals("ä", MD_STD_IN::ensureStringIsUtf8("ä"));
self::assertEquals("ä", MD_STD_IN::ensureStringIsUtf8(iconv("UTF-8", 'ISO-8859-1//TRANSLIT', "ä"))); self::assertEquals("ä", MD_STD_IN::ensureStringIsUtf8($convToIso8859));
self::assertEquals("a", MD_STD_IN::ensureStringIsUtf8(iconv("UTF-8", 'ISO-2022-JP//TRANSLIT', "ä"))); self::assertEquals("a", MD_STD_IN::ensureStringIsUtf8($convToIso2022));
} }
} }

View File

@ -21,7 +21,7 @@ final class MD_STD_Test extends TestCase {
/** /**
* Data provider for returning a tmp file. * Data provider for returning a tmp file.
* *
* @return array<string, array{0: string, 1: string}> * @return array<string, array{0: non-empty-string}>
*/ */
public static function file_name_in_tmp_provider():array { public static function file_name_in_tmp_provider():array {
@ -37,7 +37,7 @@ final class MD_STD_Test extends TestCase {
/** /**
* Data provider for invalid directories. * Data provider for invalid directories.
* *
* @return array<string, array{0: string}> * @return array<non-empty-string, array{0: non-empty-string}>
*/ */
public static function invalid_directory_provider():array { public static function invalid_directory_provider():array {
@ -118,7 +118,7 @@ final class MD_STD_Test extends TestCase {
/** /**
* Checks if a file can be written. * Checks if a file can be written.
* *
* @param string $temp_file Tmp file. * @param non-empty-string $temp_file Tmp file.
* *
* @return void * @return void
*/ */
@ -146,8 +146,8 @@ final class MD_STD_Test extends TestCase {
/** /**
* Check MD_STD::realpath throws exception on non-existing path. * Check MD_STD::realpath throws exception on non-existing path.
* *
* @param string $to_validate Input to validate. * @param non-empty-string $to_validate Input to validate.
* @param string $exceptionClass Exception class. * @param class-string<Throwable> $exceptionClass Exception class.
* *
* @return void * @return void
*/ */
@ -162,8 +162,8 @@ final class MD_STD_Test extends TestCase {
/** /**
* Checks if a file can be read. * Checks if a file can be read.
* *
* @param string $to_validate Input to validate. * @param non-empty-string $to_validate Input to validate.
* @param string $exceptionClass Exception class. * @param class-string<Throwable> $exceptionClass Exception class.
* *
* @return void * @return void
*/ */
@ -229,7 +229,7 @@ final class MD_STD_Test extends TestCase {
/** /**
* Checks if nothing happens if a file does not exist. * Checks if nothing happens if a file does not exist.
* *
* @param string $temp_file Tmp file. * @param non-empty-string $temp_file Tmp file.
* *
* @return void * @return void
*/ */
@ -248,7 +248,7 @@ final class MD_STD_Test extends TestCase {
/** /**
* Checks unlink_if_exists works. * Checks unlink_if_exists works.
* *
* @param string $temp_file Tmp file. * @param non-empty-string $temp_file Tmp file.
* *
* @return void * @return void
*/ */
@ -268,7 +268,10 @@ final class MD_STD_Test extends TestCase {
*/ */
public function test_scandir():void { public function test_scandir():void {
$files = scandir(__DIR__); if (empty($files = scandir(__DIR__))) {
throw new Exception("Running regular scandir() failed on script directory: " . __DIR__);
}
$filesChecked = MD_STD::scandir(__DIR__); $filesChecked = MD_STD::scandir(__DIR__);
foreach ($filesChecked as $file) { foreach ($filesChecked as $file) {
@ -284,7 +287,7 @@ final class MD_STD_Test extends TestCase {
/** /**
* Checks invalid directory. * Checks invalid directory.
* *
* @param string $dir Dir name. * @param non-empty-string $dir Dir name.
* *
* @return void * @return void
*/ */
@ -520,9 +523,9 @@ final class MD_STD_Test extends TestCase {
/** /**
* Checks that minimizeHTMLString works. * Checks that minimizeHTMLString works.
* *
* @param string $filepath File path. * @param non-empty-string $filepath File path.
* @param array<string> $mime_types Mime types expected. * @param array<string> $mime_types Mime types expected.
* @param string $exception Exception class name. * @param class-string<Throwable> $exception Exception class.
* *
* @return void * @return void
*/ */
@ -568,7 +571,7 @@ final class MD_STD_Test extends TestCase {
/** /**
* Checks check_is_writable does not work with non-existent or non-directory paths. * Checks check_is_writable does not work with non-existent or non-directory paths.
* *
* @param string $dir Dir name. * @param non-empty-string $dir Dir name.
* *
* @return void * @return void
*/ */