From 3d563bfcc0e9120d83789acd929842b7917e7c1b Mon Sep 17 00:00:00 2001 From: codemasher Date: Sat, 27 Nov 2021 14:01:14 +0100 Subject: [PATCH] :shower: refeactored away some superfluous constants --- src/Common/EccLevel.php | 50 +++++++++++++++---------------------- src/Common/Mode.php | 14 ----------- src/Common/Version.php | 16 +++++++++++- src/QRCode.php | 14 +++++------ src/QROptionsTrait.php | 2 +- tests/Data/QRMatrixTest.php | 2 +- tests/QRCodeReaderTest.php | 2 +- 7 files changed, 44 insertions(+), 56 deletions(-) diff --git a/src/Common/EccLevel.php b/src/Common/EccLevel.php index d9f849d81..c2d7a608b 100644 --- a/src/Common/EccLevel.php +++ b/src/Common/EccLevel.php @@ -30,32 +30,6 @@ final class EccLevel{ /** @var int */ public const H = 0b10; // 30%. - /** - * References to the keys of the following tables: - * - * @see \chillerlan\QRCode\Common\Version::MAX_BITS - * @see \chillerlan\QRCode\Common\EccLevel::RSBLOCKS - * @see \chillerlan\QRCode\Common\EccLevel::formatPattern - * - * @var int[] - */ - public const MODES = [ - self::L => 0, - self::M => 1, - self::Q => 2, - self::H => 3, - ]; - - /** - * @var string[] - */ - public const MODES_STRING = [ - self::L => 'L', - self::M => 'M', - self::Q => 'Q', - self::H => 'H', - ]; - /** * ISO/IEC 18004:2000 Tables 7-11 - Number of symbol characters and input data capacity for versions 1 to 40 * @@ -183,7 +157,12 @@ final class EccLevel{ * returns the string representation of the current ECC level */ public function __toString():string{ - return self::MODES_STRING[$this->eccLevel]; + return [ + self::L => 'L', + self::M => 'M', + self::Q => 'Q', + self::H => 'H', + ][$this->eccLevel]; } /** @@ -195,16 +174,27 @@ final class EccLevel{ /** * returns the ordinal value of the current ECC level + * + * references to the keys of the following tables: + * + * @see \chillerlan\QRCode\Common\EccLevel::MAX_BITS + * @see \chillerlan\QRCode\Common\EccLevel::FORMAT_PATTERN + * @see \chillerlan\QRCode\Common\Version::RSBLOCKS */ public function getOrdinal():int{ - return self::MODES[$this->eccLevel]; + return [ + self::L => 0, + self::M => 1, + self::Q => 2, + self::H => 3, + ][$this->eccLevel]; } /** * returns the format pattern for the given $eccLevel and $maskPattern */ public function getformatPattern(MaskPattern $maskPattern):int{ - return self::FORMAT_PATTERN[self::MODES[$this->eccLevel]][$maskPattern->getPattern()]; + return self::FORMAT_PATTERN[$this->getOrdinal()][$maskPattern->getPattern()]; } /** @@ -215,7 +205,7 @@ final class EccLevel{ public function getMaxBits():array{ return array_combine( array_keys(self::MAX_BITS), - array_column(self::MAX_BITS, self::MODES[$this->eccLevel]) + array_column(self::MAX_BITS, $this->getOrdinal()) ); } diff --git a/src/Common/Mode.php b/src/Common/Mode.php index 4285861cc..e18b01e5a 100644 --- a/src/Common/Mode.php +++ b/src/Common/Mode.php @@ -63,20 +63,6 @@ final class Mode{ self::BYTE => Byte::class, ]; - /** - * References to the keys of the following table(s): - * - * @see \chillerlan\QRCode\Common\Version::MAX_LENGTH - * - * @var int[] - */ - public const MODES = [ - self::NUMBER => 0, - self::ALPHANUM => 1, - self::BYTE => 2, - self::KANJI => 3, - ]; - /** * returns the length bits for the version breakpoints 1-9, 10-26 and 27-40 * diff --git a/src/Common/Version.php b/src/Common/Version.php index 46f84075d..10d72758c 100644 --- a/src/Common/Version.php +++ b/src/Common/Version.php @@ -311,9 +311,23 @@ final class Version{ /** * the maximum character count for the given $mode and $eccLevel + * + * @throws \chillerlan\QRCode\QRCodeException */ public function getMaxLengthForMode(int $mode, EccLevel $eccLevel):?int{ - return self::MAX_LENGTH[$this->version][Mode::MODES[$mode]][$eccLevel->getOrdinal()] ?? null; + + $dataModes = [ + Mode::NUMBER => 0, + Mode::ALPHANUM => 1, + Mode::BYTE => 2, + Mode::KANJI => 3, + ]; + + if(!isset($dataModes[$mode])){ + throw new QRCodeException('invalid $mode'); + } + + return self::MAX_LENGTH[$this->version][$dataModes[$mode]][$eccLevel->getOrdinal()] ?? null; } /** diff --git a/src/QRCode.php b/src/QRCode.php index 1256730da..eec48a5a0 100755 --- a/src/QRCode.php +++ b/src/QRCode.php @@ -29,24 +29,22 @@ use function class_exists, class_implements, in_array, mb_convert_encoding, mb_d class QRCode{ /** @var int */ - public const VERSION_AUTO = -1; + public const VERSION_AUTO = -1; /** @var int */ - public const MASK_PATTERN_AUTO = -1; + public const MASK_PATTERN_AUTO = -1; /** * @deprecated backward compatibility * @see \chillerlan\QRCode\Common\EccLevel */ /** @var int */ - public const ECC_L = EccLevel::L; + public const ECC_L = EccLevel::L; /** @var int */ - public const ECC_M = EccLevel::M; + public const ECC_M = EccLevel::M; /** @var int */ - public const ECC_Q = EccLevel::Q; + public const ECC_Q = EccLevel::Q; /** @var int */ - public const ECC_H = EccLevel::H; - /** @var int[] */ - public const ECC_MODES = EccLevel::MODES; + public const ECC_H = EccLevel::H; /** @var string */ public const OUTPUT_MARKUP_HTML = 'html'; diff --git a/src/QROptionsTrait.php b/src/QROptionsTrait.php index 410ef3f68..ef34420db 100644 --- a/src/QROptionsTrait.php +++ b/src/QROptionsTrait.php @@ -256,7 +256,7 @@ trait QROptionsTrait{ */ protected function set_eccLevel(int $eccLevel):void{ - if(!isset(EccLevel::MODES[$eccLevel])){ + if(!in_array($eccLevel, [EccLevel::L, EccLevel::M, EccLevel::Q, EccLevel::H], true)){ throw new QRCodeException(sprintf('Invalid error correct level: %s', $eccLevel)); } diff --git a/tests/Data/QRMatrixTest.php b/tests/Data/QRMatrixTest.php index bf741df16..53e554135 100755 --- a/tests/Data/QRMatrixTest.php +++ b/tests/Data/QRMatrixTest.php @@ -68,7 +68,7 @@ final class QRMatrixTest extends TestCase{ * Tests if eccLevel() returns the current (given) ECC level */ public function testECC():void{ - $this::assertSame(EccLevel::MODES[EccLevel::L], $this->matrix->eccLevel()->getOrdinal()); + $this::assertSame(EccLevel::L, $this->matrix->eccLevel()->getLevel()); } /** diff --git a/tests/QRCodeReaderTest.php b/tests/QRCodeReaderTest.php index a56bc3ebd..11d2d34a4 100644 --- a/tests/QRCodeReaderTest.php +++ b/tests/QRCodeReaderTest.php @@ -104,7 +104,7 @@ class QRCodeReaderTest extends TestCase{ foreach(range(1, 40) as $v){ $version = new Version($v); - foreach(EccLevel::MODES as $ecc => $_){ + foreach([EccLevel::L, EccLevel::M, EccLevel::Q, EccLevel::H] as $ecc){ $eccLevel = new EccLevel($ecc); yield 'version: '.$version.$eccLevel => [