Skip to content

Commit 3bfa0be

Browse files
committed
:octocat: move determining min/max version
1 parent d746b66 commit 3bfa0be

3 files changed

Lines changed: 16 additions & 53 deletions

File tree

src/Data/QRData.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use chillerlan\QRCode\QROptions;
1515
use chillerlan\QRCode\Common\{BitBuffer, EccLevel, Mode, Version};
1616
use chillerlan\Settings\SettingsContainerInterface;
17-
use function sprintf;
17+
use function max, min, sprintf;
1818

1919
/**
2020
* Processes the binary data and maps it on a QRMatrix which is then being returned
@@ -193,10 +193,14 @@ public function getMinimumVersion():Version{
193193
return new Version($this->options->version);
194194
}
195195

196+
// we don't know whether the min/max versions were set correctly, so we determine here which value is higher
197+
$min = min($this->options->versionMin, $this->options->versionMax);
198+
$max = max($this->options->versionMin, $this->options->versionMax);
199+
196200
$total = $this->estimateTotalBitLength();
197201

198202
// guess the version number within the given range
199-
for($version = $this->options->versionMin; $version <= $this->options->versionMax; $version++){
203+
for($version = $min; $version <= $max; $version++){
200204
if($total <= ($this->maxBitsForEcc[$version] - 4)){
201205
return new Version($version);
202206
}

src/QROptionsTrait.php

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,22 @@ trait QROptionsTrait{
5151
*
5252
* if `QROptions::$version` is set to `Version::AUTO` (default: 1)
5353
*/
54-
protected int $versionMin = 1;
54+
protected int $versionMin = 1 {
55+
set{
56+
$this->versionMin = max(1, min(40, $value));
57+
}
58+
}
5559

5660
/**
5761
* Maximum QR version
5862
*
5963
* if `QROptions::$version` is set to `Version::AUTO` (default: 40)
6064
*/
61-
protected int $versionMax = 40;
65+
protected int $versionMax = 40 {
66+
set{
67+
$this->versionMax = max(1, min(40, $value));
68+
}
69+
}
6270

6371
/**
6472
* Error correct level
@@ -492,11 +500,6 @@ trait QROptionsTrait{
492500
* @see FPDF::__construct()
493501
*/
494502
public string $fpdfMeasureUnit = 'pt' {
495-
/**
496-
* sets the FPDF measurement unit
497-
*
498-
* @codeCoverageIgnore
499-
*/
500503
set{
501504
$value = strtolower($value);
502505

@@ -505,7 +508,6 @@ trait QROptionsTrait{
505508
}
506509

507510
// @todo throw or ignore silently?
508-
509511
}
510512
}
511513

@@ -521,34 +523,6 @@ trait QROptionsTrait{
521523
*/
522524
public string|null $xmlStylesheet = null;
523525

524-
525-
/**
526-
* clamp min/max version number
527-
*/
528-
protected function setMinMaxVersion(int $versionMin, int $versionMax):void{
529-
$min = max(1, min(40, $versionMin));
530-
$max = max(1, min(40, $versionMax));
531-
532-
$this->versionMin = min($min, $max);
533-
$this->versionMax = max($min, $max);
534-
}
535-
536-
/**
537-
* sets the minimum version number
538-
*
539-
* @todo: for some reason this crashes php when trying to access the other property ($this->versionMax) within the hook
540-
*/
541-
protected function set_versionMin(int $version):void{
542-
$this->setMinMaxVersion($version, $this->versionMax);
543-
}
544-
545-
/**
546-
* sets the maximum version number
547-
*/
548-
protected function set_versionMax(int $version):void{
549-
$this->setMinMaxVersion($this->versionMin, $version);
550-
}
551-
552526
/**
553527
* clamp the logo space values between 0 and maximum length (177 modules at version 40)
554528
*/

tests/QROptionsTest.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,6 @@ public function versionClamp(int $version, int $expected):void{
3737
$this::assertSame($expected, $o->version);
3838
}
3939

40-
/**
41-
* Tests the $versionMin/$versionMax clamping
42-
*/
43-
#[Test]
44-
#[TestWith([5, 10, 5, 10], 'normal clamp')]
45-
#[TestWith([-42, 42, 1, 40], 'exceeding values')]
46-
#[TestWith([10, 5, 5, 10], 'min > max' )]
47-
#[TestWith([42, -42, 1, 40], 'min > max, exceeding')]
48-
public function versionMinMaxClamp(int $versionMin, int $versionMax, int $expectedMin, int $expectedMax):void{
49-
$o = new QROptions(['versionMin' => $versionMin, 'versionMax' => $versionMax]);
50-
51-
$this::assertSame($expectedMin, $o->versionMin);
52-
$this::assertSame($expectedMax, $o->versionMax);
53-
}
54-
5540
/**
5641
* Tests setting the ECC level from string or int
5742
*

0 commit comments

Comments
 (0)