:octocat: QRMatrix: remove property getters in favor of asymmetric visibility

This commit is contained in:
smiley
2026-03-18 01:19:14 +01:00
parent 8bb5a85d7b
commit 88b2f9a98c
25 changed files with 68 additions and 105 deletions
+1 -1
View File
@@ -57,7 +57,7 @@ class QRImageWithLogo extends QRGdImagePNG{
$lh = (($this->options->logoSpaceHeight - 2) * $this->options->scale);
// get the qrcode size
$ql = ($this->matrix->getSize() * $this->options->scale);
$ql = ($this->matrix->moduleCount * $this->options->scale);
// scale the logo and copy it over. done!
imagecopyresampled($this->image, $im, (($ql - $lw) / 2), (($ql - $lh) / 2), 0, 0, $lw, $lh, $w, $h);
+1 -1
View File
@@ -37,7 +37,7 @@ class MeltedSVGQRCodeOutput extends QRMarkupSVG{
$melt = $this->options->melt; // avoid magic getter in long loops
// collect the modules for each type
foreach($this->matrix->getMatrix() as $y => $row){
foreach($this->matrix->matrix as $y => $row){
foreach($row as $x => $M_TYPE){
$M_TYPE_LAYER = $M_TYPE;
+1 -1
View File
@@ -34,7 +34,7 @@ class GroupShapeSVGQRCodeOutput extends QRMarkupSVG{
$melt = $this->options->melt; // avoid magic getter in long loops
// collect the modules for each type
foreach($this->matrix->getMatrix() as $y => $row){
foreach($this->matrix->matrix as $y => $row){
foreach($row as $x => $M_TYPE){
$M_TYPE_LAYER = $M_TYPE;
+1 -1
View File
@@ -42,7 +42,7 @@ class RandomDotsSVGOutput extends QRMarkupSVG{
$dotColors = $this->options->dotColors; // avoid magic getter in long loops
// collect the modules for each type
foreach($this->matrix->getMatrix() as $y => $row){
foreach($this->matrix->matrix as $y => $row){
foreach($row as $x => $M_TYPE){
$M_TYPE_LAYER = $M_TYPE;
+1 -1
View File
@@ -163,7 +163,7 @@ class RoundQuietzoneSVGoutput extends QRMarkupSVG{
$dotColors = $this->options->dotColors; // avoid magic getter in long loops
// collect the modules for each type
foreach($this->matrix->getMatrix() as $y => $row){
foreach($this->matrix->matrix as $y => $row){
foreach($row as $x => $M_TYPE){
$M_TYPE_LAYER = $M_TYPE;
+1 -1
View File
@@ -118,7 +118,7 @@ final class MaskPattern{
*/
public static function getBestPattern(QRMatrix $QRMatrix):self{
$penalties = [];
$size = $QRMatrix->getSize();
$size = $QRMatrix->moduleCount;
foreach(self::PATTERNS as $pattern){
$mp = new self($pattern);
+7 -44
View File
@@ -79,12 +79,12 @@ class QRMatrix{
/**
* the matrix version - always set in QRMatrix, may be null in BitMatrix
*/
protected Version|null $version = null;
protected(set) Version|null $version = null;
/**
* the current ECC level - always set in QRMatrix, may be null in BitMatrix
*/
protected EccLevel|null $eccLevel = null;
protected(set) EccLevel|null $eccLevel = null;
/**
* the mask pattern that was used in the most recent operation, set via:
@@ -93,19 +93,21 @@ class QRMatrix{
* - QRMatrix::mask()
* - BitMatrix::readFormatInformation()
*/
protected MaskPattern|null $maskPattern = null;
protected(set) MaskPattern|null $maskPattern = null;
/**
* the size (side length) of the matrix, including quiet zone (if created)
*
* size = version * 4 + 17 [ + 2 * quietzone size]
*/
protected int $moduleCount;
protected(set) int $moduleCount;
/**
* the actual matrix data array
*
* @var int[][]
*/
protected array $matrix;
protected(set) array $matrix;
/**
* QRMatrix constructor.
@@ -141,15 +143,6 @@ class QRMatrix{
;
}
/**
* Returns the data matrix
*
* @return int[][]
*/
public function getMatrix():array{
return $this->matrix;
}
/**
* Returns a boolean representation of the data matrix
*
@@ -166,36 +159,6 @@ class QRMatrix{
return $matrix;
}
/**
* Returns the current version number
*/
public function getVersion():Version|null{
return $this->version;
}
/**
* Returns the current ECC level
*/
public function getEccLevel():EccLevel|null{
return $this->eccLevel;
}
/**
* Returns the current mask pattern
*/
public function getMaskPattern():MaskPattern|null{
return $this->maskPattern;
}
/**
* Returns the absoulute size of the matrix, including quiet zone (after setting it).
*
* size = version * 4 + 17 [ + 2 * quietzone size]
*/
public function getSize():int{
return $this->moduleCount;
}
/**
* Returns the value of the module at position [$x, $y] or -1 if the coordinate is outside the matrix
*/
+3 -3
View File
@@ -87,9 +87,9 @@ final class Decoder{
private function decodeMatrix(BitMatrix $matrix):DecoderResult{
// Read raw codewords
$rawCodewords = $matrix->readCodewords();
$this->version = $matrix->getVersion();
$this->eccLevel = $matrix->getEccLevel();
$this->maskPattern = $matrix->getMaskPattern();
$this->version = $matrix->version;
$this->eccLevel = $matrix->eccLevel;
$this->maskPattern = $matrix->maskPattern;
if($this->version === null || $this->eccLevel === null || $this->maskPattern === null){
throw new QRCodeDecoderException('unable to read version or format info'); // @codeCoverageIgnore
+1 -1
View File
@@ -221,7 +221,7 @@ final class AlignmentPatternFinder{
* returns vertical center of alignment pattern, or null if not found
*/
private function crossCheckVertical(int $startI, int $centerJ, int $maxCount, int $originalStateCountTotal):float|null{
$maxI = $this->matrix->getSize();
$maxI = $this->matrix->moduleCount;
$stateCount = [];
$stateCount[0] = 0;
$stateCount[1] = 0;
+2 -2
View File
@@ -135,7 +135,7 @@ final class Detector{
*/
private function sizeOfBlackWhiteBlackRunBothWays(float $fromX, float $fromY, float $toX, float $toY):float{
$result = $this->sizeOfBlackWhiteBlackRun((int)$fromX, (int)$fromY, (int)$toX, (int)$toY);
$dimension = $this->matrix->getSize();
$dimension = $this->matrix->moduleCount;
// Now count other way -- don't run off image though of course
$scale = 1.0;
$otherToX = ($fromX - ($toX - $fromX));
@@ -289,7 +289,7 @@ final class Detector{
float $allowanceFactor,
):AlignmentPattern|null{
// Look for an alignment pattern (3 modules in size) around where it should be
$dimension = $this->matrix->getSize();
$dimension = $this->matrix->moduleCount;
$allowance = (int)($allowanceFactor * $overallEstModuleSize);
$alignmentAreaLeftX = max(0, ($estAlignmentX - $allowance));
$alignmentAreaRightX = min(($dimension - 1), ($estAlignmentX + $allowance));
+4 -4
View File
@@ -50,7 +50,7 @@ final class FinderPatternFinder{
* @return \chillerlan\QRCode\Detector\FinderPattern[]
*/
public function find():array{
$dimension = $this->matrix->getSize();
$dimension = $this->matrix->moduleCount;
// We are looking for black/white/black/white/black modules in
// 1:1:3:1:1 ratio; this tracks the number of such modules seen so far
@@ -280,7 +280,7 @@ final class FinderPatternFinder{
return false;
}
$dimension = $this->matrix->getSize();
$dimension = $this->matrix->moduleCount;
// Now also count down, right from center
$i = 1;
@@ -329,7 +329,7 @@ final class FinderPatternFinder{
* @noinspection DuplicatedCode
*/
private function crossCheckVertical(int $startI, int $centerJ, int $maxCount, int $originalStateCountTotal):float|null{
$maxI = $this->matrix->getSize();
$maxI = $this->matrix->moduleCount;
$stateCount = self::crossCheckStateCount;
// Start counting up from center
@@ -413,7 +413,7 @@ final class FinderPatternFinder{
* @noinspection DuplicatedCode
*/
private function crossCheckHorizontal(int $startJ, int $centerI, int $maxCount, int $originalStateCountTotal):float|null{
$maxJ = $this->matrix->getSize();
$maxJ = $this->matrix->moduleCount;
$stateCount = self::crossCheckStateCount;
$j = $startJ;
+1 -1
View File
@@ -150,7 +150,7 @@ final class GridSampler{
[$this->points, ] = $transform->transformPoints($this->points);
// Quick check to see if points transformed to something inside the image;
// sufficient to check the endpoints
$this->checkAndNudgePoints($matrix->getSize());
$this->checkAndNudgePoints($matrix->moduleCount);
// no need to try/catch as QRMatrix::set() will silently discard out of bounds values
# try{
+1 -1
View File
@@ -77,7 +77,7 @@ class QRFpdf extends QROutputAbstract{
$this->prevColor = null;
foreach($this->matrix->getMatrix() as $y => $row){
foreach($this->matrix->matrix as $y => $row){
foreach($row as $x => $M_TYPE){
$this->module($x, $y, $M_TYPE);
}
+1 -1
View File
@@ -263,7 +263,7 @@ abstract class QRGdImage extends QROutputAbstract{
* Draws the QR image
*/
protected function drawImage():void{
foreach($this->matrix->getMatrix() as $y => $row){
foreach($this->matrix->matrix as $y => $row){
foreach($row as $x => $M_TYPE){
$this->module($x, $y, $M_TYPE);
}
+1 -1
View File
@@ -183,7 +183,7 @@ class QRImagick extends QROutputAbstract{
$this->imagickDraw = new ImagickDraw;
$this->imagickDraw->setStrokeWidth(0);
foreach($this->matrix->getMatrix() as $y => $row){
foreach($this->matrix->matrix as $y => $row){
foreach($row as $x => $M_TYPE){
$this->module($x, $y, $M_TYPE);
}
+1 -1
View File
@@ -102,7 +102,7 @@ class QRInterventionImage extends QROutputAbstract{
$this->image->fill($this->prepareModuleValue($this->options->bgColor));
}
foreach($this->matrix->getMatrix() as $y => $row){
foreach($this->matrix->matrix as $y => $row){
foreach($row as $x => $M_TYPE){
$this->module($x, $y, $M_TYPE);
}
+1 -1
View File
@@ -24,7 +24,7 @@ class QRMarkupHTML extends QRMarkup{
$rows = [];
$cssClass = $this->getCssClass();
foreach($this->matrix->getMatrix() as $row){
foreach($this->matrix->matrix as $row){
$element = '<span style="background: %s;"></span>';
$modules = array_map(fn(int $M_TYPE):string => sprintf($element, $this->getModuleValue($M_TYPE)), $row);
+5 -5
View File
@@ -55,8 +55,8 @@ class QRMarkupXML extends QRMarkup{
$root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$root->setAttribute('xsi:noNamespaceSchemaLocation', $this::SCHEMA);
$root->setAttribute('version', (string)$this->matrix->getVersion());
$root->setAttribute('eccLevel', (string)$this->matrix->getEccLevel());
$root->setAttribute('version', (string)$this->matrix->version);
$root->setAttribute('eccLevel', (string)$this->matrix->eccLevel);
$root->appendChild($this->createMatrix());
$this->dom->appendChild($root);
@@ -76,15 +76,15 @@ class QRMarkupXML extends QRMarkup{
protected function createMatrix():DOMElement{
[$width, $height] = $this->getOutputDimensions();
$matrix = $this->dom->createElement('matrix');
$dimension = $this->matrix->getVersion()->getDimension();
$dimension = $this->matrix->version->getDimension();
$matrix->setAttribute('size', (string)$dimension);
$matrix->setAttribute('quietzoneSize', (string)(int)(($this->moduleCount - $dimension) / 2));
$matrix->setAttribute('maskPattern', (string)$this->matrix->getMaskPattern()->getPattern());
$matrix->setAttribute('maskPattern', (string)$this->matrix->maskPattern->getPattern());
$matrix->setAttribute('width', (string)$width);
$matrix->setAttribute('height', (string)$height);
foreach($this->matrix->getMatrix() as $y => $row){
foreach($this->matrix->matrix as $y => $row){
$matrixRow = $this->row($y, $row);
if($matrixRow !== null){
+3 -3
View File
@@ -28,7 +28,7 @@ abstract class QROutputAbstract implements QROutputInterface{
/**
* the current size of the QR matrix
*
* @see \chillerlan\QRCode\Data\QRMatrix::getSize()
* @see \chillerlan\QRCode\Data\QRMatrix::$moduleCount
*/
protected int $moduleCount;
@@ -130,7 +130,7 @@ abstract class QROutputAbstract implements QROutputInterface{
* Call this method if you modify the matrix from within your custom module in case the dimensions have been changed
*/
protected function setMatrixDimensions():void{
$this->moduleCount = $this->matrix->getSize();
$this->moduleCount = $this->matrix->moduleCount;
$this->scale = $this->options->scale;
$this->length = ($this->moduleCount * $this->scale);
}
@@ -268,7 +268,7 @@ abstract class QROutputAbstract implements QROutputInterface{
$paths = [];
// collect the modules for each type
foreach($this->matrix->getMatrix() as $y => $row){
foreach($this->matrix->matrix as $y => $row){
foreach($row as $x => $M_TYPE){
$M_TYPE_LAYER = $M_TYPE;
+4 -4
View File
@@ -44,18 +44,18 @@ class QRStringJSON extends QROutputAbstract{
*/
public function dump(string|null $file = null):string{
[$width, $height] = $this->getOutputDimensions();
$version = $this->matrix->getVersion();
$version = $this->matrix->version;
$dimension = $version->getDimension();
$json = [
'$schema' => $this::SCHEMA,
'qrcode' => [
'version' => $version->getVersionNumber(),
'eccLevel' => (string)$this->matrix->getEccLevel(),
'eccLevel' => (string)$this->matrix->eccLevel,
'matrix' => [
'size' => $dimension,
'quietzoneSize' => (int)(($this->moduleCount - $dimension) / 2),
'maskPattern' => $this->matrix->getMaskPattern()->getPattern(),
'maskPattern' => $this->matrix->maskPattern->getPattern(),
'width' => $width,
'height' => $height,
'rows' => [],
@@ -63,7 +63,7 @@ class QRStringJSON extends QROutputAbstract{
],
];
foreach($this->matrix->getMatrix() as $y => $row){
foreach($this->matrix->matrix as $y => $row){
$matrixRow = $this->row($y, $row);
if($matrixRow !== null){
+1 -1
View File
@@ -36,7 +36,7 @@ class QRStringText extends QROutputAbstract{
$lines = [];
$linestart = $this->options->textLineStart;
foreach($this->matrix->getMatrix() as $row){
foreach($this->matrix->matrix as $row){
$lines[] = $linestart.implode('', array_map($this->getModuleValue(...), $row));
}
+1 -1
View File
@@ -59,7 +59,7 @@ abstract class DataInterfaceTestAbstract extends TestCase{
$matrix = $this->QRData->writeMatrix()->setFormatInfo($maskPattern)->mask($maskPattern);
$this::assertInstanceOf(QRMatrix::class, $matrix);
$this::assertSame($pattern, $matrix->getMaskPattern()->getPattern());
$this::assertSame($pattern, $matrix->maskPattern->getPattern());
}
/**
+1 -1
View File
@@ -47,7 +47,7 @@ final class QRDataTest extends TestCase{
$matrix->setFormatInfo($maskPattern)->mask($maskPattern);
$this::assertSame(3, $matrix->getVersion()->getVersionNumber());
$this::assertSame(3, $matrix->version->getVersionNumber());
// attempt to read
$options->outputBase64 = false;
+23 -23
View File
@@ -43,7 +43,7 @@ final class QRMatrixTest extends TestCase{
*/
#[Test]
public function getSize():void{
$this::assertCount($this->matrix->getSize(), $this->matrix->getBooleanMatrix());
$this::assertCount($this->matrix->moduleCount, $this->matrix->getBooleanMatrix());
}
/**
@@ -51,7 +51,7 @@ final class QRMatrixTest extends TestCase{
*/
#[Test]
public function getVersion():void{
$this::assertSame($this::version, $this->matrix->getVersion()->getVersionNumber());
$this::assertSame($this::version, $this->matrix->version->getVersionNumber());
}
/**
@@ -59,7 +59,7 @@ final class QRMatrixTest extends TestCase{
*/
#[Test]
public function getECC():void{
$this::assertSame(EccLevel::L, $this->matrix->getEccLevel()->getLevel());
$this::assertSame(EccLevel::L, $this->matrix->eccLevel->getLevel());
}
/**
@@ -70,8 +70,8 @@ final class QRMatrixTest extends TestCase{
// set via matrix evaluation
$matrix = new QRCode()->addByteSegment('testdata')->getQRMatrix();
$this::assertInstanceOf(MaskPattern::class, $matrix->getMaskPattern());
$this::assertSame(MaskPattern::PATTERN_100, $matrix->getMaskPattern()->getPattern());
$this::assertInstanceOf(MaskPattern::class, $matrix->maskPattern);
$this::assertSame(MaskPattern::PATTERN_100, $matrix->maskPattern->getPattern());
}
/**
@@ -120,7 +120,7 @@ final class QRMatrixTest extends TestCase{
$this->dm($matrix);
$this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get(8, ($matrix->getSize() - 8)));
$this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get(8, ($matrix->moduleCount - 8)));
}
/**
@@ -134,8 +134,8 @@ final class QRMatrixTest extends TestCase{
$this->dm($matrix);
$this::assertSame(QRMatrix::M_FINDER_DARK, $matrix->get(0, 0));
$this::assertSame(QRMatrix::M_FINDER_DARK, $matrix->get(0, ($matrix->getSize() - 1)));
$this::assertSame(QRMatrix::M_FINDER_DARK, $matrix->get(($matrix->getSize() - 1), 0));
$this::assertSame(QRMatrix::M_FINDER_DARK, $matrix->get(0, ($matrix->moduleCount - 1)));
$this::assertSame(QRMatrix::M_FINDER_DARK, $matrix->get(($matrix->moduleCount - 1), 0));
}
/**
@@ -150,8 +150,8 @@ final class QRMatrixTest extends TestCase{
$this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(7, 0));
$this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(0, 7));
$this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(0, ($matrix->getSize() - 8)));
$this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(($matrix->getSize() - 8), 0));
$this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(0, ($matrix->moduleCount - 8)));
$this::assertSame(QRMatrix::M_SEPARATOR, $matrix->get(($matrix->moduleCount - 8), 0));
}
/**
@@ -160,7 +160,7 @@ final class QRMatrixTest extends TestCase{
#[Test]
#[DataProvider('matrixProvider')]
public function setAlignmentPattern(QRMatrix $matrix):void{
$version = $matrix->getVersion();
$version = $matrix->version;
if($version->getVersionNumber() === 1){
$this::markTestSkipped('N/A (Version 1 has no alignment pattern)');
@@ -201,7 +201,7 @@ final class QRMatrixTest extends TestCase{
$this->dm($matrix);
$size = $matrix->getSize();
$size = $matrix->moduleCount;
for($i = 7; $i < ($size - 7); $i++){
if(($i % 2) === 0){
@@ -222,7 +222,7 @@ final class QRMatrixTest extends TestCase{
#[DataProvider('matrixProvider')]
public function setVersionNumber(QRMatrix $matrix):void{
if($matrix->getVersion()->getVersionNumber() < 7){
if($matrix->version->getVersionNumber() < 7){
$this::markTestSkipped('N/A (Version < 7)');
}
@@ -230,10 +230,10 @@ final class QRMatrixTest extends TestCase{
$this->dm($matrix);
$this::assertTrue($matrix->checkType(($matrix->getSize() - 9), 0, QRMatrix::M_VERSION));
$this::assertTrue($matrix->checkType(($matrix->getSize() - 11), 5, QRMatrix::M_VERSION));
$this::assertTrue($matrix->checkType(0, ($matrix->getSize() - 9), QRMatrix::M_VERSION));
$this::assertTrue($matrix->checkType(5, ($matrix->getSize() - 11), QRMatrix::M_VERSION));
$this::assertTrue($matrix->checkType(($matrix->moduleCount - 9), 0, QRMatrix::M_VERSION));
$this::assertTrue($matrix->checkType(($matrix->moduleCount - 11), 5, QRMatrix::M_VERSION));
$this::assertTrue($matrix->checkType(0, ($matrix->moduleCount - 9), QRMatrix::M_VERSION));
$this::assertTrue($matrix->checkType(5, ($matrix->moduleCount - 11), QRMatrix::M_VERSION));
}
/**
@@ -248,8 +248,8 @@ final class QRMatrixTest extends TestCase{
$this::assertTrue($matrix->checkType(8, 0, QRMatrix::M_FORMAT));
$this::assertTrue($matrix->checkType(0, 8, QRMatrix::M_FORMAT));
$this::assertTrue($matrix->checkType(($matrix->getSize() - 1), 8, QRMatrix::M_FORMAT));
$this::assertTrue($matrix->checkType(($matrix->getSize() - 8), 8, QRMatrix::M_FORMAT));
$this::assertTrue($matrix->checkType(($matrix->moduleCount - 1), 8, QRMatrix::M_FORMAT));
$this::assertTrue($matrix->checkType(($matrix->moduleCount - 8), 8, QRMatrix::M_FORMAT));
}
/**
@@ -258,7 +258,7 @@ final class QRMatrixTest extends TestCase{
#[Test]
#[DataProvider('matrixProvider')]
public function setQuietZone(QRMatrix $matrix):void{
$size = $matrix->getSize();
$size = $matrix->moduleCount;
$quietZoneSize = 5;
$matrix->set(0, 0, true, QRMatrix::M_LOGO);
@@ -271,7 +271,7 @@ final class QRMatrixTest extends TestCase{
$this::assertCount($s, $matrix->getBooleanMatrix());
$this::assertCount($s, $matrix->getBooleanMatrix()[($size - 1)]);
$size = $matrix->getSize();
$size = $matrix->moduleCount;
$this->dm($matrix);
@@ -304,9 +304,9 @@ final class QRMatrixTest extends TestCase{
$matrix->initFunctionalPatterns();
// matrix size
$size = $matrix->getSize();
$size = $matrix->moduleCount;
// quiet zone size
$qz = (($size - $matrix->getVersion()->getDimension()) / 2);
$qz = (($size - $matrix->version->getDimension()) / 2);
// initial dark module position
$this::assertSame(QRMatrix::M_DARKMODULE, $matrix->get((8 + $qz), ($size - 8 - $qz)));
+1 -1
View File
@@ -85,7 +85,7 @@ trait QRMatrixDebugTrait{
// limit
/** @noinspection PhpUndefinedConstantInspection - see phpunit.xml.dist */
if(!defined('MATRIX_DEBUG_VERSION') || $matrix->getVersion()->getVersionNumber() !== MATRIX_DEBUG_VERSION){
if(!defined('MATRIX_DEBUG_VERSION') || $matrix->version->getVersionNumber() !== MATRIX_DEBUG_VERSION){
return;
}