Skip to content

Commit 6bba12d

Browse files
committed
:octocat: add QROutputAbstract::getModuleValue() to avoid directly accessing the $moduleValues array
1 parent a4ae0e9 commit 6bba12d

File tree

8 files changed

+24
-14
lines changed

8 files changed

+24
-14
lines changed

src/Output/QREps.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ public function dump(string $file = null):string{
108108
}
109109

110110
// 4 values will be interpreted as CMYK, 3 as RGB
111-
$format = (count($this->moduleValues[$M_TYPE]) === 4) ? '%f %f %f %f C' : '%f %f %f R';
112-
$eps[] = sprintf($format, ...$this->moduleValues[$M_TYPE]);
113-
$eps[] = implode("\n", $path);
111+
$val = $this->getModuleValue($M_TYPE);
112+
$format = (count($val) === 4) ? '%f %f %f %f C' : '%f %f %f R';
113+
$eps[] = sprintf($format, ...$val);
114+
$eps[] = implode("\n", $path);
114115
}
115116

116117
// end file

src/Output/QRFpdf.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ public function dump(string $file = null){
123123
continue;
124124
}
125125

126-
/** @var int $M_TYPE */
127-
$color = $this->moduleValues[$M_TYPE];
126+
$color = $this->getModuleValue($M_TYPE);
128127

129128
if($prevColor !== $color){
130129
/** @phan-suppress-next-line PhanParamTooFewUnpack */

src/Output/QRGdImage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,15 @@ protected function setPixel(int $x, int $y, int $M_TYPE):void{
252252
(int)(($y * $this->scale) + ($this->scale / 2)),
253253
(int)(2 * $this->options->circleRadius * $this->scale),
254254
(int)(2 * $this->options->circleRadius * $this->scale),
255-
$this->moduleValues[$M_TYPE]
255+
$this->getModuleValue($M_TYPE)
256256
)
257257
: imagefilledrectangle(
258258
$this->image,
259259
($x * $this->scale),
260260
($y * $this->scale),
261261
(($x + 1) * $this->scale),
262262
(($y + 1) * $this->scale),
263-
$this->moduleValues[$M_TYPE]
263+
$this->getModuleValue($M_TYPE)
264264
);
265265
}
266266

src/Output/QRImagick.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ protected function setPixel(int $x, int $y, int $M_TYPE):void{
204204
return;
205205
}
206206

207-
$this->imagickDraw->setFillColor($this->moduleValues[$M_TYPE]);
207+
$this->imagickDraw->setFillColor($this->getModuleValue($M_TYPE));
208208

209209
$this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)
210210
? $this->imagickDraw->circle(

src/Output/QRMarkupHTML.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected function createMarkup(bool $saveToFile):string{
3131
$html .= '<div>';
3232

3333
foreach($row as $M_TYPE){
34-
$html .= sprintf('<span style="background: %s;"></span>', $this->moduleValues[$M_TYPE]);
34+
$html .= sprintf('<span style="background: %s;"></span>', $this->getModuleValue($M_TYPE));
3535
}
3636

3737
$html .= '</div>'.$this->options->eol;

src/Output/QRMarkupSVG.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,17 @@ protected function paths():string{
124124
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path
125125
*/
126126
protected function path(string $path, int $M_TYPE):string{
127+
$val = $this->getModuleValue($M_TYPE);
127128
// ignore non-existent module values
128-
$format = !isset($this->moduleValues[$M_TYPE]) || empty($this->moduleValues[$M_TYPE])
129+
$format = empty($val)
129130
? '<path class="%1$s" d="%2$s"/>'
130131
: '<path class="%1$s" fill="%3$s" fill-opacity="%4$s" d="%2$s"/>';
131132

132133
return sprintf(
133134
$format,
134135
$this->getCssClass($M_TYPE),
135136
$path,
136-
($this->moduleValues[$M_TYPE] ?? ''), // value may or may not exist
137+
($val ?? ''), // value may or may not exist
137138
$this->options->svgOpacity
138139
);
139140
}

src/Output/QROutputAbstract.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,16 @@ protected function setModuleValues():void{
9292
}
9393

9494
/**
95-
* Returns the final value for the given input (return value depends on the output module)
95+
* Returns the prepared value for the given $M_TYPE (return value depends on the output class)
96+
*
97+
* @return mixed|null
98+
*/
99+
protected function getModuleValue(int $M_TYPE){
100+
return ($this->moduleValues[$M_TYPE] ?? null);
101+
}
102+
103+
/**
104+
* Prepares the value for the given input (return value depends on the output class)
96105
*
97106
* @param mixed $value
98107
*
@@ -101,7 +110,7 @@ protected function setModuleValues():void{
101110
abstract protected function prepareModuleValue($value);
102111

103112
/**
104-
* Returns a default value for either dark or light modules (return value depends on the output module)
113+
* Returns a default value for either dark or light modules (return value depends on the output class)
105114
*
106115
* @return mixed
107116
*/

src/Output/QRString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected function text():string{
7070
$r = [];
7171

7272
foreach($row as $M_TYPE){
73-
$r[] = $this->moduleValues[$M_TYPE];
73+
$r[] = $this->getModuleValue($M_TYPE);
7474
}
7575

7676
$str[] = implode('', $r);

0 commit comments

Comments
 (0)