Skip to content

Commit c27973a

Browse files
committed
:octocat: minor optimizations
1 parent 83d5ce9 commit c27973a

File tree

5 files changed

+32
-35
lines changed

5 files changed

+32
-35
lines changed

src/Common/BitBuffer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ final class BitBuffer{
4343
/**
4444
* BitBuffer constructor.
4545
*
46-
* @param int[]|null $bytes
46+
* @param int[] $bytes
4747
*/
48-
public function __construct(array $bytes = null){
49-
$this->buffer = ($bytes ?? []);
48+
public function __construct(array $bytes = []){
49+
$this->buffer = $bytes;
5050
$this->length = count($this->buffer);
5151
}
5252

src/Common/MaskPattern.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ private static function isWhiteVertical(array $matrix, int $height, int $x, int
299299
}
300300

301301
for($y = $from; $y < $to; $y++){
302-
if($matrix[$y][$x]){
302+
if($matrix[$y][$x] === true){
303303
return false;
304304
}
305305
}
@@ -317,7 +317,7 @@ public static function testRule4(array $matrix, int $height, int $width):int{
317317

318318
foreach($matrix as $row){
319319
foreach($row as $val){
320-
if($val){
320+
if($val === true){
321321
$darkCells++;
322322
}
323323
}

src/Data/QRMatrix.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,14 @@ public function get(int $x, int $y):int{
286286
public function set(int $x, int $y, bool $value, int $M_TYPE):self{
287287

288288
if(isset($this->matrix[$y][$x])){
289-
$this->matrix[$y][$x] = (($M_TYPE & ~$this::IS_DARK) | (($value) ? $this::IS_DARK : 0));
289+
// we don't know whether the input is dark, so we remove the dark bit
290+
$M_TYPE &= ~$this::IS_DARK;
291+
292+
if($value === true){
293+
$M_TYPE |= $this::IS_DARK;
294+
}
295+
296+
$this->matrix[$y][$x] = $M_TYPE;
290297
}
291298

292299
return $this;
@@ -322,15 +329,16 @@ public function flip(int $x, int $y):self{
322329
* Checks whether the module at ($x, $y) is of the given $M_TYPE
323330
*
324331
* true => $value & $M_TYPE === $M_TYPE
332+
*
333+
* Also, returns false if the given coordinates are out of range.
325334
*/
326335
public function checkType(int $x, int $y, int $M_TYPE):bool{
327-
$val = $this->get($x, $y);
328336

329-
if($val === -1){
330-
return false;
337+
if(isset($this->matrix[$y][$x])){
338+
return ($this->matrix[$y][$x] & $M_TYPE) === $M_TYPE;
331339
}
332340

333-
return ($val & $M_TYPE) === $M_TYPE;
341+
return false;
334342
}
335343

336344
/**
@@ -350,14 +358,16 @@ public function checkTypeIn(int $x, int $y, array $M_TYPES):bool{
350358

351359
/**
352360
* Checks whether the module at ($x, $y) is true (dark) or false (light)
361+
*
362+
* Also, returns false if the given coordinates are out of range.
353363
*/
354364
public function check(int $x, int $y):bool{
355365

356-
if(!isset($this->matrix[$y][$x])){
357-
return false;
366+
if(isset($this->matrix[$y][$x])){
367+
return $this->isDark($this->matrix[$y][$x]);
358368
}
359369

360-
return $this->isDark($this->matrix[$y][$x]);
370+
return false;
361371
}
362372

363373
/**
@@ -759,14 +769,12 @@ public function writeCodewords(BitBuffer $bitBuffer):self{
759769
continue;
760770
}
761771

762-
$value = 0;
772+
$this->matrix[$y][$x] = $this::M_DATA;
763773

764774
if($iByte < $byteCount && (($data[$iByte] >> $iBit--) & 1) === 1){
765-
$value = $this::IS_DARK;
775+
$this->matrix[$y][$x] |= $this::IS_DARK;
766776
}
767777

768-
$this->matrix[$y][$x] = ($this::M_DATA | $value);
769-
770778
if($iBit === -1){
771779
$iByte++;
772780
$iBit = 7;
@@ -792,11 +800,7 @@ public function mask(MaskPattern $maskPattern):self{
792800
foreach($this->matrix as $y => $row){
793801
foreach($row as $x => $val){
794802
// skip non-data modules
795-
if(($val & $this::M_DATA) !== $this::M_DATA){
796-
continue;
797-
}
798-
799-
if($mask($x, $y)){
803+
if(($val & $this::M_DATA) === $this::M_DATA && $mask($x, $y)){
800804
$this->flip($x, $y);
801805
}
802806
}

src/Output/QRImagick.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@ class QRImagick extends QROutputAbstract{
4848
*/
4949
public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){
5050

51-
if(!extension_loaded('imagick')){
52-
throw new QRCodeOutputException('ext-imagick not loaded'); // @codeCoverageIgnore
53-
}
54-
55-
if(!extension_loaded('fileinfo')){
56-
throw new QRCodeOutputException('ext-fileinfo not loaded'); // @codeCoverageIgnore
51+
foreach(['fileinfo', 'imagick'] as $ext){
52+
if(!extension_loaded($ext)){
53+
throw new QRCodeOutputException(sprintf('ext-%s not loaded', $ext)); // @codeCoverageIgnore
54+
}
5755
}
5856

5957
parent::__construct($options, $matrix);

src/QRCode.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,9 @@ public function getQRMatrix():QRMatrix{
253253
protected function addMatrixModifications(QRMatrix $matrix):QRMatrix{
254254

255255
if($this->options->addLogoSpace){
256-
$logoSpaceWidth = $this->options->logoSpaceWidth;
257-
$logoSpaceHeight = $this->options->logoSpaceHeight;
258-
259256
// check whether one of the dimensions was omitted
260-
if($logoSpaceWidth === null || $logoSpaceHeight === null){
261-
$logoSpaceWidth = ($logoSpaceWidth ?? $logoSpaceHeight ?? 0);
262-
$logoSpaceHeight = null;
263-
}
257+
$logoSpaceWidth = ($this->options->logoSpaceWidth ?? $this->options->logoSpaceHeight ?? 0);
258+
$logoSpaceHeight = ($this->options->logoSpaceHeight ?? $logoSpaceWidth);
264259

265260
$matrix->setLogoSpace(
266261
$logoSpaceWidth,

0 commit comments

Comments
 (0)