ソースを参照

:shower: refeactored away some superfluous constants

codemasher 4 年 前
コミット
3d563bfcc0

+ 20 - 30
src/Common/EccLevel.php

@@ -30,32 +30,6 @@ final class EccLevel{
 	/** @var int */
 	/** @var int */
 	public const H = 0b10; // 30%.
 	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
 	 * 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
 	 * returns the string representation of the current ECC level
 	 */
 	 */
 	public function __toString():string{
 	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
 	 * 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{
 	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
 	 * returns the format pattern for the given $eccLevel and $maskPattern
 	 */
 	 */
 	public function getformatPattern(MaskPattern $maskPattern):int{
 	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{
 	public function getMaxBits():array{
 		return array_combine(
 		return array_combine(
 			array_keys(self::MAX_BITS),
 			array_keys(self::MAX_BITS),
-			array_column(self::MAX_BITS, self::MODES[$this->eccLevel])
+			array_column(self::MAX_BITS, $this->getOrdinal())
 		);
 		);
 	}
 	}
 
 

+ 0 - 14
src/Common/Mode.php

@@ -63,20 +63,6 @@ final class Mode{
 		self::BYTE     => Byte::class,
 		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
 	 * returns the length bits for the version breakpoints 1-9, 10-26 and 27-40
 	 *
 	 *

+ 15 - 1
src/Common/Version.php

@@ -311,9 +311,23 @@ final class Version{
 
 
 	/**
 	/**
 	 * the maximum character count for the given $mode and $eccLevel
 	 * the maximum character count for the given $mode and $eccLevel
+	 *
+	 * @throws \chillerlan\QRCode\QRCodeException
 	 */
 	 */
 	public function getMaxLengthForMode(int $mode, EccLevel $eccLevel):?int{
 	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;
 	}
 	}
 
 
 	/**
 	/**

+ 6 - 8
src/QRCode.php

@@ -29,24 +29,22 @@ use function class_exists, class_implements, in_array, mb_convert_encoding, mb_d
 class QRCode{
 class QRCode{
 
 
 	/** @var int */
 	/** @var int */
-	public const VERSION_AUTO       = -1;
+	public const VERSION_AUTO      = -1;
 	/** @var int */
 	/** @var int */
-	public const MASK_PATTERN_AUTO  = -1;
+	public const MASK_PATTERN_AUTO = -1;
 
 
 	/**
 	/**
 	 * @deprecated backward compatibility
 	 * @deprecated backward compatibility
 	 * @see \chillerlan\QRCode\Common\EccLevel
 	 * @see \chillerlan\QRCode\Common\EccLevel
 	 */
 	 */
 	/** @var int */
 	/** @var int */
-	public const ECC_L     = EccLevel::L;
+	public const ECC_L = EccLevel::L;
 	/** @var int */
 	/** @var int */
-	public const ECC_M     = EccLevel::M;
+	public const ECC_M = EccLevel::M;
 	/** @var int */
 	/** @var int */
-	public const ECC_Q     = EccLevel::Q;
+	public const ECC_Q = EccLevel::Q;
 	/** @var int */
 	/** @var int */
-	public const ECC_H     = EccLevel::H;
-	/** @var int[] */
-	public const ECC_MODES = EccLevel::MODES;
+	public const ECC_H = EccLevel::H;
 
 
 	/** @var string */
 	/** @var string */
 	public const OUTPUT_MARKUP_HTML = 'html';
 	public const OUTPUT_MARKUP_HTML = 'html';

+ 1 - 1
src/QROptionsTrait.php

@@ -256,7 +256,7 @@ trait QROptionsTrait{
 	 */
 	 */
 	protected function set_eccLevel(int $eccLevel):void{
 	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));
 			throw new QRCodeException(sprintf('Invalid error correct level: %s', $eccLevel));
 		}
 		}
 
 

+ 1 - 1
tests/Data/QRMatrixTest.php

@@ -68,7 +68,7 @@ final class QRMatrixTest extends TestCase{
 	 * Tests if eccLevel() returns the current (given) ECC level
 	 * Tests if eccLevel() returns the current (given) ECC level
 	 */
 	 */
 	public function testECC():void{
 	public function testECC():void{
-		$this::assertSame(EccLevel::MODES[EccLevel::L], $this->matrix->eccLevel()->getOrdinal());
+		$this::assertSame(EccLevel::L, $this->matrix->eccLevel()->getLevel());
 	}
 	}
 
 
 	/**
 	/**

+ 1 - 1
tests/QRCodeReaderTest.php

@@ -104,7 +104,7 @@ class QRCodeReaderTest extends TestCase{
 		foreach(range(1, 40) as $v){
 		foreach(range(1, 40) as $v){
 			$version = new Version($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);
 				$eccLevel = new EccLevel($ecc);
 
 
 				yield 'version: '.$version.$eccLevel => [
 				yield 'version: '.$version.$eccLevel => [