From 9ec548fd324ca052eb68d86702a24e9678f6d591 Mon Sep 17 00:00:00 2001 From: smiley Date: Wed, 18 Mar 2026 17:14:05 +0100 Subject: [PATCH] :octocat: QROutputAbstract: remove property copies of previously magic properties --- examples/svgMeltedModules.php | 2 +- examples/svgModuleGroupShape.php | 2 +- examples/svgRandomColoredDots.php | 2 +- examples/svgRoundQuietzone.php | 2 +- src/Output/QREps.php | 2 +- src/Output/QRFpdf.php | 2 +- src/Output/QRGdImage.php | 11 +++---- src/Output/QRImagick.php | 6 ++-- src/Output/QRInterventionImage.php | 6 ++-- src/Output/QRMarkupHTML.php | 6 ++-- src/Output/QRMarkupSVG.php | 22 +++++++------ src/Output/QRMarkupXML.php | 2 +- src/Output/QROutputAbstract.php | 51 +++--------------------------- src/Output/QRStringJSON.php | 2 +- src/Output/QRStringText.php | 2 +- 15 files changed, 39 insertions(+), 81 deletions(-) diff --git a/examples/svgMeltedModules.php b/examples/svgMeltedModules.php index 02a63d251..3c1540e54 100644 --- a/examples/svgMeltedModules.php +++ b/examples/svgMeltedModules.php @@ -41,7 +41,7 @@ class MeltedSVGQRCodeOutput extends QRMarkupSVG{ foreach($row as $x => $M_TYPE){ $M_TYPE_LAYER = $M_TYPE; - if($this->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->excludeFromConnect)){ + if($this->options->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->options->excludeFromConnect)){ // to connect paths we'll redeclare the $M_TYPE_LAYER to data only $M_TYPE_LAYER = QRMatrix::M_DATA; diff --git a/examples/svgModuleGroupShape.php b/examples/svgModuleGroupShape.php index 1ed5840df..5215a4a4b 100644 --- a/examples/svgModuleGroupShape.php +++ b/examples/svgModuleGroupShape.php @@ -38,7 +38,7 @@ class GroupShapeSVGQRCodeOutput extends QRMarkupSVG{ foreach($row as $x => $M_TYPE){ $M_TYPE_LAYER = $M_TYPE; - if($this->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->excludeFromConnect)){ + if($this->options->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->options->excludeFromConnect)){ // to connect paths we'll redeclare the $M_TYPE_LAYER to data only $M_TYPE_LAYER = QRMatrix::M_DATA; diff --git a/examples/svgRandomColoredDots.php b/examples/svgRandomColoredDots.php index a1cdfdc52..8033bad6f 100644 --- a/examples/svgRandomColoredDots.php +++ b/examples/svgRandomColoredDots.php @@ -46,7 +46,7 @@ class RandomDotsSVGOutput extends QRMarkupSVG{ foreach($row as $x => $M_TYPE){ $M_TYPE_LAYER = $M_TYPE; - if($this->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->excludeFromConnect)){ + if($this->options->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->options->excludeFromConnect)){ // to connect paths we'll redeclare the $M_TYPE_LAYER to data only $M_TYPE_LAYER = QRMatrix::M_DATA; diff --git a/examples/svgRoundQuietzone.php b/examples/svgRoundQuietzone.php index 7f622d40a..c3b797f42 100644 --- a/examples/svgRoundQuietzone.php +++ b/examples/svgRoundQuietzone.php @@ -167,7 +167,7 @@ class RoundQuietzoneSVGoutput extends QRMarkupSVG{ foreach($row as $x => $M_TYPE){ $M_TYPE_LAYER = $M_TYPE; - if($this->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->excludeFromConnect)){ + if($this->options->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->options->excludeFromConnect)){ // to connect paths we'll redeclare the $M_TYPE_LAYER to data only $M_TYPE_LAYER = QRMatrix::M_DATA; diff --git a/src/Output/QREps.php b/src/Output/QREps.php index 6d418086d..75fa83687 100644 --- a/src/Output/QREps.php +++ b/src/Output/QREps.php @@ -164,7 +164,7 @@ class QREps extends QROutputAbstract{ */ protected function moduleTransform(int $x, int $y, int $M_TYPE, int $M_TYPE_LAYER):string|null{ - if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){ + if(!$this->options->drawLightModules && !$this->matrix->isDark($M_TYPE)){ return null; } diff --git a/src/Output/QRFpdf.php b/src/Output/QRFpdf.php index 784100a0c..a23bad3b3 100644 --- a/src/Output/QRFpdf.php +++ b/src/Output/QRFpdf.php @@ -103,7 +103,7 @@ class QRFpdf extends QROutputAbstract{ */ protected function module(int $x, int $y, int $M_TYPE):void{ - if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){ + if(!$this->options->drawLightModules && !$this->matrix->isDark($M_TYPE)){ return; } diff --git a/src/Output/QRGdImage.php b/src/Output/QRGdImage.php index 47feb4a01..84bf729e5 100644 --- a/src/Output/QRGdImage.php +++ b/src/Output/QRGdImage.php @@ -71,7 +71,6 @@ abstract class QRGdImage extends QROutputAbstract{ $this->matrix->invert(); } - $this->copyVars(); $this->setMatrixDimensions(); } @@ -200,7 +199,7 @@ abstract class QRGdImage extends QROutputAbstract{ */ protected function createImage():GdImage{ - if($this->drawCircularModules && $this->options->gdImageUseUpscale && $this->options->scale < 20){ + if($this->options->drawCircularModules && $this->options->gdImageUseUpscale && $this->options->scale < 20){ // increase the initial image size by 10 $this->length *= 10; $this->scale *= 10; @@ -275,19 +274,19 @@ abstract class QRGdImage extends QROutputAbstract{ */ protected function module(int $x, int $y, int $M_TYPE):void{ - if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){ + if(!$this->options->drawLightModules && !$this->matrix->isDark($M_TYPE)){ return; } $color = $this->getModuleValue($M_TYPE); - if($this->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->keepAsSquare)){ + if($this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)){ imagefilledellipse( $this->image, (($x * $this->scale) + intdiv($this->scale, 2)), (($y * $this->scale) + intdiv($this->scale, 2)), - (int)($this->circleDiameter * $this->scale), - (int)($this->circleDiameter * $this->scale), + (int)($this->options->circleRadius * 2 * $this->scale), + (int)($this->options->circleRadius * 2 * $this->scale), $color, ); diff --git a/src/Output/QRImagick.php b/src/Output/QRImagick.php index a6a1b6503..9a753e61b 100644 --- a/src/Output/QRImagick.php +++ b/src/Output/QRImagick.php @@ -197,17 +197,17 @@ class QRImagick extends QROutputAbstract{ */ protected function module(int $x, int $y, int $M_TYPE):void{ - if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){ + if(!$this->options->drawLightModules && !$this->matrix->isDark($M_TYPE)){ return; } $this->imagickDraw->setFillColor($this->getModuleValue($M_TYPE)); - if($this->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->keepAsSquare)){ + if($this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)){ $this->imagickDraw->circle( (($x + 0.5) * $this->scale), (($y + 0.5) * $this->scale), - (($x + 0.5 + $this->circleRadius) * $this->scale), + (($x + 0.5 + $this->options->circleRadius) * $this->scale), (($y + 0.5) * $this->scale), ); diff --git a/src/Output/QRInterventionImage.php b/src/Output/QRInterventionImage.php index 461fa9dd9..a68fb0faa 100644 --- a/src/Output/QRInterventionImage.php +++ b/src/Output/QRInterventionImage.php @@ -129,19 +129,19 @@ class QRInterventionImage extends QROutputAbstract{ */ protected function module(int $x, int $y, int $M_TYPE):void{ - if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){ + if(!$this->options->drawLightModules && !$this->matrix->isDark($M_TYPE)){ return; } $color = $this->getModuleValue($M_TYPE); - if($this->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->keepAsSquare)){ + if($this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)){ $this->image->drawCircle( (($x * $this->scale) + intdiv($this->scale, 2)), (($y * $this->scale) + intdiv($this->scale, 2)), function(CircleFactory $circle) use ($color):void{ - $circle->radius((int)($this->circleRadius * $this->scale)); + $circle->radius((int)($this->options->circleRadius * $this->scale)); $circle->background($color); }, ); diff --git a/src/Output/QRMarkupHTML.php b/src/Output/QRMarkupHTML.php index 10e182d32..fa154e06b 100644 --- a/src/Output/QRMarkupHTML.php +++ b/src/Output/QRMarkupHTML.php @@ -28,10 +28,10 @@ class QRMarkupHTML extends QRMarkup{ $element = ''; $modules = array_map(fn(int $M_TYPE):string => sprintf($element, $this->getModuleValue($M_TYPE)), $row); - $rows[] = sprintf('
%s
%s', implode('', $modules), $this->eol); + $rows[] = sprintf('
%s
%s', implode('', $modules), $this->options->eol); } - $html = sprintf('
%3$s%2$s
%3$s', $cssClass, implode('', $rows), $this->eol); + $html = sprintf('
%3$s%2$s
%3$s', $cssClass, implode('', $rows), $this->options->eol); // wrap the snippet into a body when saving to file if($saveToFile){ @@ -39,7 +39,7 @@ class QRMarkupHTML extends QRMarkup{ '%2$s%2$s%2$s'. 'QR Code%2$s%1$s%2$s', $html, - $this->eol, + $this->options->eol, ); } diff --git a/src/Output/QRMarkupSVG.php b/src/Output/QRMarkupSVG.php index 217776824..92815ebde 100644 --- a/src/Output/QRMarkupSVG.php +++ b/src/Output/QRMarkupSVG.php @@ -67,13 +67,13 @@ class QRMarkupSVG extends QRMarkup{ $svg = $this->header(); if($this->options->svgDefs !== ''){ - $svg .= sprintf('%1$s%2$s%2$s', $this->options->svgDefs, $this->eol); + $svg .= sprintf('%1$s%2$s%2$s', $this->options->svgDefs, $this->options->eol); } $svg .= $this->paths(); // close svg - $svg .= sprintf('%1$s%1$s', $this->eol); + $svg .= sprintf('%1$s%1$s', $this->options->eol); return $svg; } @@ -102,11 +102,11 @@ class QRMarkupSVG extends QRMarkup{ $this->options->cssClass, $this->getViewBox(), $this->options->svgPreserveAspectRatio, - $this->eol, + $this->options->eol, ); if($this->options->svgAddXmlHeader){ - $header = sprintf('%s%s', $this->eol, $header); + $header = sprintf('%s%s', $this->options->eol, $header); } return $header; @@ -129,7 +129,7 @@ class QRMarkupSVG extends QRMarkup{ $chonks[] = implode(' ', $chunk); } - $path = implode($this->eol, $chonks); + $path = implode($this->options->eol, $chonks); if($path === ''){ continue; @@ -138,7 +138,7 @@ class QRMarkupSVG extends QRMarkup{ $svg[] = $this->path($path, $M_TYPE); } - return implode($this->eol, $svg); + return implode($this->options->eol, $svg); } /** @@ -167,17 +167,19 @@ class QRMarkupSVG extends QRMarkup{ */ protected function moduleTransform(int $x, int $y, int $M_TYPE, int $M_TYPE_LAYER):string|null{ - if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){ + if(!$this->options->drawLightModules && !$this->matrix->isDark($M_TYPE)){ return null; } - if($this->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->keepAsSquare)){ + if($this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)){ // string interpolation: ugly and fast - $ix = ($x + 0.5 - $this->circleRadius); + $r = $this->options->circleRadius; + $d = $r * 2; + $ix = ($x + 0.5 - $r); $iy = ($y + 0.5); // phpcs:ignore - return "M$ix $iy a$this->circleRadius $this->circleRadius 0 1 0 $this->circleDiameter 0 a$this->circleRadius $this->circleRadius 0 1 0 -$this->circleDiameter 0Z"; + return "M$ix $iy a$r $r 0 1 0 $d 0 a$r $r 0 1 0 -$d 0Z"; } // phpcs:ignore diff --git a/src/Output/QRMarkupXML.php b/src/Output/QRMarkupXML.php index 7fbf2cd4c..81f68563b 100644 --- a/src/Output/QRMarkupXML.php +++ b/src/Output/QRMarkupXML.php @@ -128,7 +128,7 @@ class QRMarkupXML extends QRMarkup{ protected function module(int $x, int $y, int $M_TYPE):DOMElement|null{ $isDark = $this->matrix->isDark($M_TYPE); - if(!$this->drawLightModules && !$isDark){ + if(!$this->options->drawLightModules && !$isDark){ return null; } diff --git a/src/Output/QROutputAbstract.php b/src/Output/QROutputAbstract.php index 6df326b8f..c6ff3533a 100644 --- a/src/Output/QROutputAbstract.php +++ b/src/Output/QROutputAbstract.php @@ -55,28 +55,11 @@ abstract class QROutputAbstract implements QROutputInterface{ protected SettingsContainerInterface|QROptions $options; /** - * @see \chillerlan\QRCode\QROptions::$excludeFromConnect - * @var int[] + * The current module scale value (might have been modified) + * + * @see \chillerlan\QRCode\QROptions::$scale */ - protected array $excludeFromConnect; - /** - * @see \chillerlan\QRCode\QROptions::$keepAsSquare - * @var int[] - */ - protected array $keepAsSquare; - /** @see \chillerlan\QRCode\QROptions::$scale */ protected int $scale; - /** @see \chillerlan\QRCode\QROptions::$connectPaths */ - protected bool $connectPaths; - /** @see \chillerlan\QRCode\QROptions::$eol */ - protected string $eol; - /** @see \chillerlan\QRCode\QROptions::$drawLightModules */ - protected bool $drawLightModules; - /** @see \chillerlan\QRCode\QROptions::$drawCircularModules */ - protected bool $drawCircularModules; - /** @see \chillerlan\QRCode\QROptions::$circleRadius */ - protected float $circleRadius; - protected float $circleDiameter; /** * QROutputAbstract constructor. @@ -94,36 +77,10 @@ abstract class QROutputAbstract implements QROutputInterface{ $this->matrix->invert(); } - $this->copyVars(); $this->setMatrixDimensions(); $this->setModuleValues(); } - /** - * Creates copies of several QROptions values to avoid calling the magic getters/property hooks - * in long loops for a significant performance increase. - * - * These variables are usually used in the "module" methods and are called up to 31329 times (at version 40). - */ - protected function copyVars():void{ - - $vars = [ - 'connectPaths', - 'excludeFromConnect', - 'eol', - 'drawLightModules', - 'drawCircularModules', - 'keepAsSquare', - 'circleRadius', - ]; - - foreach($vars as $property){ - $this->{$property} = $this->options->{$property}; - } - - $this->circleDiameter = ($this->circleRadius * 2); - } - /** * Sets/updates the matrix dimensions * @@ -272,7 +229,7 @@ abstract class QROutputAbstract implements QROutputInterface{ foreach($row as $x => $M_TYPE){ $M_TYPE_LAYER = $M_TYPE; - if($this->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->excludeFromConnect)){ + if($this->options->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->options->excludeFromConnect)){ // to connect paths we'll redeclare the $M_TYPE_LAYER to data only $M_TYPE_LAYER = QRMatrix::M_DATA; diff --git a/src/Output/QRStringJSON.php b/src/Output/QRStringJSON.php index d01c92f32..94341cbd6 100644 --- a/src/Output/QRStringJSON.php +++ b/src/Output/QRStringJSON.php @@ -115,7 +115,7 @@ class QRStringJSON extends QROutputAbstract{ protected function module(int $x, int $y, int $M_TYPE):array|null{ $isDark = $this->matrix->isDark($M_TYPE); - if(!$this->drawLightModules && !$isDark){ + if(!$this->options->drawLightModules && !$isDark){ return null; } diff --git a/src/Output/QRStringText.php b/src/Output/QRStringText.php index 1437a6f51..27fe3720c 100644 --- a/src/Output/QRStringText.php +++ b/src/Output/QRStringText.php @@ -40,7 +40,7 @@ class QRStringText extends QROutputAbstract{ $lines[] = $linestart.implode('', array_map($this->getModuleValue(...), $row)); } - $data = implode($this->eol, $lines); + $data = implode($this->options->eol, $lines); $this->saveToFile($data, $file);