Skip to content

Commit e9743c0

Browse files
committed
:octocat: spimplifications & cleanup
1 parent 1a0208a commit e9743c0

File tree

4 files changed

+51
-59
lines changed

4 files changed

+51
-59
lines changed

src/Common/MaskPattern.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,6 @@ public function getPattern():int{
9494
/**
9595
* Returns a closure that applies the mask for the chosen mask pattern.
9696
*
97-
* Encapsulates data masks for the data bits in a QR code, per ISO 18004:2006 6.8. Implementations
98-
* of this class can un-mask a raw BitMatrix. For simplicity, they will unmask the entire BitMatrix,
99-
* including areas used for finder patterns, timing patterns, etc. These areas should be unused
100-
* after the point they are unmasked anyway.
101-
*
10297
* Note that the diagram in section 6.8.1 is misleading since it indicates that $i is column position
10398
* and $j is row position. In fact, as the text says, $i is row position and $j is column position.
10499
*
@@ -304,7 +299,7 @@ private static function isWhiteVertical(array $matrix, int $height, int $x, int
304299
}
305300

306301
for($y = $from; $y < $to; $y++){
307-
if($matrix[$y][$x]){
302+
if($matrix[$y][$x] === true){
308303
return false;
309304
}
310305
}
@@ -322,7 +317,7 @@ public static function testRule4(array $matrix, int $height, int $width):int{
322317

323318
foreach($matrix as $row){
324319
foreach($row as $val){
325-
if($val){
320+
if($val === true){
326321
$darkCells++;
327322
}
328323
}

src/Data/QRMatrix.php

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

243243
if(isset($this->matrix[$y][$x])){
244-
$this->matrix[$y][$x] = (($M_TYPE & ~$this::IS_DARK) | (($value) ? $this::IS_DARK : 0));
244+
// we don't know whether the input is dark, so we remove the dark bit
245+
$M_TYPE &= ~$this::IS_DARK;
246+
247+
if($value === true){
248+
$M_TYPE |= $this::IS_DARK;
249+
}
250+
251+
$this->matrix[$y][$x] = $M_TYPE;
245252
}
246253

247254
return $this;
@@ -277,15 +284,16 @@ public function flip(int $x, int $y):static{
277284
* Checks whether the module at ($x, $y) is of the given $M_TYPE
278285
*
279286
* true => $value & $M_TYPE === $M_TYPE
287+
*
288+
* Also, returns false if the given coordinates are out of range.
280289
*/
281290
public function checkType(int $x, int $y, int $M_TYPE):bool{
282-
$val = $this->get($x, $y);
283291

284-
if($val === -1){
285-
return false;
292+
if(isset($this->matrix[$y][$x])){
293+
return ($this->matrix[$y][$x] & $M_TYPE) === $M_TYPE;
286294
}
287295

288-
return ($val & $M_TYPE) === $M_TYPE;
296+
return false;
289297
}
290298

291299
/**
@@ -305,14 +313,16 @@ public function checkTypeIn(int $x, int $y, array $M_TYPES):bool{
305313

306314
/**
307315
* Checks whether the module at ($x, $y) is true (dark) or false (light)
316+
*
317+
* Also, returns false if the given coordinates are out of range.
308318
*/
309319
public function check(int $x, int $y):bool{
310320

311-
if(!isset($this->matrix[$y][$x])){
312-
return false;
321+
if(isset($this->matrix[$y][$x])){
322+
return $this->isDark($this->matrix[$y][$x]);
313323
}
314324

315-
return $this->isDark($this->matrix[$y][$x]);
325+
return false;
316326
}
317327

318328
/**
@@ -714,14 +724,12 @@ public function writeCodewords(BitBuffer $bitBuffer):static{
714724
continue;
715725
}
716726

717-
$value = 0;
727+
$this->matrix[$y][$x] = $this::M_DATA;
718728

719729
if($iByte < $byteCount && (($data[$iByte] >> $iBit--) & 1) === 1){
720-
$value = $this::IS_DARK;
730+
$this->matrix[$y][$x] |= $this::IS_DARK;
721731
}
722732

723-
$this->matrix[$y][$x] = ($this::M_DATA | $value);
724-
725733
if($iBit === -1){
726734
$iByte++;
727735
$iBit = 7;
@@ -747,11 +755,7 @@ public function mask(MaskPattern $maskPattern):static{
747755
foreach($this->matrix as $y => $row){
748756
foreach($row as $x => $val){
749757
// skip non-data modules
750-
if(($val & $this::M_DATA) !== $this::M_DATA){
751-
continue;
752-
}
753-
754-
if($mask($x, $y)){
758+
if(($val & $this::M_DATA) === $this::M_DATA && $mask($x, $y)){
755759
$this->flip($x, $y);
756760
}
757761
}

tests/Common/MaskPatternTest.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,36 +129,36 @@ public function testInvalidMaskPatternException():void{
129129

130130
public function testPenaltyRule1():void{
131131
// horizontal
132-
$this::assertSame(0, MaskPattern::testRule1([[0, 0, 0, 0]], 1, 4));
133-
$this::assertSame(3, MaskPattern::testRule1([[0, 0, 0, 0, 0, 1]], 1, 6));
134-
$this::assertSame(4, MaskPattern::testRule1([[0, 0, 0, 0, 0, 0]], 1, 6));
132+
$this::assertSame(0, MaskPattern::testRule1([[false, false, false, false]], 1, 4));
133+
$this::assertSame(3, MaskPattern::testRule1([[false, false, false, false, false, true]], 1, 6));
134+
$this::assertSame(4, MaskPattern::testRule1([[false, false, false, false, false, false]], 1, 6));
135135
// vertical
136-
$this::assertSame(0, MaskPattern::testRule1([[0], [0], [0], [0]], 4, 1));
137-
$this::assertSame(3, MaskPattern::testRule1([[0], [0], [0], [0], [0], [1]], 6, 1));
138-
$this::assertSame(4, MaskPattern::testRule1([[0], [0], [0], [0], [0], [0]], 6, 1));
136+
$this::assertSame(0, MaskPattern::testRule1([[false], [false], [false], [false]], 4, 1));
137+
$this::assertSame(3, MaskPattern::testRule1([[false], [false], [false], [false], [false], [true]], 6, 1));
138+
$this::assertSame(4, MaskPattern::testRule1([[false], [false], [false], [false], [false], [false]], 6, 1));
139139
}
140140

141141
public function testPenaltyRule2():void{
142-
$this::assertSame(0, MaskPattern::testRule2([[0]], 1, 1));
143-
$this::assertSame(0, MaskPattern::testRule2([[0, 0], [0, 1]], 2, 2));
144-
$this::assertSame(3, MaskPattern::testRule2([[0, 0], [0, 0]], 2, 2));
145-
$this::assertSame(12, MaskPattern::testRule2([[0, 0, 0], [0, 0, 0], [0, 0, 0]], 3, 3));
142+
$this::assertSame(0, MaskPattern::testRule2([[false]], 1, 1));
143+
$this::assertSame(0, MaskPattern::testRule2([[false, false], [false, true]], 2, 2));
144+
$this::assertSame(3, MaskPattern::testRule2([[false, false], [false, false]], 2, 2));
145+
$this::assertSame(12, MaskPattern::testRule2([[false, false, false], [false, false, false], [false, false, false]], 3, 3));
146146
}
147147

148148
public function testPenaltyRule3():void{
149149
// horizontal
150-
$this::assertSame(40, MaskPattern::testRule3([[0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1]], 1, 11));
151-
$this::assertSame(40, MaskPattern::testRule3([[1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0]], 1, 11));
152-
$this::assertSame(0, MaskPattern::testRule3([[1, 0, 1, 1, 1, 0, 1]], 1, 7));
150+
$this::assertSame(40, MaskPattern::testRule3([[false, false, false, false, true, false, true, true, true, false, true]], 1, 11));
151+
$this::assertSame(40, MaskPattern::testRule3([[true, false, true, true, true, false, true, false, false, false, false]], 1, 11));
152+
$this::assertSame(0, MaskPattern::testRule3([[true, false, true, true, true, false, true]], 1, 7));
153153
// vertical
154-
$this::assertSame(40, MaskPattern::testRule3([[0], [0], [0], [0], [1], [0], [1], [1], [1], [0], [1]], 11, 1));
155-
$this::assertSame(40, MaskPattern::testRule3([[1], [0], [1], [1], [1], [0], [1], [0], [0], [0], [0]], 11, 1));
156-
$this::assertSame(0, MaskPattern::testRule3([[1], [0], [1], [1], [1], [0], [1]], 7, 1));
154+
$this::assertSame(40, MaskPattern::testRule3([[false], [false], [false], [false], [true], [false], [true], [true], [true], [false], [true]], 11, 1));
155+
$this::assertSame(40, MaskPattern::testRule3([[true], [false], [true], [true], [true], [false], [true], [false], [false], [false], [false]], 11, 1));
156+
$this::assertSame(0, MaskPattern::testRule3([[true], [false], [true], [true], [true], [false], [true]], 7, 1));
157157
}
158158

159159
public function testPenaltyRule4():void{
160-
$this::assertSame(100, MaskPattern::testRule4([[0]], 1, 1));
161-
$this::assertSame(0, MaskPattern::testRule4([[0, 1]], 1, 2));
162-
$this::assertSame(30, MaskPattern::testRule4([[0, 1, 1, 1, 1, 0]], 1, 6));
160+
$this::assertSame(100, MaskPattern::testRule4([[false]], 1, 1));
161+
$this::assertSame(0, MaskPattern::testRule4([[false, true]], 1, 2));
162+
$this::assertSame(30, MaskPattern::testRule4([[false, true, true, true, true, false]], 1, 6));
163163
}
164164
}

tests/Data/QRMatrixTest.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,31 @@ protected function setUp():void{
3737
);
3838
}
3939

40-
/**
41-
* Validates the QRMatrix instance
42-
*/
43-
public function testInstance():void{
44-
$this::assertInstanceOf(QRMatrix::class, $this->matrix);
45-
}
46-
4740
/**
4841
* Tests if size() returns the actual matrix size/count
4942
*/
50-
public function testSize():void{
43+
public function testGetSize():void{
5144
$this::assertCount($this->matrix->getSize(), $this->matrix->getMatrix(true));
5245
}
5346

5447
/**
5548
* Tests if version() returns the current (given) version
5649
*/
57-
public function testVersion():void{
50+
public function testGetVersion():void{
5851
$this::assertSame($this::version, $this->matrix->getVersion()->getVersionNumber());
5952
}
6053

6154
/**
6255
* Tests if eccLevel() returns the current (given) ECC level
6356
*/
64-
public function testECC():void{
57+
public function testGetECC():void{
6558
$this::assertSame(EccLevel::L, $this->matrix->getEccLevel()->getLevel());
6659
}
6760

6861
/**
6962
* Tests if maskPattern() returns the current (or default) mask pattern
7063
*/
71-
public function testMaskPattern():void{
64+
public function testGetMaskPattern():void{
7265
// set via matrix evaluation
7366
$matrix = (new QRCode)->addByteSegment('testdata')->getQRMatrix();
7467

@@ -326,7 +319,7 @@ public function testRotate90(QRMatrix $matrix):void{
326319
/**
327320
* Tests if the logo space is drawn square if one of the dimensions is omitted
328321
*/
329-
public function testSetLogoSpaceOmitHeight():void{
322+
public function testSetLogoSpaceOmitDimension():void{
330323
$o = new QROptions;
331324
$o->version = 2;
332325
$o->eccLevel = EccLevel::H;
@@ -336,7 +329,7 @@ public function testSetLogoSpaceOmitHeight():void{
336329

337330
$matrix = (new QRCode($o))->addByteSegment('testdata')->getQRMatrix();
338331

339-
$this::debugMatrix($matrix);
332+
$this->debugMatrix($matrix);
340333

341334
$this::assertFalse($matrix->checkType(9, 9, QRMatrix::M_LOGO));
342335
$this::assertTrue($matrix->checkType(10, 10, QRMatrix::M_LOGO));
@@ -358,7 +351,7 @@ public function testSetLogoSpaceOrientation():void{
358351
// also testing size adjustment to uneven numbers
359352
$matrix->setLogoSpace(20, 14);
360353

361-
$this::debugMatrix($matrix);
354+
$this->debugMatrix($matrix);
362355

363356
// NW corner
364357
$this::assertFalse($matrix->checkType(17, 20, QRMatrix::M_LOGO));
@@ -382,7 +375,7 @@ public function testSetLogoSpacePosition():void{
382375
$matrix = (new QRCode($o))->addByteSegment('testdata')->getQRMatrix();
383376

384377
$matrix->setLogoSpace(21, 21, -10, -10);
385-
$this::debugMatrix($matrix);
378+
$this->debugMatrix($matrix);
386379
$this::assertSame(QRMatrix::M_QUIETZONE, $matrix->get(9, 9));
387380
$this::assertSame(QRMatrix::M_LOGO, $matrix->get(10, 10));
388381
$this::assertSame(QRMatrix::M_LOGO, $matrix->get(20, 20));
@@ -391,7 +384,7 @@ public function testSetLogoSpacePosition():void{
391384
// I just realized that setLogoSpace() could be called multiple times
392385
// on the same instance, and I'm not going to do anything about it :P
393386
$matrix->setLogoSpace(21, 21, 45, 45);
394-
$this::debugMatrix($matrix);
387+
$this->debugMatrix($matrix);
395388
$this::assertNotSame(QRMatrix::M_LOGO, $matrix->get(54, 54));
396389
$this::assertSame(QRMatrix::M_LOGO, $matrix->get(55, 55));
397390
$this::assertSame(QRMatrix::M_QUIETZONE, $matrix->get(67, 67));

0 commit comments

Comments
 (0)