Просмотр исходного кода

added PHP7 scalar type hints, cleanup

smiley 9 лет назад
Родитель
Сommit
f66213f24e

+ 8 - 5
src/BitBuffer.php

@@ -38,20 +38,23 @@ class BitBuffer{
 	/**
 	 * @param int $num
 	 * @param int $length
+	 *
+	 * @return void
 	 */
-	public function put($num, $length){
+	public function put(int $num, int $length){
 
-		$i = 0;
-		while($i < $length){
-			$this->putBit(($num >> ($length - $i - 1))&1 === 1); $i++;
+		for($i = 0; $i < $length; $i++){
+			$this->putBit(($num >> ($length - $i - 1))&1 === 1);
 		}
 
 	}
 
 	/**
 	 * @param bool $bit
+	 *
+	 * @return void
 	 */
-	public function putBit($bit){
+	public function putBit(bool $bit){
 		$bufIndex = floor($this->length / 8);
 
 		if(count($this->buffer) <= $bufIndex){

+ 11 - 10
src/Data/AlphaNum.php

@@ -12,8 +12,7 @@
 
 namespace chillerlan\QRCode\Data;
 
-use chillerlan\QRCode\BitBuffer;
-use chillerlan\QRCode\QRConst;
+use chillerlan\QRCode\{BitBuffer, QRConst};
 
 /**
  *
@@ -44,6 +43,8 @@ class AlphaNum extends QRDataAbstract{
 
 	/**
 	 * @param \chillerlan\QRCode\BitBuffer $buffer
+	 *
+	 * @return void
 	 */
 	public function write(BitBuffer &$buffer){
 		$i = 0;
@@ -60,26 +61,26 @@ class AlphaNum extends QRDataAbstract{
 	}
 
 	/**
-	 * @param string $c
+	 * @param string $chr
 	 *
 	 * @return int
 	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
 	 */
-	private static function getCharCode($c){
-		$c = ord($c);
+	private static function getCharCode(string $chr):int {
+		$chr = ord($chr);
 
 		switch(true){
-			case ord('0') <= $c && $c <= ord('9'): return $c - ord('0');
-			case ord('A') <= $c && $c <= ord('Z'): return $c - ord('A') + 10;
+			case ord('0') <= $chr && $chr <= ord('9'): return $chr - ord('0');
+			case ord('A') <= $chr && $chr <= ord('Z'): return $chr - ord('A') + 10;
 			default:
-				foreach(self::CHAR_MAP as $i => $char){
-					if(ord($char) === $c){
+				foreach(self::CHAR_MAP as $i => $c){
+					if(ord($c) === $chr){
 						return $i;
 					}
 				}
 		}
 
-		throw new QRCodeDataException('illegal char: '.$c);
+		throw new QRCodeDataException('illegal char: '.$chr);
 	}
 
 }

+ 3 - 2
src/Data/Byte.php

@@ -12,8 +12,7 @@
 
 namespace chillerlan\QRCode\Data;
 
-use chillerlan\QRCode\BitBuffer;
-use chillerlan\QRCode\QRConst;
+use chillerlan\QRCode\{BitBuffer, QRConst};
 
 /**
  *
@@ -32,6 +31,8 @@ class Byte extends QRDataAbstract{
 
 	/**
 	 * @param \chillerlan\QRCode\BitBuffer $buffer
+	 *
+	 * @return void
 	 */
 	public function write(BitBuffer &$buffer){
 		$i = 0;

+ 2 - 2
src/Data/Kanji.php

@@ -12,8 +12,7 @@
 
 namespace chillerlan\QRCode\Data;
 
-use chillerlan\QRCode\BitBuffer;
-use chillerlan\QRCode\QRConst;
+use chillerlan\QRCode\{BitBuffer, QRConst};
 
 /**
  *
@@ -33,6 +32,7 @@ class Kanji extends QRDataAbstract{
 	/**
 	 * @param \chillerlan\QRCode\BitBuffer $buffer
 	 *
+	 * @return void
 	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
 	 */
 	public function write(BitBuffer &$buffer){

+ 4 - 3
src/Data/Number.php

@@ -12,8 +12,7 @@
 
 namespace chillerlan\QRCode\Data;
 
-use chillerlan\QRCode\BitBuffer;
-use chillerlan\QRCode\QRConst;
+use chillerlan\QRCode\{BitBuffer, QRConst};
 
 /**
  *
@@ -32,6 +31,8 @@ class Number extends QRDataAbstract{
 
 	/**
 	 * @param \chillerlan\QRCode\BitBuffer $buffer
+	 *
+	 * @return void
 	 */
 	public function write(BitBuffer &$buffer){
 		$i = 0;
@@ -60,7 +61,7 @@ class Number extends QRDataAbstract{
 	 * @return int
 	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
 	 */
-	private static function parseInt($string){
+	private static function parseInt(string $string):int {
 		$num = 0;
 
 		$len = strlen($string);

+ 4 - 3
src/Data/QRDataAbstract.php

@@ -37,20 +37,21 @@ abstract class QRDataAbstract implements QRDataInterface{
 	 *
 	 * @param string $data
 	 */
-	public function __construct($data){
+	public function __construct(string $data){
 		$this->data = $data;
 		$this->dataLength = strlen($data);
 	}
 
 	/**
+	 * @see QRCode::createData()
+	 *
 	 * @param int $type
 	 *
 	 * @return int
 	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
-	 * @see QRCode::createData()
 	 * @codeCoverageIgnore
 	 */
-	public function getLengthInBits($type){
+	public function getLengthInBits(int $type):int {
 
 		switch(true){
 			case $type >= 1 && $type <= 9:

+ 1 - 1
src/Data/QRDataInterface.php

@@ -33,6 +33,6 @@ interface QRDataInterface{
 	 * @return int
 	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
 	 */
-	public function getLengthInBits($type);
+	public function getLengthInBits(int $type):int;
 
 }

+ 1 - 1
src/Output/QRImage.php

@@ -36,7 +36,7 @@ class QRImage extends QROutputAbstract{
 	/**
 	 * @return string
 	 */
-	public function dump(){
+	public function dump():string {
 		// clamp input (@todo: determine sane values!)
 		$this->options->pixelSize = max(1, min(25, (int)$this->options->pixelSize));
 		$this->options->marginSize = max(0, min(25, (int)$this->options->marginSize));

+ 5 - 3
src/Output/QRMarkup.php

@@ -35,7 +35,7 @@ class QRMarkup extends QROutputAbstract{
 	/**
 	 * @return string
 	 */
-	public function dump(){
+	public function dump():string {
 		switch($this->options->type){
 			case QRCode::OUTPUT_MARKUP_SVG : return $this->toSVG();
 			case QRCode::OUTPUT_MARKUP_HTML:
@@ -47,7 +47,7 @@ class QRMarkup extends QROutputAbstract{
 	/**
 	 * @return string
 	 */
-	protected function toHTML(){
+	protected function toHTML():string {
 		$html = '';
 
 		foreach($this->matrix as $row){
@@ -73,10 +73,12 @@ class QRMarkup extends QROutputAbstract{
 	}
 
 	/**
+	 * @link https://github.com/codemasher/php-qrcode/pull/5
+	 *
 	 * @return string
 	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
 	 */
-	protected function toSVG(){
+	protected function toSVG():string {
 		$length = $this->pixelCount * $this->options->pixelSize + $this->options->marginSize * 2;
 		$class  = !empty($this->options->cssClass) ? $this->options->cssClass : hash('crc32', microtime(true));
 

+ 2 - 2
src/Output/QROutputAbstract.php

@@ -62,10 +62,10 @@ abstract class QROutputAbstract implements QROutputInterface{
 	/**
 	 * @param array $matrix
 	 *
-	 * @return $this
+	 * @return \chillerlan\QRCode\Output\QROutputInterface
 	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
 	 */
-	public function setMatrix(array $matrix){
+	public function setMatrix(array $matrix):QROutputInterface {
 		$this->pixelCount = count($matrix);
 
 		// specify valid range?

+ 2 - 2
src/Output/QROutputInterface.php

@@ -25,9 +25,9 @@ interface QROutputInterface{
 	/**
 	 * @param array $matrix
 	 *
-	 * @return $this
+	 * @return \chillerlan\QRCode\Output\QROutputInterface
 	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
 	 */
-	public function setMatrix(array $matrix);
+	public function setMatrix(array $matrix):QROutputInterface;
 
 }

+ 2 - 2
src/Output/QRString.php

@@ -29,7 +29,7 @@ class QRString extends QROutputAbstract{
 	/**
 	 * @return string
 	 */
-	public function dump(){
+	public function dump():string {
 
 		switch($this->options->type){
 			case QRCode::OUTPUT_STRING_TEXT: return $this->toString();
@@ -43,7 +43,7 @@ class QRString extends QROutputAbstract{
 	/**
 	 * @return string
 	 */
-	protected function toString(){
+	protected function toString():string {
 		$str = '';
 
 		foreach($this->matrix as $row){

+ 23 - 26
src/Polynomial.php

@@ -25,12 +25,12 @@ class Polynomial{
 	/**
 	 * @var array
 	 */
-	protected $EXP_TABLE = [];
+	protected $expTable = [];
 
 	/**
 	 * @var array
 	 */
-	protected $LOG_TABLE = [];
+	protected $logTable = [];
 
 	/**
 	 * Polynomial constructor.
@@ -38,7 +38,7 @@ class Polynomial{
 	 * @param array $num
 	 * @param int   $shift
 	 */
-	public function __construct(array $num = [1], $shift = 0){
+	public function __construct(array $num = [1], int $shift = 0){
 		$this->setNum($num, $shift)->setTables();
 	}
 
@@ -46,9 +46,9 @@ class Polynomial{
 	 * @param array $num
 	 * @param int   $shift
 	 *
-	 * @return $this
+	 * @return \chillerlan\QRCode\Polynomial
 	 */
-	public function setNum(array $num, $shift = 0){
+	public function setNum(array $num, int $shift = 0):Polynomial {
 		$offset = 0;
 		$numCount = count($num);
 
@@ -58,43 +58,37 @@ class Polynomial{
 
 		$this->num = array_fill(0, $numCount - $offset + $shift, 0);
 
-		$i = 0;
-		while($i < $numCount - $offset){
+		for($i = 0; $i < $numCount - $offset; $i++){
 			$this->num[$i] = $num[$i + $offset];
-			$i++;
 		}
 
 		return $this;
 	}
 
 	/**
-	 *
+	 * @return void
 	 */
 	protected function setTables(){
-		$this->EXP_TABLE = $this->LOG_TABLE = array_fill(0, 256, 0);
+		$this->expTable = $this->logTable = array_fill(0, 256, 0);
 
-		$i = 0;
-		while($i < 8){
-			$this->EXP_TABLE[$i] = 1 << $i;
-			$i++;
+		for($i = 0; $i < 8; $i++){
+			$this->expTable[$i] = 1 << $i;
 		}
 
-		$i = 8;
-		while($i < 256){
-			$this->EXP_TABLE[$i] = $this->EXP_TABLE[$i - 4] ^ $this->EXP_TABLE[$i - 5] ^ $this->EXP_TABLE[$i - 6] ^ $this->EXP_TABLE[$i - 8];
-			$i++;
+		for($i = 8; $i < 256; $i++){
+			$this->expTable[$i] = $this->expTable[$i - 4]^$this->expTable[$i - 5]^$this->expTable[$i - 6]^$this->expTable[$i - 8];
 		}
 
-		$i = 0;
-		while($i < 255){
-			$this->LOG_TABLE[$this->EXP_TABLE[$i]] = $i;
-			$i++;
+		for($i = 0; $i < 255; $i++){
+			$this->logTable[$this->expTable[$i]] = $i;
 		}
 
 	}
 
 	/**
 	 * @param array $e
+	 *
+	 * @return void
 	 */
 	public function multiply(array $e){
 		$n = array_fill(0, count($this->num) + count($e) - 1, 0);
@@ -110,6 +104,8 @@ class Polynomial{
 
 	/**
 	 * @param array $e
+	 *
+	 * @return void
 	 */
 	public function mod(array $e){
 		$n = $this->num;
@@ -119,6 +115,7 @@ class Polynomial{
 		}
 
 		$ratio = $this->glog($n[0]) - $this->glog($e[0]);
+
 		foreach($e as $i => &$v){
 			$n[$i] ^= $this->gexp($this->glog($v) + $ratio);
 		}
@@ -132,13 +129,13 @@ class Polynomial{
 	 * @return int
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 */
-	public function glog($n){
+	public function glog(int $n):int {
 
 		if($n < 1){
 			throw new QRCodeException('log('.$n.')');
 		}
 
-		return $this->LOG_TABLE[$n];
+		return $this->logTable[$n];
 	}
 
 	/**
@@ -146,7 +143,7 @@ class Polynomial{
 	 *
 	 * @return int
 	 */
-	public function gexp($n){
+	public function gexp(int $n):int {
 
 		if($n < 0){
 			$n += 255;
@@ -155,7 +152,7 @@ class Polynomial{
 			$n -= 255;
 		}
 
-		return $this->EXP_TABLE[$n];
+		return $this->expTable[$n];
 	}
 
 }

+ 43 - 39
src/QRCode.php

@@ -12,10 +12,7 @@
 
 namespace chillerlan\QRCode;
 
-use chillerlan\QRCode\Data\AlphaNum;
-use chillerlan\QRCode\Data\Byte;
-use chillerlan\QRCode\Data\Kanji;
-use chillerlan\QRCode\Data\Number;
+use chillerlan\QRCode\Data\{AlphaNum, Byte, Kanji, Number};
 use chillerlan\QRCode\Output\QROutputInterface;
 
 /**
@@ -127,10 +124,10 @@ class QRCode{
 	 * @param string                            $data
 	 * @param \chillerlan\QRCode\QROptions|null $options
 	 *
-	 * @return $this
+	 * @return \chillerlan\QRCode\QRCode
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 */
-	public function setData($data, QROptions $options = null){
+	public function setData(string $data, QROptions $options = null):QRCode {
 		$data = trim($data);
 
 		if(empty($data)){
@@ -178,12 +175,12 @@ class QRCode{
 	}
 
 	/**
-	 * @param $mode
+	 * @param int $mode
 	 *
 	 * @return int
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 */
-	protected function getTypeNumber($mode){
+	protected function getTypeNumber(int $mode):int {
 		$length = $this->qrDataInterface->dataLength;
 
 		if($this->qrDataInterface->mode === QRConst::MODE_KANJI){
@@ -204,17 +201,18 @@ class QRCode{
 	 */
 	public function output(){
 		$this->qrOutputInterface->setMatrix($this->getRawData());
+
 		return $this->qrOutputInterface->dump();
 	}
 
 	/**
 	 * @return array
 	 */
-	public function getRawData(){
+	public function getRawData():array {
 		$this->minLostPoint = 0;
 		$this->maskPattern = 0;
 
-		foreach(range(0, 7) as $pattern){
+		for($pattern = 0; $pattern <= 7; $pattern++){
 			$this->testPattern($pattern);
 		}
 
@@ -225,6 +223,8 @@ class QRCode{
 
 	/**
 	 * @param array $range
+	 *
+	 * @return void
 	 */
 	protected function testLevel1(array $range){
 
@@ -260,6 +260,8 @@ class QRCode{
 
 	/**
 	 * @param array $range
+	 *
+	 * @return void
 	 */
 	protected function testLevel2(array $range){
 
@@ -288,6 +290,8 @@ class QRCode{
 	/**
 	 * @param array $range1
 	 * @param array $range2
+	 *
+	 * @return void
 	 */
 	protected function testLevel3(array $range1, array $range2){
 
@@ -331,6 +335,8 @@ class QRCode{
 
 	/**
 	 * @param array $range
+	 *
+	 * @return void
 	 */
 	protected function testLevel4(array $range){
 
@@ -346,8 +352,10 @@ class QRCode{
 
 	/**
 	 * @param int $pattern
+	 *
+	 * @return void
 	 */
-	protected function testPattern($pattern){
+	protected function testPattern(int $pattern){
 		$this->getMatrix(true, $pattern);
 		$this->lostPoint = 0;
 		$this->darkCount = 0;
@@ -370,16 +378,17 @@ class QRCode{
 
 	/**
 	 * @param bool $test
+	 *
+	 * @return void
 	 */
-	protected function setTypeNumber($test){
+	protected function setTypeNumber(bool $test){
 		$bits = Util::getBCHTypeNumber($this->typeNumber);
-		$i = 0;
-		while($i < 18){
+
+		for($i = 0; $i < 18; $i++){
 			$a = (int)floor($i / 3);
 			$b = $i % 3 + $this->pixelCount - 8 - 3;
 
 			$this->matrix[$a][$b] = $this->matrix[$b][$a] = !$test && (($bits >> $i) & 1) === 1;
-			$i++;
 		}
 
 	}
@@ -387,12 +396,14 @@ class QRCode{
 	/**
 	 * @param bool $test
 	 * @param int  $pattern
+	 *
+	 * @return void
 	 */
-	protected function setTypeInfo($test, $pattern){
+	protected function setTypeInfo(bool $test, int $pattern){
 		$this->setPattern();
 		$bits = Util::getBCHTypeInfo(($this->errorCorrectLevel << 3) | $pattern);
-		$i = 0;
-		while($i < 15){
+
+		for($i = 0; $i < 15; $i++){
 			$mod = !$test && (($bits >> $i) & 1) === 1;
 
 			switch(true){
@@ -408,13 +419,14 @@ class QRCode{
 				default:
 					$this->matrix[8][15 - $i - 1] = $mod;
 			}
-			$i++;
+
 		}
 
 		$this->matrix[$this->pixelCount - 8][8] = !$test;
 	}
 
 	/**
+	 * @return void
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 */
 	protected function createData(){
@@ -467,7 +479,7 @@ class QRCode{
 	 * @return array
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 */
-	protected function createBytes(){
+	protected function createBytes():array {
 		$totalCodeCount = $maxDcCount = $maxEcCount = $offset = $index = 0;
 		$rsBlocks = Util::getRSBlocks($this->typeNumber, $this->errorCorrectLevel);
 		$rsBlockCount = count($rsBlocks);
@@ -492,11 +504,9 @@ class QRCode{
 			$rsPoly = new Polynomial;
 			$modPoly = new Polynomial;
 
-			$i = 0;
-			while($i < $rsBlockTotal - $rsBlockDataCount){
+			foreach(range(0, $rsBlockTotal - $rsBlockDataCount - 1) as $i){
 				$modPoly->setNum([1, $modPoly->gexp($i)]);
 				$rsPoly->multiply($modPoly->num);
-				$i++;
 			}
 
 			$rsPolyCount = count($rsPoly->num);
@@ -515,24 +525,20 @@ class QRCode{
 		$data = array_fill(0, $totalCodeCount, null);
 		$rsrange = range(0, $rsBlockCount - 1);
 
-		$i = 0;
-		while($i < $maxDcCount){
+		foreach(range(0, $maxDcCount - 1) as $i){
 			foreach($rsrange as $key){
 				if($i < count($dcdata[$key])){
 					$data[$index++] = $dcdata[$key][$i];
 				}
 			}
-			$i++;
 		}
 
-		$i = 0;
-		while($i < $maxEcCount){
+		foreach(range(0, $maxEcCount - 1) as $i){
 			foreach($rsrange as $key){
 				if($i < count($ecdata[$key])){
 					$data[$index++] = $ecdata[$key][$i];
 				}
 			}
-			$i++;
 		}
 
 		return $data;
@@ -541,9 +547,10 @@ class QRCode{
 	/**
 	 * @param int $pattern
 	 *
+	 * @return void
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 */
-	protected function mapData($pattern){
+	protected function mapData(int $pattern){
 		$this->createData();
 		$data = $this->createBytes();
 		$inc = -1;
@@ -558,8 +565,7 @@ class QRCode{
 			}
 
 			while(true){
-				$c = 0;
-				while($c < 2){
+				foreach([0, 1] as $c){
 					$_col = $col - $c;
 
 					if($this->matrix[$row][$_col] === null){
@@ -594,7 +600,6 @@ class QRCode{
 							$bitIndex = 7;
 						}
 					}
-					$c++;
 				}
 
 				$row += $inc;
@@ -609,7 +614,7 @@ class QRCode{
 	}
 
 	/**
-	 *
+	 * @return void
 	 */
 	protected function setupPositionProbePattern(){
 		$range = range(-1, 7);
@@ -618,8 +623,7 @@ class QRCode{
 			$row = $grid[0];
 			$col = $grid[1];
 
-			$r = -1;
-			while($r < 8){
+			foreach($range as $r){
 				foreach($range as $c){
 
 					if($row + $r <= -1 || $this->pixelCount <= $row + $r || $col + $c <= -1 || $this->pixelCount <= $col + $c){
@@ -631,14 +635,13 @@ class QRCode{
 						|| (0 <= $c && $c <= 6 && ($r === 0 || $r === 6))
 						|| (2 <= $c && $c <= 4 &&  2 <= $r && $r <= 4);
 				}
-				$r++;
 			}
 		}
 
 	}
 
 	/**
-	 *
+	 * @return void
 	 */
 	protected function setupPositionAdjustPattern(){
 		$range = QRConst::PATTERN_POSITION[$this->typeNumber - 1];
@@ -663,6 +666,7 @@ class QRCode{
 	}
 
 	/**
+	 * @return void
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 */
 	protected function setPattern(){
@@ -686,7 +690,7 @@ class QRCode{
 	 *
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 */
-	protected function getMatrix($test, $maskPattern){
+	protected function getMatrix(bool $test, int $maskPattern){
 		$this->pixelCount = $this->typeNumber * 4 + 17;
 		$this->matrix = array_fill(0, $this->pixelCount, array_fill(0, $this->pixelCount, null));
 		$this->setTypeInfo($test, $maskPattern);

+ 50 - 47
src/Util.php

@@ -18,17 +18,17 @@ namespace chillerlan\QRCode;
 class Util{
 
 	/**
-	 * @param string $s
+	 * @param string $string
 	 *
 	 * @return bool
 	 */
-	public static function isNumber($s){
+	public static function isNumber(string $string):bool {
+		$len = strlen($string);
 
-		$len = strlen($s);
 		for($i = 0; $i < $len; $i++){
-			$c = ord($s[$i]);
+			$chr = ord($string[$i]);
 
-			if(!(ord('0') <= $c && $c <= ord('9'))){
+			if(!(ord('0') <= $chr && $chr <= ord('9'))){
 				return false;
 			}
 		}
@@ -37,17 +37,21 @@ class Util{
 	}
 
 	/**
-	 * @param string $s
+	 * @param string $string
 	 *
 	 * @return bool
 	 */
-	public static function isAlphaNum($s){
+	public static function isAlphaNum(string $string):bool {
+		$len = strlen($string);
 
-		$len = strlen($s);
 		for($i = 0; $i < $len; $i++){
-			$c = ord($s[$i]);
+			$chr = ord($string[$i]);
 
-			if(!(ord('0') <= $c && $c <= ord('9')) && !(ord('A') <= $c && $c <= ord('Z')) && strpos(' $%*+-./:', $s[$i]) === false){
+			if(
+				  !(ord('0') <= $chr && $chr <= ord('9'))
+			   && !(ord('A') <= $chr && $chr <= ord('Z'))
+			   && strpos(' $%*+-./:', $string[$i]) === false
+			){
 				return false;
 			}
 		}
@@ -56,20 +60,21 @@ class Util{
 	}
 
 	/**
-	 * @param string $s
+	 * @param string $string
 	 *
 	 * @return bool
 	 */
-	public static function isKanji($s){
+	public static function isKanji(string $string):bool {
 
-		if(empty($s)){
+		if(empty($string)){
 			return false;
 		}
 
 		$i = 0;
-		$len = strlen($s);
-		while($i + 1 < $len){
-			$c = ((0xff&ord($s[$i])) << 8)|(0xff&ord($s[$i + 1]));
+		$length = strlen($string);
+
+		while($i + 1 < $length){
+			$c = ((0xff&ord($string[$i])) << 8)|(0xff&ord($string[$i + 1]));
 
 			if(!($c >= 0x8140 && $c <= 0x9FFC) && !($c >= 0xE040 && $c <= 0xEBBF)){
 				return false;
@@ -78,7 +83,7 @@ class Util{
 			$i += 2;
 		}
 
-		return !($i < $len);
+		return !($i < $length);
 	}
 
 	/**
@@ -86,29 +91,34 @@ class Util{
 	 *
 	 * @return int
 	 */
-	public static function getBCHTypeInfo($data){
-		$d = $data << 10;
-
-		while(self::getBCHDigit($d) - self::getBCHDigit(QRConst::G15) >= 0){
-			$d ^= (QRConst::G15 << (self::getBCHDigit($d) - self::getBCHDigit(QRConst::G15)));
-		}
+	public static function getBCHTypeInfo(int $data):int {
+		return (($data << 10)|self::getBCHT($data, 10, QRConst::G15))^QRConst::G15_MASK;
+	}
 
-		return (($data << 10)|$d)^QRConst::G15_MASK;
+	/**
+	 * @param int $data
+	 *
+	 * @return int
+	 */
+	public static function getBCHTypeNumber(int $data):int{
+		return ($data << 12)|self::getBCHT($data, 12, QRConst::G18);
 	}
 
 	/**
 	 * @param int $data
+	 * @param int $bits
+	 * @param int $mask
 	 *
 	 * @return int
 	 */
-	public static function getBCHTypeNumber($data){
-		$d = $data << 12;
+	protected static function getBCHT(int $data, int $bits, int $mask):int {
+		$digit = $data << $bits;
 
-		while(self::getBCHDigit($d) - self::getBCHDigit(QRConst::G18) >= 0){
-			$d ^= (QRConst::G18 << (self::getBCHDigit($d) - self::getBCHDigit(QRConst::G18)));
+		while(self::getBCHDigit($digit) - self::getBCHDigit($mask) >= 0){
+			$digit ^= ($mask << (self::getBCHDigit($digit) - self::getBCHDigit($mask)));
 		}
 
-		return ($data << 12)|$d;
+		return $digit;
 	}
 
 	/**
@@ -116,7 +126,7 @@ class Util{
 	 *
 	 * @return int
 	 */
-	public static function getBCHDigit($data){
+	public static function getBCHDigit(int $data):int {
 		$digit = 0;
 
 		while($data !== 0){
@@ -134,19 +144,16 @@ class Util{
 	 * @return array
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 */
-	public static function getRSBlocks($typeNumber, $errorCorrectLevel){
-		// PHP5 compat
-		$RSBLOCK = QRConst::RSBLOCK;
-		$BLOCK_TABLE = QRConst::BLOCK_TABLE;
+	public static function getRSBlocks(int $typeNumber, int $errorCorrectLevel):array {
 
-		if(!isset($RSBLOCK[$errorCorrectLevel])){
+		if(!array_key_exists($errorCorrectLevel, QRConst::RSBLOCK)){
 			throw new QRCodeException('$typeNumber: '.$typeNumber.' / $errorCorrectLevel: '.$errorCorrectLevel);
 		}
 
-		$rsBlock = $BLOCK_TABLE[($typeNumber - 1) * 4 + $RSBLOCK[$errorCorrectLevel]];
-
+		$rsBlock = QRConst::BLOCK_TABLE[($typeNumber - 1) * 4 + QRConst::RSBLOCK[$errorCorrectLevel]];
 		$list = [];
 		$length = count($rsBlock) / 3;
+
 		for($i = 0; $i < $length; $i++){
 			for($j = 0; $j < $rsBlock[$i * 3 + 0]; $j++){
 				$list[] = [$rsBlock[$i * 3 + 1], $rsBlock[$i * 3 + 2]];
@@ -159,26 +166,22 @@ class Util{
 	/**
 	 * @param int $typeNumber
 	 * @param int $mode
-	 * @param int $ecLevel
+	 * @param int $errorCorrectLevel
 	 *
 	 * @return int
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 */
-	public static function getMaxLength($typeNumber, $mode, $ecLevel){
-		$RSBLOCK = QRConst::RSBLOCK;
-		$MAX_LENGTH = QRConst::MAX_LENGTH;
-		$MODE = QRConst::MODE;
+	public static function getMaxLength(int $typeNumber, int $mode, int $errorCorrectLevel):int {
 
-		if(!isset($RSBLOCK[$ecLevel])){
-			throw new QRCodeException('Invalid error correct level: '.$ecLevel);
+		if(!array_key_exists($errorCorrectLevel, QRConst::RSBLOCK)){
+			throw new QRCodeException('Invalid error correct level: '.$errorCorrectLevel);
 		}
 
-		if(!isset($MODE[$mode])){
+		if(!array_key_exists($mode, QRConst::MODE)){
 			throw new QRCodeException('Invalid mode: '.$mode);
 		}
 
-		return $MAX_LENGTH[$typeNumber - 1][$RSBLOCK[$ecLevel]][$MODE[$mode]];
+		return QRConst::MAX_LENGTH[$typeNumber - 1][QRConst::RSBLOCK[$errorCorrectLevel]][QRConst::MODE[$mode]];
 	}
 
-
 }