:octocat: do not throw in QRDataModeInterface::validateString() (#182)

This commit is contained in:
smiley
2023-03-01 20:11:45 +01:00
parent d650fe7306
commit cc58c403f8
4 changed files with 39 additions and 4 deletions
+10 -2
View File
@@ -12,6 +12,7 @@ namespace chillerlan\QRCode\Data;
use chillerlan\QRCode\Common\{BitBuffer, Mode};
use Throwable;
use function chr, implode, is_string, mb_convert_encoding, mb_detect_encoding,
mb_detect_order, mb_internal_encoding, mb_strlen, ord, sprintf, strlen;
@@ -81,8 +82,15 @@ final class Hanzi extends QRDataModeAbstract{
* checks if a string qualifies as Hanzi/GB2312
*/
public static function validateString(string $string):bool{
$string = self::convertEncoding($string);
$len = strlen($string);
try{
$string = self::convertEncoding($string);
}
catch(Throwable $e){
return false;
}
$len = strlen($string);
if($len < 2 || $len % 2 !== 0){
return false;
+10 -2
View File
@@ -12,6 +12,7 @@ namespace chillerlan\QRCode\Data;
use chillerlan\QRCode\Common\{BitBuffer, Mode};
use Throwable;
use function chr, implode, is_string, mb_convert_encoding, mb_detect_encoding,
mb_detect_order, mb_internal_encoding, mb_strlen, ord, sprintf, strlen;
@@ -79,8 +80,15 @@ final class Kanji extends QRDataModeAbstract{
* checks if a string qualifies as SJIS Kanji
*/
public static function validateString(string $string):bool{
$string = self::convertEncoding($string);
$len = strlen($string);
try{
$string = self::convertEncoding($string);
}
catch(Throwable $e){
return false;
}
$len = strlen($string);
if($len < 2 || $len % 2 !== 0){
return false;
+8
View File
@@ -40,4 +40,12 @@ final class ByteTest extends DataInterfaceTestAbstract{
$this::markTestSkipped('N/A (binary mode)');
}
/**
* @inheritDoc
*/
public function testBinaryStringInvalid():void{
/** @noinspection PhpUnitTestFailedLineInspection */
$this::markTestSkipped('N/A (binary mode)');
}
}
+11
View File
@@ -16,6 +16,7 @@ use PHPUnit\Framework\TestCase;
use chillerlan\QRCode\Data\{Hanzi, QRCodeDataException, QRData, QRDataModeInterface, QRMatrix};
use ReflectionClass;
use function hex2bin;
use function str_repeat;
/**
@@ -98,6 +99,16 @@ abstract class DataInterfaceTestAbstract extends TestCase{
$this::assertSame($expected, $this->FQN::validateString($string));
}
/**
* Tests if a binary string is properly validated as false
*
* @see https://github.com/chillerlan/php-qrcode/issues/182
*/
public function testBinaryStringInvalid():void{
/** @noinspection PhpUndefinedMethodInspection */
$this::assertFalse($this->FQN::validateString(hex2bin('01015989f47dff8e852122117e04c90b9f15defc1c36477b1fe1')));
}
/**
* returns versions within the version breakpoints 1-9, 10-26 and 27-40
*/