diff --git a/src/Common/BitBuffer.php b/src/Common/BitBuffer.php index 9d4d1f221..4a59f2b58 100644 --- a/src/Common/BitBuffer.php +++ b/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); } diff --git a/src/Common/MaskPattern.php b/src/Common/MaskPattern.php index c99733716..8441a7f53 100644 --- a/src/Common/MaskPattern.php +++ b/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++; } } diff --git a/src/Data/QRMatrix.php b/src/Data/QRMatrix.php index 6d1c13a7b..d92c1a784 100755 --- a/src/Data/QRMatrix.php +++ b/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); } } diff --git a/src/Output/QRImagick.php b/src/Output/QRImagick.php index b28c00abd..cfd3a581f 100644 --- a/src/Output/QRImagick.php +++ b/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); diff --git a/src/QRCode.php b/src/QRCode.php index b5ccea9d3..8caa1d739 100755 --- a/src/QRCode.php +++ b/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,