Преглед изворни кода

:octocat: minor optimizations

smiley пре 2 година
родитељ
комит
c27973ace0
5 измењених фајлова са 32 додато и 35 уклоњено
  1. 3 3
      src/Common/BitBuffer.php
  2. 2 2
      src/Common/MaskPattern.php
  3. 21 17
      src/Data/QRMatrix.php
  4. 4 6
      src/Output/QRImagick.php
  5. 2 7
      src/QRCode.php

+ 3 - 3
src/Common/BitBuffer.php

@@ -43,10 +43,10 @@ final class BitBuffer{
 	/**
 	 * BitBuffer constructor.
 	 *
-	 * @param int[]|null $bytes
+	 * @param int[] $bytes
 	 */
-	public function __construct(array $bytes = null){
-		$this->buffer = ($bytes ?? []);
+	public function __construct(array $bytes = []){
+		$this->buffer = $bytes;
 		$this->length = count($this->buffer);
 	}
 

+ 2 - 2
src/Common/MaskPattern.php

@@ -299,7 +299,7 @@ final class MaskPattern{
 		}
 
 		for($y = $from; $y < $to; $y++){
-			if($matrix[$y][$x]){
+			if($matrix[$y][$x] === true){
 				return false;
 			}
 		}
@@ -317,7 +317,7 @@ final class MaskPattern{
 
 		foreach($matrix as $row){
 			foreach($row as $val){
-				if($val){
+				if($val === true){
 					$darkCells++;
 				}
 			}

+ 21 - 17
src/Data/QRMatrix.php

@@ -286,7 +286,14 @@ class QRMatrix{
 	public function set(int $x, int $y, bool $value, int $M_TYPE):self{
 
 		if(isset($this->matrix[$y][$x])){
-			$this->matrix[$y][$x] = (($M_TYPE & ~$this::IS_DARK) | (($value) ? $this::IS_DARK : 0));
+			// we don't know whether the input is dark, so we remove the dark bit
+			$M_TYPE &= ~$this::IS_DARK;
+
+			if($value === true){
+				$M_TYPE |= $this::IS_DARK;
+			}
+
+			$this->matrix[$y][$x] = $M_TYPE;
 		}
 
 		return $this;
@@ -322,15 +329,16 @@ class QRMatrix{
 	 * Checks whether the module at ($x, $y) is of the given $M_TYPE
 	 *
 	 *   true => $value & $M_TYPE === $M_TYPE
+	 *
+	 * Also, returns false if the given coordinates are out of range.
 	 */
 	public function checkType(int $x, int $y, int $M_TYPE):bool{
-		$val = $this->get($x, $y);
 
-		if($val === -1){
-			return false;
+		if(isset($this->matrix[$y][$x])){
+			return ($this->matrix[$y][$x] & $M_TYPE) === $M_TYPE;
 		}
 
-		return ($val & $M_TYPE) === $M_TYPE;
+		return false;
 	}
 
 	/**
@@ -350,14 +358,16 @@ class QRMatrix{
 
 	/**
 	 * Checks whether the module at ($x, $y) is true (dark) or false (light)
+	 *
+	 * Also, returns false if the given coordinates are out of range.
 	 */
 	public function check(int $x, int $y):bool{
 
-		if(!isset($this->matrix[$y][$x])){
-			return false;
+		if(isset($this->matrix[$y][$x])){
+			return $this->isDark($this->matrix[$y][$x]);
 		}
 
-		return $this->isDark($this->matrix[$y][$x]);
+		return false;
 	}
 
 	/**
@@ -759,14 +769,12 @@ class QRMatrix{
 						continue;
 					}
 
-					$value = 0;
+					$this->matrix[$y][$x] = $this::M_DATA;
 
 					if($iByte < $byteCount && (($data[$iByte] >> $iBit--) & 1) === 1){
-						$value = $this::IS_DARK;
+						$this->matrix[$y][$x] |= $this::IS_DARK;
 					}
 
-					$this->matrix[$y][$x] = ($this::M_DATA | $value);
-
 					if($iBit === -1){
 						$iByte++;
 						$iBit = 7;
@@ -792,11 +800,7 @@ class QRMatrix{
 		foreach($this->matrix as $y => $row){
 			foreach($row as $x => $val){
 				// skip non-data modules
-				if(($val & $this::M_DATA) !== $this::M_DATA){
-					continue;
-				}
-
-				if($mask($x, $y)){
+				if(($val & $this::M_DATA) === $this::M_DATA && $mask($x, $y)){
 					$this->flip($x, $y);
 				}
 			}

+ 4 - 6
src/Output/QRImagick.php

@@ -48,12 +48,10 @@ class QRImagick extends QROutputAbstract{
 	 */
 	public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){
 
-		if(!extension_loaded('imagick')){
-			throw new QRCodeOutputException('ext-imagick not loaded'); // @codeCoverageIgnore
-		}
-
-		if(!extension_loaded('fileinfo')){
-			throw new QRCodeOutputException('ext-fileinfo not loaded'); // @codeCoverageIgnore
+		foreach(['fileinfo', 'imagick'] as $ext){
+			if(!extension_loaded($ext)){
+				throw new QRCodeOutputException(sprintf('ext-%s not loaded', $ext)); // @codeCoverageIgnore
+			}
 		}
 
 		parent::__construct($options, $matrix);

+ 2 - 7
src/QRCode.php

@@ -253,14 +253,9 @@ class QRCode{
 	protected function addMatrixModifications(QRMatrix $matrix):QRMatrix{
 
 		if($this->options->addLogoSpace){
-			$logoSpaceWidth  = $this->options->logoSpaceWidth;
-			$logoSpaceHeight = $this->options->logoSpaceHeight;
-
 			// check whether one of the dimensions was omitted
-			if($logoSpaceWidth === null || $logoSpaceHeight === null){
-				$logoSpaceWidth  = ($logoSpaceWidth ?? $logoSpaceHeight ?? 0);
-				$logoSpaceHeight = null;
-			}
+			$logoSpaceWidth  = ($this->options->logoSpaceWidth ?? $this->options->logoSpaceHeight ?? 0);
+			$logoSpaceHeight = ($this->options->logoSpaceHeight ?? $logoSpaceWidth);
 
 			$matrix->setLogoSpace(
 				$logoSpaceWidth,